我已经定义并训练了自动编码器模型,如下所示:
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.
因此,我创建了一个新的输入层,并在此之上添加了pre的第一个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将检查验证数据并运行它。这就是给出此错误的地方。
答案 0 :(得分:0)
感谢@ 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())