我已经定义并训练了一个自动编码器模型,如下所示:
input_enc = Input(batch_shape=(batch_size, seq_len, n_features), name='encoder_input')
first_enc = LSTM(32, activation='tanh', dropout=0.1, recurrent_dropout=0.1, return_sequences=True, stateful=True, name='encoder_first_layer')(input_enc)
if output_dropout:
first_enc = Dropout(0.2)(first_enc)
encoded, hidden_state, cell_state = LSTM(14, activation='tanh', dropout=0.1, recurrent_dropout=0.1, return_sequences=False, return_state=True , stateful=True, name='encoded')(first_enc)
decoder_input = RepeatVector(28, name='repeatVector')(encoded)
first_dec = LSTM(32, return_sequences=True, name='decoder_first_layer')(decoder_input)
out_decoder = LSTM(1, return_sequences=True, name='decoder_output_layer')(first_dec)
autoencoder_model = Model(input_enc, out_decoder)
encoder_model = Model(inputs=input_enc, outputs=[encoded, hidden_state, cell_state])
自动编码器模型的输入形状(n_samples, seq_len=28, n_features=1)
且batch_size = 138
尝试自动编码器后,我加载编码器部分并将其用作另一个模型的输入。
input_layer = Input(batch_shape=(batch_size, seq_len, n_features), name='ae_prediction_input')
encoder_first_layer = encoder.layers[1](input_layer)
encoded_layer, h_state, c_state = encoder.layers[2](encoder_first_layer)
first_layer = Dense(24, input_dim=28, activation=activation, name="first_dense_layer")(h_state)
if dropout:
first_layer = Dropout(0.2, name="first_dropout_layer")(first_layer)
second_layer = Dense(12, activation=activation, name="second_dense_layer")(first_layer)
if dropout:
second_layer = Dropout(0.2, name="snd_dropout_layer")(second_layer)
out = Dense(1, name='output_layer')(second_layer)
new_model = Model(input_layer, out)
new.compile(loss='mean_squared_error', optimizer=rmsprop_optimizer)
history = new_model.fit(train_data, train_y, epochs=5, callbacks=[earlyStopping], batch_size=batch_size
, validation_data=(validation_data, validation_y), shuffle=False)
起初我尝试使用编码器的输入层,但给了我这个错误: ValueError: Layer encoder_first_layer was called with an input that isn't a symbolic tensor. Received type: <class 'eras.engine.input_layer.InputLayer'>.Full input: [<keras.engine.input_layer.InputLayer object at 0x13b97ced0>]. All inputs to the layer should be tensors.
因此,我正在创建一个新的输入层,并在此基础上添加预训练编码器模式的第一个 LSTM 层。当我尝试拟合new_model
时,第一个纪元运行,在第一个纪元结束时,我收到以下错误:
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'encoder_input' with dtype float and shape [138,28,1]
[[{{node encoder_input}} = Placeholder[dtype=DT_FLOAT, shape=[138,28,1], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
问题出在哪里?为什么第一个纪元运行,然后它记住编码器输入层没有接收值?
更新:问题似乎与验证数据有关。在上一个纪元结束时,Keras 会检查验证数据并运行它。这就是它给出此错误的地方。
感谢@thushv89答案,我将代码修复如下:
encoder = load_model(encoder_path)
input_enc = Input(batch_shape=(batch_size, seq_len, n_features), name='ae_prediction_input')
first_enc = LSTM(encoder.layers[1].units, activation=activation, dropout=encoder.layers[1].dropout, recurrent_dropout=encoder.layers[1].recurrent_dropout, return_sequences=True, stateful=encoder.layers[1].stateful)(input_enc)
encoded, hidden_state, cell_state = LSTM(encoder.layers[2].units, activation=activation, dropout=encoder.layers[2].dropout, recurrent_dropout=encoder.layers[2].recurrent_dropout , return_sequences=False, stateful=encoder.layers[2].stateful)(first_enc)
first_dense = Dense(24, input_dim=28, activation=activation,name="first_dense_layer")(h_state)
if dropout:
first_dense = Dropout(0.2, name="first_dropout_layer")(first_dense)
second_layer = Dense(12, activation=activation, name="second_dense_layer")(first_dense)
if dropout:
second_layer = Dropout(0.2, name="snd_dropout_layer")(second_layer)
out = Dense(1, name='output_layer')(second_layer)
model = Model(input_enc, out)
model.layers[1].set_weights(encoder.layers[1].get_weights())
model.layers[2].set_weights(encoder.layers[2].get_weights())