How do I resolve this error?
ValueError: Layer model_101 expects 2 input(s), but it received 1 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, 1, 1, 64) dtype=float32>]
I am following the guide from Jason Brownlee on how to develop Encoder-Decoder Model for Sequence-to-Sequence Prediction. Instead of using LSTM units, I would like to also be able to use GRU units. I managed get everything work with LSTMa and with GRUs I could define and train the model by adapting the "define_models" function from the tutorial like this:
def define_models(n_input, n_output, hparams):
n_units = hparams["NUMUNITS"]
Unit = tf.keras.layers.LSTM if hparams["UNIT"] =="LSTM" else tf.keras.layers.GRU
dropout = hparams["DROPOUT"]
# define training encoder
encoder_inputs = Input(shape=(None, n_input))
encoder = Unit(n_units, return_state=True)
if hparams["UNIT"] == "LSTM":
encoder_outputs, state_h, state_c = encoder(encoder_inputs)
encoder_states = [state_h, state_c]
else:
encoder_outputs, state_h = encoder(encoder_inputs)
encoder_states = [state_h]
# define training decoder
decoder_inputs = Input(shape=(None, n_output))
decoder_lstm = Unit(n_units, return_sequences=True, return_state=True)
if hparams["UNIT"] == "LSTM":
decoder_outputs, _, _ = decoder_lstm(decoder_inputs, initial_state=encoder_states)
else:
decoder_outputs, _ = decoder_lstm(decoder_inputs, initial_state=encoder_states)
decoder_dense = Dense(n_output)
decoder_outputs = decoder_dense(decoder_outputs)
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
# define inference encoder
encoder_model = Model(encoder_inputs, encoder_states)
# define inference decoder
decoder_state_input_h = Input(shape=(n_units,))
if hparams["UNIT"] == "LSTM":
decoder_state_input_c = Input(shape=(n_units,))
decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
decoder_outputs, state_h, state_c = decoder_lstm(decoder_inputs, initial_state=decoder_states_inputs)
decoder_states = [state_h, state_c]
else:
decoder_states_inputs = [decoder_state_input_h]
decoder_outputs, state_h = decoder_lstm(decoder_inputs, initial_state=decoder_states_inputs)
decoder_states = [state_h]
decoder_outputs = decoder_dense(decoder_outputs)
decoder_model = Model([decoder_inputs] + decoder_states_inputs, [decoder_outputs] + decoder_states)
# return all models
return model, encoder_model, decoder_model
I trained the GRU-Encoder-Decoder Model as it is done in the tutorial, and when I want to use it to predict a sequence using this adjusted predict_sequence function:
def predict_sequence(infenc, infdec, source, n_steps, cardinality, hparams):
unit = hparams['UNIT']
print(f'unit: {unit}')
state = infenc.predict(source)
target_seq = np.array([0.0 for _ in range(cardinality)]).reshape(1, 1, cardinality)
output = list()
for t in range(n_steps):
if unit == "LSTM":
yhat, h, c = infdec.predict([target_seq] + state)
else:
yhat, h = infdec.predict([target_seq] + state)
output.append(yhat[0,0,:])
if unit == "LSTM":
state = [h, c]
else:
state = [h]
target_seq = yhat
return np.array(output)
But in this function the line
yhat, h, = infdec.predict([target_seq] + state)
produces the error.
As a reference to how to adjust the code so that it works with GRUs I used this guide from Keras.