fn
bilinear
→Tensorbilinear(x1: Tensor, x2: Tensor, weight: Tensor, bias: Tensor | None = None)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
x1TensorFirst input, shape
(..., in1_features).x2TensorSecond input, shape
(..., in2_features).weightTensorWeight tensor, shape
(out_features, in1_features, in2_features).biasTensor= NoneBias vector, shape
(out_features,). Default None.Returns
TensorOutput tensor, shape (..., out_features).
Notes
For each output channel ,
where is the -th slice of the weight tensor. The parameter count grows multiplicatively in , so bilinear layers are usually kept narrow. Reduces to two ordinary linear layers when 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)