nn.Linear()
PyTorch的 nn.Linear() 是用于设置网络中的全连接层的,需要注意在二维图像处理的任务中,全连接层的输入与输出一般都设置为二维张量,形状通常为[batch_size, size],不同于卷积层要求输入输出是四维张量。其用法与形参说明如下:
torch.nn.Linear(in_features, out_features, bias=True, device=None, dtype=None)
即 $y=x A^{T}+b$
Parameters
-
- in_features – size of each input sample
- out_features – size of each output sample
- bias – If set to False, the layer will not learn an additive bias. Default: True
in_features 指的是输入的二维张量的大小,即输入的 [batch_size, size] 中的 size 。
out_features 指的是输出的二维张量的大小,即输出的二维张量的形状为 [ batch_size,output_size],当然,它也代表了该全连接层的神经元个数。
从输入输出的张量的 shape 角度来理解,相当于一个输入为 [batch_size, in_features] 的张量变换成了 [batch_size, out_features] 的输出张量。
Variables
-
- weight :weight.shape = (out_features,in_features)
- bias : bias.shape = (out_features)
Example
x = torch.arange(6).view(-1,2).type(torch.float32)
print(x)
m = nn.Linear(2,3)
print(m)
print(m.weight.shape)
print(m.bias.shape)
print(m(x))
tensor([[0., 1.],
[2., 3.],
[4., 5.]])
Linear(in_features=2, out_features=3, bias=True)
torch.Size([3, 2])
torch.Size([3])
tensor([[-0.0630, 0.8754, 1.0031],
[ 1.3898, 2.9152, 1.2959],
[ 2.8425, 4.9551, 1.5887]], grad_fn=<AddmmBackward0>)
Example
model = nn.Sequential(
nn.Linear(2,3)
)
print(model)
for param in model.state_dict():
print(param)
print(model.state_dict()[param])
Sequential(
(0): Linear(in_features=2, out_features=3, bias=True)
)
0.weight
tensor([[ 0.1144, 0.2086],
[-0.6700, -0.3607],
[-0.1835, 0.4866]])
0.bias
tensor([ 0.4158, -0.5482, 0.2358])