import torch
x = torch.randn(128, 20) # 输入的维度是(128,20)
m = torch.nn.Linear(20, 30) # 20, 30是指维度
output = m(x)
print('m.weight.shape:\n', m.weight.shape)
print('m.bias.shape:\n', m.bias.shape)
print('output.shape:\n', output.shape)
m.weight.shape:
torch.Size([30, 20])
m.bias.shape:
torch.Size([30])
output.shape:
torch.Size([128, 30])
为什么m.weight.shape=(30, 20)?
因为线性变换的公式是:
y
=
x
A
T
+
b
y =xA^{T} + b
y=xAT+b
先生成一个(30, 20)的weight,实际运算中再转置,这样就能和x做矩阵乘法了。
Reference:
https://blog.csdn.net/m0_37586991/article/details/87861418