zl程序教程

您现在的位置是:首页 >  后端

当前栏目

用pytorch的两种方法创建神经网络

2023-09-14 09:05:37 时间

import torch
import torch.nn.functional as F

replace following class code with an easy sequential network

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)

easy and fast way to build your network

net2 = torch.nn.Sequential(
torch.nn.Linear(1, 10),
torch.nn.ReLU(),
torch.nn.Linear(10, 1)
)

print(net1) # net1 arch