‘‘‘
class torch.nn.Linear(in_features,out_features,bias = True )[来源]
参数:
in_features - 每个输入样本的大小
out_features - 每个输出样本的大小
bias - 如果设置为False,则图层不会学习附加偏差。默认值:True
‘‘‘
import torch
x = torch.randn(3, 2) # 输入的维度是(3,2)
m = torch.nn.Linear(2, 4) # 2,4是指维度
output = m(x)
print("x",x)
print(‘m.weight.shape:\n ‘, m.weight.shape,m.weight)
print(‘m.bias.shape:\n‘, m.bias.shape,m.bias)
print(‘output.shape:\n‘, output.shape,output)
‘‘‘
x tensor([[-0.4972, 1.2745],
[ 1.2993, -0.6580],
[-0.2165, 0.8603]])
m.weight.shape:torch.Size([4, 2]) Parameter containing:
tensor([[-0.5528, -0.1309],
[ 0.6907, 0.5723],
[-0.2242, 0.1904],
[ 0.1678, -0.6903]], requires_grad=True)
m.bias.shape:torch.Size([4]) Parameter containing:
tensor([-0.1663, -0.0111, 0.4852, 0.5688], requires_grad=True)
output.shape:torch.Size([3, 4])
tensor([[-0.0584, 0.3749, 0.8394, -0.3944],
[-0.7984, 0.5097, 0.0685, 1.2410],
[-0.1593, 0.3317, 0.6975, -0.0614]], grad_fn=<AddmmBackward>)
-0.4972*-0.5528+1.2745* -0.1309+-0.1663=-0.0584
‘‘‘
原文:https://www.cnblogs.com/hapyygril/p/11586860.html