热门标签 | HotTags
当前位置:  开发笔记 > 人工智能 > 正文

在冻结的Keras模型中,辍学层是否仍处于活动状态(即,trainable=False)?

如何解决《在冻结的Keras模型中,辍学层是否仍处于活动状态(即,trainable=False)?》经验,为你挑选了1个好方法。

我有两个训练有素的模型(model_Amodel_B),并且两个模型都有辍学层。我已经冻结model_Amodel_B合并了它们,并获得了新的密集层model_AB(但我尚未删除model_A的和model_B的辍学层)。model_AB的权重将是不可训练的,除了增加的致密层。

现在的问题是:在辍学层model_Amodel_B活动状态(即滴神经元)时,我的训练model_AB



1> today..:

Short answer: The dropout layers will continue dropping neurons during training, even if you set their trainable property to False.

Long answer: There are two distinct notions in Keras:

Updating the weights and states of a layer: this is controlled using trainable property of that layer, i.e. if you set layer.trainable = False then the weights and internal states of the layer would not be updated.

Behavior of a layer in training and testing phases: as you know a layer, like dropout, may have a different behavior during training and testing phases. Learning phase in Keras is set using keras.backend.set_learning_phase(). For example, when you call model.fit(...) the learning phase is automatically set to 1 (i.e. training), whereas when you use model.predict(...) it will be automatically set to 0 (i.e. test). Further, note that learning phase of 1 (i.e. training) does not necessarily imply updating the weighs/states of a layer. You can run your model with a learning phase of 1 (i.e. training phase), but no weights will be updated; just layers will switch to their training behavior (see this answer for more information). Further, there is another way to set learning phase for each individual layer by passing training=True在张量上调用图层时的实参(有关更多信息,请参见此答案)。

因此,根据以上几点,当您trainable=False在退出层上设置并在训练模式下使用该层时(例如,通过调用model.fit(...),或手动将学习阶段设置为训练,如下例所示),神经元仍会被退出层丢弃。

这是一个可重现的示例,说明了这一点:

from keras import layers
from keras import models
from keras import backend as K
import numpy as np

inp = layers.Input(shape=(10,))
out = layers.Dropout(0.5)(inp)

model = models.Model(inp, out)
model.layers[-1].trainable = False  # set dropout layer as non-trainable
model.compile(optimizer='adam', loss='mse') # IMPORTANT: we must always compile model after changing `trainable` attribute

# create a custom backend function so that we can control the learning phase
func = K.function(model.inputs + [K.learning_phase()], model.outputs)

x = np.ones((1,10))
# learning phase = 1, i.e. training mode
print(func([x, 1]))
# the output will be:
[array([[2., 2., 2., 0., 0., 2., 2., 2., 0., 0.]], dtype=float32)]
# as you can see some of the neurons have been dropped

# now set learning phase = 0, i.e test mode
print(func([x, 0]))
# the output will be:
[array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]], dtype=float32)]
# unsurprisingly, no neurons have been dropped in test phase


推荐阅读
author-avatar
个信2502894627
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有