我有一个具有以下特征的多层LSTM自动编码器。

model = Sequential()
model.add(LSTM(250, dropout_U = 0.2, dropout_W = 0.2)) #L1
model.add(LSTM(150, dropout_U = 0.2, dropout_W = 0.2)) #L2
model.add(LSTM(100, dropout_U = 0.2, dropout_W = 0.2)) #L3
model.add(LSTM(150, dropout_U = 0.2, dropout_W = 0.2)) #L4
model.add(LSTM(250, dropout_U = 0.2, dropout_W = 0.2)) #L5
model.compile(optimizer='adam', loss='mse')


仅在测试阶段,我要在#L2中输入数据并获取#L4的输出,然后计算此表示层的输入和输出之间的差。

如何在此中间层中提供数据?当我为#L2层定义Keras输入错误时,我认为图形断开是合理的。

最佳答案

感谢@ mahsa-monavari和@frogatto的回答

from keras import backend as K

# with a Sequential model
get_3rd_layer_output = K.function([model.layers[0].input],
                                  [model.layers[3].output])
layer_output = get_3rd_layer_output([x])[0]

09-10 04:56
查看更多