有
in_features * out_features
linear.weight
和
out_features
在中学习的参数
linear.bias
. 你可以想到
nn.Linear
担任
-
将张量重塑为某个值
(N', in_features)
哪里
N'
N
以及使用
*
input_2d = input.reshape(-1, in_features)
-
应用标准矩阵乘法
output_2d = linear.weight @ input_2d
-
加上偏见
output_2d += linear.bias.reshape(1, in_features)
(请注意,我们在所有媒体上进行了广播
尺寸)
-
input
,除了最后一个:
output = output_2d.reshape(*input.shape[:-1], out_features)
-
return output
因此,主导维度
被视为与
*
N
显式,让您知道输入必须是