
torch.nn.Linear的作用是對輸入向量進行矩陣的乘積和加法。y=x(A)轉置+b。這點類似於全連接神經網絡的的隱藏層。in_feature代表輸入神經元的個數。out_feature代表輸出神經元的個數。bias為False不參與訓練。如果為True則參與訓練。
x = torch.randn(20) # 輸入的維度是(20)
m = torch.nn.Linear(20, 1) # 20,1是指輸入維度、輸出維度 神經網絡又20個輸入神經元,1個輸出神經元。
output = m(x)
print('m.weight.shape:\n ', m.weight.shape)
print('m.bias.shape:\n', m.bias.shape)
print('output.shape:\n', output.shape)
輸出結果:
m.weight.shape:
torch.Size([1, 20])//運算時,權值需要轉置。
m.bias.shape:
torch.Size([1])//只有一個神經元故bias只有一個。
output.shape:
torch.Size([1])//一個神經元只有一個輸出值。
