nn.Linear
回归nn.Linear
,线性变换,数学计算公式是:
y
=
x
A
T
+
b
y = xA^{T} + b
y=xAT+b。神经网络的全连接层可以调用实现
nn.Bilinear双线性变换层
也归属Linear Layers。torch官网的介绍如下:
数学计算公式为
y
=
x
1
T
A
x
2
+
b
y = x_{1}^{T}Ax_{2} +b
y=x1TAx2+b。双线性变换在一些深度学习模型中被用来捕获输入之间的双线性关系,尤其在某些图像处理任务和多模态学习任务中非常有用。
示例
input1 = torch.tensor([[1.0, 2.0, 3.0, 4.0, 5.0]])
# input2 = torch.tensor([[1.0, 2.0, 3.0, 4.0]])
input2 = torch.tensor([[5.0, 4.0, 3.0, 2.0]])
# 定义双线性层
bilinear = torch.nn.Bilinear(5, 4, 1)
# 初始化权重和偏置
bilinear.weight.data = torch.ones(1, 5, 4) # 注意到权重是三维的
bilinear.bias.data = torch.zeros(1)
# 前向传播
output = bilinear(input1, input2)
print(f'输出分数为:{output}')