我正在尝试在 pytorch 中创建一个多层神经网络类。我想知道以下两段代码是否创建了相同的网络。
模型 1 与 nn.Linear
class TestModel(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim):
super(TestModel, self).__init__()
self.fc1 = nn.Linear(input_dim,hidden_dim)
self.fc2 = nn.Linear(hidden_dim,output_dim)
def forward(self, x):
x = nn.functional.relu(self.fc1(x))
x = nn.functional.softmax(self.fc2(x))
return x
Run Code Online (Sandbox Code Playgroud)
模型 2 与 nn.Sequential
class TestModel2(nn.Module):
def __init__(self, input, hidden, output):
super(TestModel2, self).__init__()
self.seq = nn.Sequential(
nn.Linear(input_dim,hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim,output_dim),
nn.Softmax()
)
def forward(self, x):
return self.seq(x)
Run Code Online (Sandbox Code Playgroud)