1

如何解决此错误?

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>]

我正在遵循Jason Brownlee 关于如何开发用于序列到序列预测的编码器-解码器模型的指南。除了使用 LSTM 单元,我还希望能够使用 GRU 单元。我设法让 LSTMa 和 GRU 的所有工作都正常工作,我可以通过调整教程中的“define_models”函数来定义和训练模型,如下所示:

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

我按照教程中的方法训练了 GRU-Encoder-Decoder 模型,当我想使用它来预测序列时,使用这个调整后的 predict_sequence 函数:

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)

但是在这个函数中,该行 yhat, h, = infdec.predict([target_seq] + state) 会产生错误。

作为如何调整代码以使其与 GRU 一起使用的参考,我使用了 Keras 的本指南

4

0 回答 0