如何在pytorch中改变模型的结构

时间:2017-05-03 17:21:19

标签: python neural-network deep-learning pytorch

我想开发堆叠式自动编码器或递归网络。这些是构建动态神经网络所必需的,它可以改变每次迭代的结构。

例如,我首先训练

class Net(nn.Module):
def __init__(self):
    super(Net, self).__init__()
    self.fc1 = nn.Linear(784,500)
    self.fc2 = nn.Linear(500,784)

def forward(self, x):
    x = F.relu(self.fc1(x))
    x = F.relu(self.fc2(x))
    return x

接下来,我想使用之前的fc1和fc2进行训练

class Net(nn.Module):
def __init__(self):
    super(Net, self).__init__()
    self.fc1 = nn.Linear(784,500)
    self.fc3 = nn.Linear(500,10)        
    self.fc4 = nn.Linear(10,500)
    self.fc2 = nn.Linear(500,784)

def forward(self, x):
    x = F.relu(self.fc1(x))
    x = F.relu(self.fc3(x))
    x = F.relu(self.fc4(x))
    x = F.relu(self.fc2(x))
    return x

如何在单一模型中构建这些网络?

1 个答案:

答案 0 :(得分:0)

您可以简单地为您的前向功能添加一个参数,该参数可以在您想要的两种可能性之间切换:

class Net(nn.Module):
def __init__(self):
    super(Net, self).__init__()
    self.fc1 = nn.Linear(784,500)
    self.fc3 = nn.Linear(500,10)        
    self.fc4 = nn.Linear(10,500)
    self.fc2 = nn.Linear(500,784)

def forward(self, x, n_layers=2):
    if 2 == n_layers:
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        return x
    elif 4 == n_layers:
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc3(x))
        x = F.relu(self.fc4(x))
        x = F.relu(self.fc2(x))
        return x
    else:
        raise Exception("Not implemented")