简体   繁体   English

迁移学习 - 如何仅更改 TensorFlow 中的 output 层?

[英]Transfer Learning - How can I change only the output layer in TensorFlow?

I am trying to apply one idea proposed by Rusu et al.我正在尝试应用 Rusu 等人提出的一个想法。 in https://arxiv.org/pdf/1511.06295.pdf , which consists in training a NN changing the output layer according to the class of the input, ie, provided that we know the id of the input, we would pick the corresponding output layer. in https://arxiv.org/pdf/1511.06295.pdf , which consists in training a NN changing the output layer according to the class of the input, ie, provided that we know the id of the input, we would pick the corresponding output 层。 This way, all the hidden layers would be trained with all the data, but each output layer would only be trained with its corresponding type of input data.这样,所有隐藏层都将使用所有数据进行训练,但每个 output 层将仅使用其相应类型的输入数据进行训练。

This is meant to achieve good results in a transfer learning framework.这是为了在迁移学习框架中取得良好的效果。

How can I implement this "change of the last layer" in tensorflow 2.0?如何在 tensorflow 2.0 中实现这种“最后一层的更改”?

If you use model subclassing , you can actually define you forward pass.如果您使用model 子类化,您实际上可以定义您的前向传递。

class MyModel(tf.keras.Model):

    def __init__(self):
        super(Model, self).__init__()
        self.block_1 = BlockA()
        self.block_2 = BlockB()
        self.global_pool = layers.GlobalAveragePooling2D()
        self.classifier = Dense(num_classes)

    def call(self, inputs):
        if condition:
            x = self.block_1(inputs)
        else:
            x = self.block_2(inputs)
        x = self.global_pool(x)
        return self.classifier(x)

You'll still have the backprop part to figure out, but I think it's fairly easy if you use a multioutput model and train all your "last layers" at the same time.您仍然需要弄清楚反向传播部分,但我认为如果您使用多输出 model 并同时训练所有“最后一层”,这相当容易。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM