Keras GRU: An `initial_state` was passed that is not compatible with `cell.state_size`

Viewed 146

For a deep learning based project I have built a sequnce2sequnce model using LSTM. Now I want to use GRU instead of LSTM but I don't have enough knowledge in the domain of Deep learning. I got this error and cant solve it.

Error Message

An `initial_state` was passed that is not compatible with `cell.state_size`. Received `state_spec`=ListWrapper([InputSpec(shape=(None, 80, 512), ndim=3), InputSpec(shape=(None, 512), ndim=2)]); however `cell.state_size` is [512]

My code

time_steps_encoder=80
num_encoder_tokens=4096
latent_dim=512
time_steps_decoder=10
num_decoder_tokens=1500
batch_size=320
# Setting up the encoder
encoder_inputs = Input(shape=(time_steps_encoder, num_encoder_tokens), name="encoder_inputs")
encoder = GRU(latent_dim, return_state=True,return_sequences=True, name='endcoder')
state_h, state_c = encoder(encoder_inputs)
encoder_states = [state_h, state_c]


#Set up the decoder
decoder_inputs = Input(shape=(time_steps_decoder, num_decoder_tokens), name= "decoder_inputs")
decoder = GRU(latent_dim, return_sequences=True, return_state=True, name='decoder')
decoder_outputs, _ , _= decoder(decoder_inputs, initial_state=encoder_states)
decoder_dense = Dense(num_decoder_tokens, activation='softmax', name='decoder_relu')
decoder_outputs = decoder_dense(decoder_outputs)


model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
model.summary()
plot_model(model, to_file='model_train.png', show_shapes=True, show_layer_names=True)

Error was showing in this line Error

1 Answers

I think, batch_size might be the first dimension of your initial_states. To put it another way, each element in a batch may start out in a distinct initial_states.

Related