fn

bilinear

Tensor
bilinear(x1: Tensor, x2: Tensor, weight: Tensor, bias: Tensor | None = None)
source

Bilinear transformation applied to two inputs.

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

Parameters

x1Tensor
First input, shape (..., in1_features).
x2Tensor
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=x1Wkx2+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=ukvkW_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)