class
Bilinear
extends
ModuleBilinear(in1_features: int, in2_features: int, out_features: int, bias: bool = True, device: DeviceLike = None, dtype: DTypeLike = None)Apply a bilinear transformation to a pair of input tensors.
For each output unit the layer computes
where is the weight tensor and is the optional bias.
Parameters
in1_featuresintDimensionality of the first input ().
in2_featuresintDimensionality of the second input ().
out_featuresintNumber of output units ().
biasbool= TrueIf
True (default) add a learnable bias term .deviceDeviceLike= NoneDevice for initial parameters.
dtypeDTypeLike= NoneDtype for initial parameters.
Attributes
weightParameterWeight tensor of shape
(out_features, in1_features, in2_features).
Each slice weight[k] is a matrix that mixes the two input spaces
for the -th output unit. Initialized with uniform sampling
over .biasParameter or NoneBias vector of shape
(out_features,).
None when bias=False.Notes
- Input 1 (
x1): . - Input 2 (
x2): . - Output: .
Bilinear captures multiplicative interactions between two feature
vectors that a plain Linear layer cannot express. Typical use cases
include:
- Attention scoring — compute compatibility between query and key vectors using a learned interaction matrix instead of the dot product.
- Relation networks — model pair-wise relationships in graph neural networks or visual question answering.
- Similarity scoring — learn an asymmetric distance metric between two embeddings (e.g. in contrastive or metric-learning settings).
Examples
Scoring query–key compatibility in an attention mechanism:
>>> import lucid
>>> import lucid.nn as nn
>>> attn_score = nn.Bilinear(64, 64, 1)
>>> q = lucid.randn(8, 64) # queries
>>> k = lucid.randn(8, 64) # keys
>>> scores = attn_score(q, k)
>>> scores.shape
(8, 1)
Multi-output relation layer with different input spaces:
>>> rel = nn.Bilinear(128, 256, 32)
>>> x1 = lucid.randn(4, 128)
>>> x2 = lucid.randn(4, 256)
>>> rel(x1, x2).shape
(4, 32)Methods (4)
dunder
__init__
→None__init__(in1_features: int, in2_features: int, out_features: int, bias: bool = True, device: DeviceLike = None, dtype: DTypeLike = None)Initialise the Bilinear module. See the class docstring for parameter semantics.
fn
reset_parameters
→Nonereset_parameters()Initialize with Kaiming uniform.
fn
forward
→Tensorforward(x1: Tensor, x2: Tensor)Apply the linear transformation to the input tensor.
Parameters
inputTensorInput tensor of shape .
Returns
TensorOutput tensor of shape .
fn
extra_repr
→strextra_repr()Return a string representation of the layer's configuration.