2

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

例如,我首先训练

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

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

4

1 回答 1

1

你可以简单地为你的 forward 函数添加一个参数,它可以在你想要的两种可能性之间切换:

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")
于 2018-02-27T12:30:02.560 回答