如何使用Keras冻结特定图层中的权重? [英] How to freeze weights in certain layer with Keras?

查看:559
本文介绍了如何使用Keras冻结特定图层中的权重?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Keras和mnist数据集冻结预测模型中某些图层的权重,但是它不起作用.代码类似于:

I am trying to freeze the weights of certain layer in a prediction model with Keras and mnist dataset, but it does not work. The code is like:

from keras.layers import Dense, Flatten
from keras.utils import to_categorical
from keras.models import Sequential, load_model
from keras.datasets import mnist
from keras.losses import categorical_crossentropy

import numpy as np

def load_data():
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')
    x_train /= 255
    x_test /= 255
    y_train = to_categorical(y_train, num_classes=10)
    y_test = to_categorical(y_test, num_classes=10)
    return x_train, y_train, x_test, y_test


def run():
    x_train, y_train, x_test, y_test = load_data()
    model = Sequential([Flatten(input_shape=(28, 28)),
                        Dense(300, name='dense1', activation='relu'),
                        Dense(100, name='dense2', activation='relu'),
                        Dense(10, name='dense3', activation='softmax')])
    model.trainable = True
    model.compile(optimizer='Adam',
                  metrics=['accuracy'],
                  loss=categorical_crossentropy)

    print(model.summary())
    model.fit(x_train, y_train, epochs=5, verbose=2)
    print(model.evaluate(x_test, y_test))
    return model

def freeze(model):
    x_train, y_train, x_test, y_test = load_data()

    name = 'dense1'

    weightsAndBias = model.get_layer(name=name).get_weights()

    # freeze the weights of this layer
    model.get_layer(name=name).trainable = False

    # record the weights before retrain
    weights_before = weightsAndBias[0]
    # retrain
    model.fit(x_train, y_train, verbose=2, epochs=1)
    weights_after = model.get_layer(name=name).get_weights()[0]

    if (weights_before == weights_after).all():
        print('the weights did not change!!!')
    else:
        print('the weights changed!!!!')

if __name__ == '__main__':
    model = run()
    freeze(model)

程序输出权重已更改!!!!". 我不明白为什么设置model.get_layer(name=name).trainable = False后名为'dense1'的图层的权重会发生变化.

The program outputs 'the weights changed!!!!'. I do not understand why the weights of the layer named 'dense1' changes after setting model.get_layer(name=name).trainable = False.

推荐答案

您可以使用:

model=Sequential()
layer=Dense(64,init='glorot_uniform',input_shape=(784,))
layer.trainable=False
model.add(layer)
layer2=Dense(784, activation='sigmoid',init='glorot_uniform')
layer2.trainable=True
model.add(layer2)
model.compile(loss='relu', optimizer=sgd,metrics = ['mae'])

这篇关于如何使用Keras冻结特定图层中的权重?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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