美文网首页
2021-10-12-torch.nn.linear

2021-10-12-torch.nn.linear

作者: 野山羊骑士 | 来源:发表于2021-10-12 15:07 被阅读0次

参考:
https://zhuanlan.zhihu.com/p/152198144

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)

# 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)

print(torch.equal(ans, output))


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

为什么 m.weight.shape = (30,20)?

答:因为线性变换的公式是:

image

先生成一个(30,20)的weight,实际运算中再转置,这样就能和x做矩阵乘法了

相关文章

网友评论

      本文标题:2021-10-12-torch.nn.linear

      本文链接:https://www.haomeiwen.com/subject/dkndoltx.html