keras冻结_keras ”冻结“ 网络层

“冻结”一个层指的是该层将不参加网络训练,即该层的权重永不会更新。在进行fine-tune时我们经常会需要这项操作。 在使用固定的embedding层处理文本输入时,也需要这个技术。

可以通过向层的构造函数传递trainable参数来指定一个层是不是可训练的,如:

frozen_layer = Dense(32,trainable=False)

此外,也可以通过将层对象的trainable属性设为True或False来为已经搭建好的模型设置要冻结的层。 在设置完后,需要运行compile来使设置生效,例如:

x = Input(shape=(32,))

layer = Dense(32)

layer.trainable = False

y = layer(x)

frozen_model = Model(x, y)

# in the model below, the weights of `layer` will not be updated during training

frozen_model.compile(optimizer='rmsprop', loss='mse')

layer.trainable = True

trainable_model = Model(x, y)

# with this model the weights of the layer will be updated during training

# (which will also affect the above model since it uses the same layer instance)

trainable_model.compile(optimizer='rmsprop', loss='mse')

frozen_model.fit(data, labels) # this does NOT update the weights of `layer`

trainable_model.fit(data, labels) # this updates the weights of `layer`

只是搬运这些,当做自己的笔记用,具体参考keras文档。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值