【Pytorch基础】torch.nn.Linear()函数

  • Post author:
  • Post category:其他


PyTorch的nn.Linear()是用于设置网络中的全连接层具体的介绍如下:

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)
m.weight.shape:
  torch.Size([30, 20])
  
print('m.bias.shape:\n', m.bias.shape)
m.bias.shape:
 torch.Size([30])
 
print('output.shape:\n', output.shape)
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   
print('ans.shape:\n', ans.shape)
ans.shape:
 torch.Size([128, 30])
 
print(torch.equal(ans, output))
True

为什么



m

.

w

e

i

g

h

t

.

s

h

a

p

e

=

(

30

,

20

)

 

?

\rm{} ~m.weight.shape = (30,20)~?











m


.


w


e


i


g


h


t


.


s


h


a


p


e




=




(


3


0


,




2


0


)




?







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





y

=

x

A

T

+

b

y = x A ^T + b






y




=








x



A










T











+








b





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



参考文献


[1]torch.nn.Linear() 理解



[2]PyTorch的nn.Linear()详解



[3]https://pytorch.org/docs/master/nn.html#linear-layers