方法一:定义神经网络类,然后实例化
 
代码:
 
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)
print(net1)
 
运行结果:
 

 
方法二:使用Sequential快速搭建
 
代码:
 
import torch
import torch.nn.functional as F
net2 = torch.nn.Sequential(
    torch.nn.Linear(1, 10),
    torch.nn.ReLU(),
    torch.nn.Linear(10, 1)
)
print(net2)     
 
运行结果:
 
