Bilinear

https://pytorch.org/docs/stable/generated/torch.nn.Bilinear.html

bilinear_demo.py
import svetoviz_webgpu as sv

# 1. Define Layer Parameters
# Learns the interaction between two separate feature spaces
bilinear_layer = nn.Bilinear(in1_features=20, in2_features=30, out_features=40)

# 2. Prepare Dual Input Tensors
input1 = torch.randn(1, 20)
input2 = torch.randn(1, 30)

def terminal_callback(buffer, message, images, files):
    output = bilinear_layer(input1, input2)

    buffer.send_system_message(f"Input 1 Features: {input1.shape[1]}")
    buffer.send_system_message(f"Input 2 Features: {input2.shape[1]}")

    # Total params roughly (in1 * in2 * out)
    params = sum(p.numel() for p in bilinear_layer.parameters())
    buffer.send_system_message(f"Total Learnable Parameters: {params}")
    buffer.send_system_message("Logic: Captures multiplicative interactions between feature spaces.")

# 3. Start the interactive session
sv.pytorch_web(module=bilinear_layer, terminal_callback=terminal_callback)
Bilinear Transformation Diagram
Bilinear activations
The Bilinear layer applies the transformation $y = x_1^T A x_2 + b$. It is designed to model complex interactions between two different inputs (e.g., combining visual features with text embeddings) where simple concatenation is insufficient to capture cross-modal correlations.