Keras,在每个纪元获取图层的输出

时间:2018-10-24 05:23:39

标签: python keras

我做了什么?

我实现了keras模型,如下所示:

train_X, test_X, train_Y, test_Y = train_test_split(X, Y, test_size=0.2, random_state=np.random.seed(7), shuffle=True)

train_X = np.reshape(train_X, (train_X.shape[0], 1, train_X.shape[1]))
test_X = np.reshape(test_X, (test_X.shape[0], 1, test_X.shape[1]))

model = Sequential()
model.add(LSTM(100, return_sequences=False, input_shape=(train_X.shape[1], train_X.shape[2])))
model.add(Dense(train_Y.shape[1], activation='softmax'))
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])

model.fit(train_X, train_Y, validation_split=.20,
                        epochs=1000, batch_size=50)

我想要什么?

我想给support vector machine(SVM)的倒数第二层(LSTM)的输出,也要训练到epoch的任何svm(即1000)中。

但是我不知道该怎么做?

有什么主意吗?

已更新

我在ModelCheckpoint中使用,如下所示:

model = Sequential()
model.add(LSTM(100, return_sequences=False, input_shape=(train_X.shape[1], train_X.shape[2])))
model.add(Dense(train_Y.shape[1], activation='softmax'))
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])

# checkpoint
filepath="weights-{epoch:02d}-{val_acc:.2f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]

model.fit(train_X, train_Y, validation_split=.20,
                    epochs=1000, batch_size=50, callbacks=callbacks_list, verbose=0)

输出:

Epoch 00991: val_acc did not improve
Epoch 00992: val_acc improved from 0.93465 to 0.93900, saving model to weights-992-0.94.hdf5
Epoch 00993: val_acc did not improve
Epoch 00994: val_acc did not improve
Epoch 00995: val_acc did not improve
Epoch 00996: val_acc did not improve
Epoch 00997: val_acc did not improve
Epoch 00998: val_acc improved from 0.93900 to 0.94543, saving model to weights-998-0.94.hdf5
Epoch 00999: val_acc did not improve

问题:

如@IonicSolutions所说,如何加载所有这些模型以获取每个时期的LSTM层的输出?

2 个答案:

答案 0 :(得分:2)

哪种情况下最有效取决于您设置和训练SVM的精确度,但是使用回调至少有两个选择:

您可以使用ModelCheckpoint callback保存每个时期正在训练的模型的副本,然后再加载所有这些模型以获得LSTM层的输出。

您还可以通过实现Callback base class来创建自己的回调。在回调中,可以访问模型,您可以使用on_epoch_end在每个时期的末尾提取LSTM输出。

编辑:要方便地访问倒数第二层,可以执行以下操作:

# Create the model with the functional API
inp = Input((train_X.shape[1], train_X.shape[2],))
lstm = LSTM(100, return_sequences=False)(inp)
dense = Dense(train_Y.shape[1], activation='softmax')(lstm)

# Create the full model
model = Model(inputs=inp, outputs=dense)

# Create the model for access to the LSTM layer
access = Model(inputs=inp, outputs=lstm)

然后,您可以在实例化时将access传递给回调。这里要注意的关键是modelaccess共享相同的LSTM层,训练model时,它们的权重将改变。

答案 1 :(得分:0)

为了在每个时期获得预测输出,我们可以这样做:

import tensorflow as tf
import keras

# define your custom callback for prediction
class PredictionCallback(tf.keras.callbacks.Callback):    
  def on_epoch_end(self, epoch, logs={}):
    y_pred = self.model.predict(self.validation_data[0])
    print('prediction: {} at epoch: {}'.format(y_pred, epoch))

# ...

# register the callback before training starts
model.fit(X_train, y_train, batch_size=32, epochs=25, 
          validation_data=(X_valid, y_valid), 
          callbacks=[PredictionCallback()])
相关问题