Keras在训练过程中得到最后一层的输出。

问题描述 投票:0回答:1

目标是在训练阶段恢复变分自动编码器最后一层的输出,作为另一个算法的训练数据.附上的是变分自动编码器的模型代码。

encoding_dim=58
input_dim=xtrain.shape[1]
inputArray=Input(shape=(input_dim,))
encoded= Dense(units=encoding_dim,activation="tanh")(inputArray) 
encoded= Dense(units=29,activation="tanh")(encoded)
encoded= Dense(units=15,activation="tanh")(encoded)
encoded= Dense(units=10,activation="tanh")(encoded)
encoded= Dense(units=3,activation="tanh")(encoded)
encoded= Dense(units=10,activation="tanh")(encoded)
decoded= Dense(units=15,activation="tanh")(encoded)
decoded= Dense(units=29,activation="tanh")(decoded)
decoded= Dense(units=encoding_dim,activation="tanh")(decoded)
decoded= Dense(units=input_dim,activation="sigmoid")(decoded) 
autoecoder=Model(inputArray,decoded)
autoecoder.summary()

autoecoder.compile(optimizer=RMSprop(),loss="mean_squared_error",metrics=["mae"])
#hyperparametrs :
batchsize=100
epoch=10
history = autoecoder.fit(xtrain_noise,xtrain,
              batch_size=batchsize,
              epochs=epoch,
              verbose=1,
              shuffle=True,
              validation_data=(xtest_noise,xtest),
              callbacks=[TensorBoard(log_dir="../logs/DenoiseautoencoderHoussem")])

我发现我可以通过以下方式来恢复所需的层数:

autoecoder.layers[10].output

但我如何将他在训练过程中的输出存储在一个列表中?谢谢。

编辑一下。我可以通过在xtrain数据上使用模型的预测方法来实现,但我认为这不是最好的方法。

keras deep-learning autoencoder
1个回答
0
投票

你可以使用之前训练过的模型的预测来训练一个新的模型,只需在所需输出的新层上堆叠,并在旧层上设置trainingable = False。这里有一个虚拟的例子

# after autoencoder fitting

for i,l in enumerate(autoecoder.layers):
    autoecoder.layers[i].trainable = False
    print(l.name, l.trainable)

output_autoecoder = autoecoder.layers[10].output
x_new = Dense(32, activation='relu')(output_autoecoder) # add a new layer for exemple

new_model = Model(autoecoder.input, x_new)
new_model.compile('adam', 'mse')
new_model.summary()

我将最后一个autoencoder层的输出作为新块的输入。我们可以合并所有编译一个新的模型,其中的输入与自动编码器相同,这样我们就可以将训练数据用于另一个算法,而不需要调用预测方法了


0
投票

要解决这个问题,只能用DL模型的.predict方法来解决,谢谢 @marrco

© www.soinside.com 2019 - 2024. All rights reserved.