如何在基于Keras的LSTM模型中得到每一层的一层权重矩阵?

时间:2017-09-28 16:13:41

标签: tensorflow deep-learning keras lstm

我有一个基于Keras的简单LSTM模型。

X_train, X_test, Y_train, Y_test = train_test_split(input, labels, test_size=0.2, random_state=i*10)

X_train = X_train.reshape(80,112,12)
X_test = X_test.reshape(20,112,12)

y_train = np.zeros((80,112),dtype='int')
y_test = np.zeros((20,112),dtype='int')

y_train = np.repeat(Y_train,112, axis=1)
y_test = np.repeat(Y_test,112, axis=1)
np.random.seed(1)

# create the model
model = Sequential()
batch_size = 20

model.add(BatchNormalization(input_shape=(112,12), mode = 0, axis = 2))#4
model.add(LSTM(100, return_sequences=False, input_shape=(112,12))) #7 

model.add(Dense(112, activation='hard_sigmoid'))#9
model.compile(loss='binary_crossentropy', optimizer='RMSprop', metrics=['binary_accuracy'])#9

model.fit(X_train, y_train, nb_epoch=30)#9

# Final evaluation of the model
scores = model.evaluate(X_test, y_test, batch_size = batch_size, verbose=0)

我知道如何通过model.get_weights()获取重量列表,但这是模型完全训练后的值。我希望在每个时代得到权重矩阵(例如,我的LSTM中的最后一层),而不仅仅是它的最终值。换句话说,我有30个时代,我需要得到30个权重矩阵值。

真的,谢谢你,我没有在keras的维基上找到解决方案。

1 个答案:

答案 0 :(得分:5)

您可以为它编写自定义回调:

from keras.callbacks import Callback

class CollectWeightCallback(Callback):
    def __init__(self, layer_index):
        super(CollectWeightCallback, self).__init__()
        self.layer_index = layer_index
        self.weights = []

    def on_epoch_end(self, epoch, logs=None):
        layer = self.model.layers[self.layer_index]
        self.weights.append(layer.get_weights())

回调的属性self.model是对正在训练的模型的引用。在培训开始时通过Callback.set_model()设置。

要获取每个纪元的最后一层的权重,请将其用于:

cbk = CollectWeightCallback(layer_index=-1)
model.fit(X_train, y_train, nb_epoch=30, callbacks=[cbk])

然后将权重矩阵收集到cbk.weights