import torch.net
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)blog
# ans = torch.mm(input,torch.t(m.weight))+m.bias 等價於下面的
ans = torch.mm(x, m.weight.t()) + m.bias
print('ans.shape:\n', ans.shape)input
print(torch.equal(ans, output))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
m.weight.shape:
torch.Size([30, 20])
m.bias.shape:
torch.Size([30])
output.shape:
torch.Size([128, 30])
ans.shape:
torch.Size([128, 30])
True
1
2
3
4
5
6
7
8
9
爲何 m.weight.shape = (30,20)?class
答:由於線性變換的公式是:import
y=xAT+b y=xA^T+b
y=xA
T
+bim
先生成一個(30,20)的weight,實際運算中再轉置,這樣就能和x作矩陣乘法了
---------------------
做者:m0_37586991
來源:CSDN
原文:https://blog.csdn.net/m0_37586991/article/details/87861418
版權聲明:本文爲博主原創文章,轉載請附上博文連接!di