Skip to content
fn

bilinear

→Tensor
bilinear(input1: Tensor, input2: Tensor, weight: Tensor, bias: Tensor | None = None)

Bilinear transformation applied to two inputs.

Computes a quadratic interaction between every pair of features from input1 and input2. Common in fine-grained recognition (bilinear pooling), tensor-decomposition attention, and gated cross-modal fusion layers.

Parameters

input1Tensor
First input, shape (..., in1_features).
input2Tensor
Second input, shape (..., in2_features).
weightTensor
Weight tensor, shape (out_features, in1_features, in2_features).
biasTensor= None
Bias vector, shape (out_features,). Default None.

Returns

Tensor

Output tensor, shape (..., out_features).

Notes

For each output channel kk,

yk=x1⊤Wkx2+bk,y_k = x_1^\top W_k x_2 + b_k,

where WkW_k is the kk-th slice of the weight tensor. The parameter count grows multiplicatively in in1×in2\text{in1} \times \text{in2}, so bilinear layers are usually kept narrow. Reduces to two ordinary linear layers when Wk=ukvk⊤W_k = u_k v_k^\top is rank-one (the factorised bilinear trick).

Examples

>>> import lucid
>>> from lucid.nn.functional import bilinear
>>> x1 = lucid.randn(2, 3)
>>> x2 = lucid.randn(2, 4)
>>> w = lucid.randn(5, 3, 4)
>>> bilinear(x1, x2, w).shape
(2, 5)

Used by 2