【Pytorch入門到精通】nn.Linear

<code>torch.nn.Linear(in_features,out_features,bias = True )/<code>

模塊用途:執行線性變換 y = A x+ b

參數說明:

in_features :每個輸入sample特徵維度

out_features :每個輸出樣本的維度

bias : 如果設置為False,則該層不學習偏差參數。默認值:True


應用Linear實現MLP:

<code>import torch
from torch import nn


class MLP(nn.Module):
def __init__(self, **kwargs):
super(MLP, self).__init__(**kwargs)
self.hidden = nn.Linear(784, 256)
self.act = nn.ReLU()
self.output = nn.Linear(256, 10)

def forward(self, x):
a = self.act(self.hidden(x))
return self.output(a)


X = torch.rand(2, 784)
net = MLP()
output = net(X)
print(net)
print(output)
print(output.size())/<code>

執行輸出:

<code>(base) PS D:\\workspace\\Git\\python> conda activate base
(base) PS D:\\workspace\\Git\\python> ${env:DEBUGPY_LAUNCHER_PORT}='14659'; & 'C:\\ProgramData\\Anaconda3\\python.exe' 'c:\\Users\\renhao\\.vscode\\extensions\\ms-python.python-2020.3.71659\\pythonFiles\\lib\\python\\debugpy\\wheels\\debugpy\\launcher' 'd:\\workspace\\Git\\python\\Pytorch\\torchModuleLearn.py'
MLP(
(hidden): Linear(in_features=784, out_features=256, bias=True)
(act): ReLU()
)
tensor([[-0.0893, 0.0537, 0.1864, -0.0603, 0.1481, 0.2578, 0.1449, 0.0449,
-0.0525, -0.0684],
[-0.1521, -0.0388, 0.0725, 0.0723, 0.0686, 0.1950, -0.0641, 0.0938,
-0.0604, 0.0821]], grad_fn=<addmmbackward>)
(base) PS D:\\workspace\\Git\\python> ${env:DEBUGPY_LAUNCHER_PORT}='3318'; & 'C:\\ProgramData\\Anaconda3\\python.exe' 'c:\\Users\\renhao\\.vscode\\extensions\\ms-python.python-2020.3.71659\\pythonFiles\\lib\\python\\debugpy\\wheels\\debugpy\\launcher' 'd:\\workspace\\Git\\python\\Pytorch\\torchModuleLearn.py'
MLP(
(hidden): Linear(in_features=784, out_features=256, bias=True)
(act): ReLU()
(output): Linear(in_features=256, out_features=10, bias=True)
)
tensor([[-0.1295, 0.0865, -0.0277, 0.1044, 0.2579, 0.1228, -0.1272, 0.0310,
-0.2329, -0.0463],
[-0.1160, 0.0768, 0.1460, 0.2687, 0.0801, -0.0196, -0.0890, 0.0590,
-0.1971, 0.0714]], grad_fn=<addmmbackward>)
torch.Size([2, 10])/<addmmbackward>/<addmmbackward>/<code>


分享到:


相關文章: