简体   繁体   English

如何在 Pytorch 中将神经网络划分为子网络?

[英]How to partition a neural network into sub-networks in Pytorch?

I'd like to partition a neural network into two sub-networks using Pytorch.我想使用 Pytorch 将神经网络划分为两个子网络。 To make things concrete, consider this image:为了使事情具体化,请考虑以下图像:

在此处输入图像描述

In 1, I've a 3x4x1 neural network.在 1 中,我有一个 3x4x1 神经网络。 What I want is, for example during epoch 1, I'd only like to update the weights in the sub-network 1, ie, the weights that appear in the sub-network 2 must be frozen.我想要的是,例如在 epoch 1 期间,我只想更新子网络 1 中的权重,即必须冻结子网络 2 中出现的权重。 Then again, in epoch 2, I'd like to train the weights that appear in sub-network 2 while the rest should be frozen.再说一次,在 epoch 2 中,我想训练出现在子网络 2 中的权重,而 rest 应该被冻结。

How can I do that?我怎样才能做到这一点?

You can do this easily if your subnet is a subset of layers.如果您的子网是层的子集,您可以轻松地做到这一点。 That is, you do not need to freeze any partial layers.也就是说,您不需要冻结任何部分图层。 It is all or nothing.要么全有,要么全无。

For your example that would mean dividing the hidden layer into two different 2-node layers.对于您的示例,这意味着将隐藏层分为两个不同的 2 节点层。 Each would belong to exactly one of the subnetworks, which gets us back to all or nothing.每个都属于恰好一个子网,这使我们回到全部或全部。

With that done, you can toggle individual layers using requires_grad .完成后,您可以使用requires_grad切换各个层。 Setting this to False on the parameters will disable training and freeze the weights.在参数上将此设置为False将禁用训练并冻结权重。 To do this for an entire model, sub-model, or Module , you loop through the model.parameters() .要对整个 model、子模型或Module执行此操作,请遍历model.parameters()

For your example, with 3 inputs, 1 output, and a now split 2x2 hidden layer, it might look something like this:对于您的示例,有 3 个输入、1 个 output 和一个现在拆分的 2x2 隐藏层,它可能看起来像这样:

import torch.nn as nn
import torch.nn.functional as F

def set_grad(model, grad):
    for param in model.parameters():
        param.requires_grad = grad

class HalfFrozenModel(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.hid1 = torch.nn.Linear(3, 2)
        self.hid2 = torch.nn.Linear(3, 2)
        self.out = torch.nn.Linear(4, 1)

    def set_freeze(self, hid1=False, hid2=False):
        set_grad(self.hid1, not hid1)
        set_grad(self.hid2, not hid2)

    def forward(self, inp):
        hid1 = self.hid1(inp)
        hid2 = self.hid2(inp)
        hidden = torch.cat([hid1, hid2], 1)
        return self.out(F.relu(hidden))

Then you can train one half or the other like so:然后你可以像这样训练一半或另一半:

model = HalfFrozenModel()
model.set_freeze(hid1=True)
# Do some training.
model.set_freeze(hid2=True)
# Do some more training.
# ...

If you happen to use fastai , then there is a concept of layer groups that is also used for this.如果您碰巧使用fastai ,那么还有一个图层组的概念也用于此目的。 The fastai documentation goes into some detail about how that works. fastai 文档详细介绍了它的工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将两个子网络合并到张量流中的模型中 - How to merge two sub-networks into a model in tensorflow PyTorch 中的神经网络、损失和优化器是如何连接的? - How are neural networks, loss and optimizer connected in PyTorch? 如果您有多个神经网络,PyTorch 如何知道训练损失应传播回哪个神经网络? - How does PyTorch know to which neural network the training loss shall be propagated back if you have multiple neural networks? PyTorch 和神经网络:一层有多少参数? - PyTorch and Neural Networks: How many parameters in a layer? PyTorch和卷积神经网络 - PyTorch and Convolutional Neural Networks 有人可以解释一下这个 pytorch 神经网络代码吗? 这里有两种不同的神经网络还是一种? - Can someone explain this pytorch neural network code ? Are there two different neural networks here or one? 对于神经网络(火炬)中的每一层,应该有多少偏差? - For each layer in neural networks (pytorch), how many biases should be there? 如何在pytorch中逐步发展神经网络? - How to progressively grow a neural network in pytorch? 如何将 numpy 数组输入到 pytorch 中的神经网络? - How to input a numpy array to a neural network in pytorch? 如何在 PyTorch 中获取神经网络的编码器部分? - How to get the encoder part of a neural network in PyTorch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM