title: Evaluation of Loss function returns error in LSTM model
body: <p>I am trying to fit an LSTM Model for text generation with pre-trained embeddings using the tensorflow <code>keras.Sequential</code> library. I am having the following evaluation error:</p>
  <pre><code>tensorflow.python.framework.errors_impl.InvalidArgumentError:  assertion failed: [Condition x == y did not hold element-wise:] [x (sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/Shape_1:0) = ] [5 199] [y (sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/strided_slice:0) = ] [200 199]
       [[node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/assert_equal_1/Assert/Assert (defined at &lt;input&gt;:161) ]] [Op:__inference_train_function_4885]
  </code></pre>
  <p>My model is as following:</p>
  <pre><code>def build_model(vocab_size, embedding_dim, rnn_units, batch_size, embedding_matrix):
      model = tf.keras.Sequential([
          #vocab_size = 30000, embedding_dim = 300, batch_size=64, embedding_matrix.shape = (30000, 300) 
          tf.keras.layers.Embedding(vocab_size, embedding_dim, weights=[embedding_matrix], trainable=False, batch_input_shape=[max_len, None]),
          tf.keras.layers.Dropout(0.2),
          tf.keras.layers.LSTM(rnn_units,
                          return_sequences=True,
                          stateful=True,
                          recurrent_initializer='glorot_uniform'),
          tf.keras.layers.Dropout(0.2),
          tf.keras.layers.LSTM(rnn_units,
                          return_sequences=True,
                          stateful=True,
                          recurrent_initializer='glorot_uniform'),
          tf.keras.layers.Dropout(0.2),
          tf.keras.layers.Dense(vocab_size)
      ])
      return model
  
  
  model = build_model(
      vocab_size=len(vocab),
      embedding_dim=embedding_dim,
      rnn_units=rnn_units,
      batch_size=batch_size,
      embedding_matrix=embedding_matrix
  )
  
  optimizer = tf.keras.optimizers.Adam()
  model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy')
  patience = 10
  early_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=patience)
  checkpoint_dir = './checkpoints'+ datetime.datetime.now().strftime(&quot;_%Y.%m.%d-%H:%M:%S&quot;)
  checkpoint_prefix = os.path.join(checkpoint_dir, &quot;ckpt_{epoch}&quot;)
  checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
      filepath=checkpoint_prefix,
      save_weights_only=True
  )
  
  history = model.fit(text_ds, epochs=epochs, callbacks=[checkpoint_callback, early_stop], validation_data=text_ds)
  </code></pre>
  <p>After looking at other similar questions, the problem seems to be with respect to the input shape and output shape. Still, I can't understand what is wrong.</p>
  <p>The model summary is:</p>
  <pre><code>Model: &quot;sequential&quot;
  _________________________________________________________________
  Layer (type)                 Output Shape              Param #   
  =================================================================
  embedding (Embedding)        (200, None, 300)          9000000   
  _________________________________________________________________
  dropout (Dropout)            (200, None, 300)          0         
  _________________________________________________________________
  lstm (LSTM)                  (200, None, 1024)         5427200   
  _________________________________________________________________
  dropout_1 (Dropout)          (200, None, 1024)         0         
  _________________________________________________________________
  lstm_1 (LSTM)                (200, None, 1024)         8392704   
  _________________________________________________________________
  dropout_2 (Dropout)          (200, None, 1024)         0         
  _________________________________________________________________
  dense (Dense)                (200, None, 30000)        30750000  
  =================================================================
  Total params: 53,569,904
  Trainable params: 44,569,904
  Non-trainable params: 9,000,000
  _________________________________________________________________
  </code></pre>
  <p>The inputs and outputs shape are the following:</p>
  <pre><code>Output: 
  (200, None, 300)
  (200, None, 300)
  (200, None, 1024)
  (200, None, 1024)
  (200, None, 1024)
  (200, None, 1024)
  (200, None, 30000)
  
  Input: 
  (200, None)
  (200, None, 300)
  (200, None, 300)
  (200, None, 1024)
  (200, None, 1024)
  (200, None, 1024)
  (200, None, 1024)
  </code></pre>
  <p>Edit:</p>
  <p>By putting <code>return_sequences=False</code> in the last LSTM, I get:</p>
  <pre><code>tensorflow.python.framework.errors_impl.InvalidArgumentError:  Incompatible shapes: [200,199,300] vs. [5,199,300]
       [[node sequential/dropout/dropout/Mul_1 (defined at &lt;input&gt;:161) ]] [Op:__inference_train_function_4801]
  </code></pre>
  <p>And in this case the model summary is:</p>
  <pre><code>Model: &quot;sequential&quot;
  _________________________________________________________________
  Layer (type)                 Output Shape              Param #   
  =================================================================
  embedding (Embedding)        (200, None, 300)          9000000   
  _________________________________________________________________
  dropout (Dropout)            (200, None, 300)          0         
  _________________________________________________________________
  lstm (LSTM)                  (200, None, 1024)         5427200   
  _________________________________________________________________
  dropout_1 (Dropout)          (200, None, 1024)         0         
  _________________________________________________________________
  lstm_1 (LSTM)                (200, 1024)               8392704   
  _________________________________________________________________
  dropout_2 (Dropout)          (200, 1024)               0         
  _________________________________________________________________
  dense (Dense)                (200, 30000)              30750000  
  =================================================================
  Total params: 53,569,904
  Trainable params: 44,569,904
  Non-trainable params: 9,000,000
  _________________________________________________________________
  </code></pre>
  <p>with inputs:</p>
  <pre><code>(200, None)
  (200, None, 300)
  (200, None, 300)
  (200, None, 1024)
  (200, None, 1024)
  (200, 1024)
  (200, 1024)
  </code></pre>