torch.nn.Linear() 理解
Posted gz153016
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了torch.nn.Linear() 理解相关的知识,希望对你有一定的参考价值。
# -- coding: utf-8 --
import torch
x = torch.randn(128, 20) # 输入的维度是(128,20)
m = torch.nn.Linear(20, 30) # 20,30是指维度
output = m(x)# (128, 20) (20, 30) -> (128, 30)
print('m.weight.shape:\\n ', m.weight.shape) # torch.Size([30, 20])
print('m.bias.shape:\\n', m.bias.shape) # torch.Size([30])
print('output.shape:\\n', output.shape) # torch.Size([128, 30])
# ans = torch.mm(input,torch.t(m.weight))+m.bias 等价于下面的
ans = torch.mm(x, m.weight.t()) + m.bias
# y = x.A(T) + b
print('ans.shape:\\n', ans.shape) # torch.Size([128, 30])
print(torch.equal(ans, output)) # # True
# 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
以上是关于torch.nn.Linear() 理解的主要内容,如果未能解决你的问题,请参考以下文章
pytorch 笔记:torch.nn.Linear() VS torch.nn.function.linear()