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) # hidden layer
self.predict = torch.nn.Linear(n_hidden, n_output) # output layer
def forward(self, x):
x = F.relu(self.hidden(x)) # activation function for hidden layer
x = self.predict(x) # linear output
return x
# 实例化
net1 = Net(1, 10, 1)
# 输出网络结构
print(net1)
import torch
import torch.nn.functional as F
# 使用Sequential快速搭建
net2 = torch.nn.Sequential(
torch.nn.Linear(1, 10),
torch.nn.ReLU(),
torch.nn.Linear(10, 1)
)
print(net2) # net2 architecture
因篇幅问题不能全部显示,请点此查看更多更全内容