Pytorch之nn.linear层
1.二维:
注意:改变的是最后一维列数
X = torch.rand(2, 4)
net1 = nn.Sequential(nn.Linear(4, 3))
d = net1(X)
print(d)
输出:
tensor([[-0.4811, -0.2753, 0.4857],
[-0.2339, -0.1274, 0.4677]], grad_fn=<AddmmBackward>)
1.三维:
注意:改变的是最后一维列数
X = torch.rand(3, 2, 4)
输出:
tensor([[[0.1390, 0.2656, 0.5775, 0.0419],
[0.1601, 0.2080, 0.6637, 0.9871]],
[[0.3458, 0.6473, 0.2802, 0.8923],
[0.1926, 0.1190, 0.8961, 0.2184]],
[[0.5982, 0.2578, 0.3267, 0.9850],
[0.8681, 0.9482, 0.6696, 0.1949]]])
net1 = nn.Sequential(nn.Linear(4, 3))
d= net1(X)
d
输出:
tensor([[[ 0.0062, 0.3436, 0.0684],
[-0.0757, 0.4480, 0.4901]],
[[-0.0655, 0.6235, 0.5633],
[-0.0134, 0.3415, 0.0643]],
[[ 0.0011, 0.5260, 0.6617],
[-0.0137, 0.7969, 0.1548]]], grad_fn=<AddBackward0>)