小编Kri*_*hna的帖子

torch.nn.sequential 与多个 torch.nn.linear 的组合

我正在尝试在 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)

python pytorch

11
推荐指数
1
解决办法
6151
查看次数

标签 统计

python ×1

pytorch ×1