【问题标题】:Giving output of one neural network as an input to another in pytorch在 pytorch 中将一个神经网络的输出作为另一个神经网络的输入
【发布时间】:2021-11-10 05:29:45
【问题描述】:

我有一个预训练的卷积神经网络,它产生和输出形状 (X,164),其中 X 是测试示例的数量。所以输出层有164个节点。我想获取这个输出并给这两个另一个网络,它只是一个完全连接的神经网络,其中第一层有 64 个节点,输出层有一个具有 sigmoid 函数的节点。我怎样才能做到这一点?我的第一个网络如下所示:

class LambdaBase(nn.Sequential):
    def __init__(self, fn, *args):
        super(LambdaBase, self).__init__(*args)
        self.lambda_func = fn

    def forward_prepare(self, input):
        output = []
        for module in self._modules.values():
            output.append(module(input))
        return output if output else input

class Lambda(LambdaBase):
    def forward(self, input):
        return self.lambda_func(self.forward_prepare(input))

class LambdaMap(LambdaBase):
    def forward(self, input):
        return list(map(self.lambda_func,self.forward_prepare(input)))

class LambdaReduce(LambdaBase):
    def forward(self, input):
        return reduce(self.lambda_func,self.forward_prepare(input))

def get_model(load_weights = True):
    pretrained_model_reloaded_th = nn.Sequential( # Sequential,
        nn.Conv2d(4,300,(19, 1)),
        nn.BatchNorm2d(300),
        nn.ReLU(),
        nn.MaxPool2d((3, 1),(3, 1)),
        nn.Conv2d(300,200,(11, 1)),
        nn.BatchNorm2d(200),
        nn.ReLU(),
        nn.MaxPool2d((4, 1),(4, 1)),
        nn.Conv2d(200,200,(7, 1)),
        nn.BatchNorm2d(200),
        nn.ReLU(),
        nn.MaxPool2d((4, 1),(4, 1)),
        Lambda(lambda x: x.view(x.size(0),-1)), # Reshape,
        nn.Sequential(Lambda(lambda x: x.view(1,-1) if 1==len(x.size()) else x ),nn.Linear(2000,1000)), # Linear,
        nn.BatchNorm1d(1000,1e-05,0.1,True),#BatchNorm1d,
        nn.ReLU(),
        nn.Dropout(0.3),
        nn.Sequential(Lambda(lambda x: x.view(1,-1) if 1==len(x.size()) else x ),nn.Linear(1000,1000)), # Linear,
        nn.BatchNorm1d(1000,1e-05,0.1,True),#BatchNorm1d,
        nn.ReLU(),
        nn.Dropout(0.3),
        nn.Sequential(Lambda(lambda x: x.view(1,-1) if 1==len(x.size()) else x ),nn.Linear(1000,164)), # Linear,
        nn.Sigmoid(),
    )
    if load_weights:
        sd = torch.load('pretrained_model.pth')
        pretrained_model_reloaded_th.load_state_dict(sd)
    return  pretrained_model_reloaded_th

model = get_model(load_weights = True)

如果我想在我的测试集上获得这个模型的输出,我可以简单地做:


output = model(X.float())

这会产生形状 (X,164) 的最终输出。现在我想把这个输出交给上面提到的另一个神经网络。我现在如何将这两个网络结合起来,如何一起优化这些网络?我们将不胜感激。

编辑: 我的第二个模型是:


# define second model architecture
next_model = nn.Sequential(
    nn.Linear(164, 64),
    nn.ReLU(),
    nn.Linear(64, 1),
    nn.Sigmoid()
)

# print model architecture
print(next_model)

我的分类器被训练为:


for epoch in range(2):  # loop over the dataset multiple times

    running_loss = 0.0
    for i, data in enumerate(trainloader, 0):
        # get the inputs; data is a list of [inputs, labels]
        inputs, labels = data

        # zero the parameter gradients
        optimizer.zero_grad()

        # forward + backward + optimize
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        # print statistics
        running_loss += loss.item()
        if i % 2000 == 1999:    # print every 2000 mini-batches
            print('[%d, %5d] loss: %.3f' %
                  (epoch + 1, i + 1, running_loss / 2000))
            running_loss = 0.0

print('Finished Training')

【问题讨论】:

  • 其他模型的定义在哪里?
  • @Ivan 我刚刚也添加了我的第二个模型。

标签: python deep-learning pytorch conv-neural-network


【解决方案1】:

如果两个模型不需要在第一个模型输出上进行任何调整,您可以简单地使用nn.Sequential

>>> network = nn.Sequential(model, next_model)

并以与 model 相同的方式使用它:

>>> output = network(X.float())

这将对应于next_model(model(X.float()))

【讨论】:

  • 谢谢伊万。但是我如何训练我的分类器。我添加了一个用于训练单个分类器的代码。
  • model 相同,即network.parameters() 包含来自modelnext_model 的参数。因此,您只需使用这些实例化优化器,并在您的 criterion 上使用 network 的输出,然后对其进行反向传播。
  • 谢谢。现在这对我来说很有意义。
猜你喜欢
  • 2017-03-16
  • 2012-10-05
  • 2015-02-23
  • 1970-01-01
  • 2020-03-15
  • 1970-01-01
  • 2017-06-13
相关资源
最近更新 更多