model.trainable =模型下的冻结权重不正确? [英] shouldn't model.trainable=False freeze weights under the model?

查看:446
本文介绍了model.trainable =模型下的冻结权重不正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图冻结免费训练的VGG16的图层(在下面为"conv_base"),并在其之上添加新图层以进行特征提取. 我希望从'conv_base'的模型拟合之前(ret1)/之后(ret2)获得相同的预测结果,但事实并非如此. 这是检查体重冻结的错误方法吗?

I am trying to freeze the free trained VGG16's layers ('conv_base' below) and add new layers on top of them for feature extracting. I expect to get same prediction results from 'conv_base' before(ret1) / after(ret2) fit of model but it is not. Is this wrong way to check weight freezing?

conv_base  = applications.VGG16(weights='imagenet', include_top=False, input_shape=[150, 150, 3]) 
conv_base.trainable = False

模型拟合前的结果

ret1 = conv_base.predict(np.ones([1, 150, 150, 3]))

在VGG16的顶部添加层并编译模型

model = models.Sequential()
model .add(conv_base)
model .add(layers.Flatten())
model .add(layers.Dense(10, activation='relu'))
model .add(layers.Dense(1, activation='sigmoid'))
m.compile('rmsprop', 'binary_crossentropy', ['accuracy'])

拟合模型

m.fit_generator(train_generator, 100, validation_data=validation_generator, validation_steps=50)

模型拟合后的结果

ret2 = conv_base.predict(np.ones([1, 150, 150, 3]))

希望这是对的,但事实并非如此.

np.equal(ret1, ret2)

推荐答案

这是一个有趣的案例.为什么发生这种情况是由以下原因引起的:

This is an interesting case. Why something like this happen is caused by the following thing:

您无法在编译后冻结整个模型,如果未编译,则不会冻结

如果设置标志model.trainable=False,则在编译keras时会将所有图层设置为不可训练.如果在编译后设置此标志-则它根本不会影响您的模型.相同-如果在编译之前设置此标志,然后将模型的一部分重用于编译另一个模型-则不会影响您的重用层.因此,model.trainable=False仅在按以下顺序应用时有效:

If you set a flag model.trainable=False then while compiling keras sets all layers to be not trainable. If you set this flag after compilation - then it will not affect your model at all. The same - if you set this flag before compiling and then you'll reuse a part of a model for compiling another one - it will not affect your reused layers. So model.trainable=False works only when you'll apply it in a following order:

# model definition
model.trainable = False
model.compile()

在任何其他情况下,它都无法按预期工作.

In any other scenario it wouldn't work as expected.

这篇关于model.trainable =模型下的冻结权重不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆