赞
踩
- import torch
- import torch.nn.functional as F
-
- """
- 此段代码主要诠释了如何快速搭建神经网络以及两种搭建方法的输出比较,
- 我们可以看到输出略有不同,但是两种方式搭建的神经网络的效果是一模一样的。
- """
-
- 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)
-
- # 另一种快速搭建神经网络的方法
- net2 = torch.nn.Sequential(
- torch.nn.Linear(1,10),
- torch.nn.ReLU(),
- torch.nn.Linear(10,1)
- )
-
- print(net1)
- print(net2)

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。