PyTorch中torch.nn.Linear()的实现

import torch
import torch.nn as nn

# 输入
input = torch.randn(2, 3)

in_features = input.shape[-1]
out_features = 1

# 初始化一个神经网络
L = nn.Linear(in_features=in_features, out_features=out_features)

# 输出
output1 = L(input)

# 手动构造Linear
output2 = input @ torch.transpose(L.weight, 0, 1) + L.bias

print('output1=', output1)
print('output2=', output2)
pass

# 结果
# output1= tensor([[-0.3088],
#         [ 0.2639]], grad_fn=)
# output2= tensor([[-0.3088],
#         [ 0.2639]], grad_fn=)

结果

output1=
 tensor([[0.1274],
        [1.3677]], grad_fn=)
output2=
 tensor([[0.1274],
        [1.3677]], grad_fn=)

你可能感兴趣的:(ML,pytorch,人工智能,python)