莫烦pytorch 快速搭建法
作者:互联网
用简单方式搭建神经网络
原始搭建法:
class Net(torch.nn.Module):
def __init__(self, n_feature, n_hidden, n_output):
super(Net, self).__init__()
self.hidden = torch.nn.Linear(n_feature, n_hidden)
self.predict = torch.nn.Linear(n_hidden, n_output)
def forward(self, x):
x = F.relu(self.hidden(x))
x = self.predict(x)
return x
net1 = Net(1, 10, 1) # 这是我们用这种方式搭建的 net1
快速搭建法:
net2 = torch.nn.Sequential(
torch.nn.Linear(1, 10),
torch.nn.ReLU(),
torch.nn.Linear(10, 1)
)
结构对比:
print(net1)
"""
Net (
(hidden): Linear (1 -> 10)
(predict): Linear (10 -> 1)
)
"""
print(net2)
"""
Sequential (
(0): Linear (1 -> 10)
(1): ReLU ()
(2): Linear (10 -> 1)
)
"""
相比net2,net1的好处就是,你可以根据你的个人需要更加个性化你自己的正向传播过程,比如(RNN)。不过如果你不需要七七八八的过程,net2更适合你。
标签:10,莫烦,nn,self,torch,pytorch,hidden,搭建,Linear 来源: https://blog.csdn.net/Kstheme/article/details/99541662