繁体   English   中英

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

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

我正在尝试应用 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 层。 这样,所有隐藏层都将使用所有数据进行训练,但每个 output 层将仅使用其相应类型的输入数据进行训练。

这是为了在迁移学习框架中取得良好的效果。

如何在 tensorflow 2.0 中实现这种“最后一层的更改”?

如果您使用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)

您仍然需要弄清楚反向传播部分,但我认为如果您使用多输出 model 并同时训练所有“最后一层”,这相当容易。

暂无
暂无

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

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