简体   繁体   中英

keras: Tensor objects are not iterable when eager execution is not enabled

I'm writing a Sequence to Sequence model in Keras. For some reason when I try defining the model in the function below:

def define_GRU_models(encoder_input_dim,
              output_dim,
              activation,
              n_units):
# define training encoder #
###########################
# layer 1
encoder_inputs = Input(shape=encoder_input_dim)
l1_encoder = GRU(n_units,
                      name='l1_encoder',
                      return_sequences=True,
                      return_state=True)
l1_encoder_outputs, l1_encoder_state = l1_encoder(encoder_inputs)

# layer 2
l2_encoder = GRU(n_units,
                      name='l2_encoder',
                      return_state=True)
l2_encoder_outputs, l2_encoder_state = l2_encoder(l1_encoder_outputs)

# define training decoder #
###########################

# layer 1
decoder_inputs = Input(shape=(None, output_dim))
l1_decoder_gru = GRU(int(n_units/2),
                          name='l1_decoder_gru',
                          return_sequences=True,
                          return_state=False)
l1_decoder_outputs, _ = l1_decoder_gru(decoder_inputs)

# layer 2
l2_decoder_gru = GRU(n_units,
                          name='l2_decoder_gru',
                          return_sequences=True,
                          return_state=False)
l2_decoder_outputs, _ = l2_decoder_gru(l1_decoder_outputs, initial_state=l1_encoder_state)

# layer 3
l3_decoder_gru = GRU(n_units,
                          name='l3_decoder_gru',
                          return_sequences=True,
                          return_state=False)
l3_decoder_outputs, _ = l3_decoder_gru(l2_decoder_outputs, initial_state=l2_encoder_state)

# layer 4
l4_decoder_gru = GRU(int(n_units/2),
                          name='l4_decoder_gru',
                          return_state=False                              )
l4_decoder_outputs, _ = l4_decoder_gru(l3_decoder_outputs)

decoder_dense = Dense(output_dim, name='decoder_dense', activation=activation)
decoder_outputs = decoder_dense(l4_decoder_outputs)
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)

return model

I reach this error:

Tensor objects are not iterable when eager execution is not enabled. To iterate over this tensor use tf.map_fn.

for this line (the first decoder layer):

l1_decoder_outputs, _ = l1_decoder_gru(decoder_inputs)

I can't seem to find the solution in any other place. What am I doing wrong? Cause it seems compatible with the keras example.

BTW, my function inputs are:

(168, 12), 24, 'softmax', 128

The problem is that the 'l1_decoder_gru' does not return its states (ie return_state=False ). It has only one output tensor which is assigned to l1_decoder_outputs . Therefore, to resolve this issue, either remove the , _ part in the left part of assignment:

l1_decoder_outputs = l1_decoder_gru(decoder_inputs)

or alternatively you can set the return_state argument to True for 'l1_decoder_gru' layer (of course, if it makes sense to do so and you may need state of this layer in another part of your model). The same thing applies to other GRU layers you have defined and used in your model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM