title: ValueError: `logits` and `labels` must have the same shape, received ((None, 250, 1) vs (None,))
body: <p>I'm a noob in machinelearning. I'm running LSTM model with the following layers in Sequential. The encoder is encoding an address into a 250 char long tf_id array. See the model summary below for reference. When i'm running model.fit() i get an error below. i tried <code>Flattened()</code> layer before the Dense(1) and it did not work. any pointers?</p>
  <p>error</p>
  <pre><code>Traceback (most recent call last):
  
  File &quot;/Users/zeus/PycharmProjects/ml_gibb_addr/com/address/train/train_lstm_model.py&quot;, line 70, in &lt;module&gt;
      train_lstm_model(data=data, epochs=10)
    File &quot;/Users/zeus/PycharmProjects/ml_gibb_addr/com/address/train/train_lstm_model.py&quot;, line 41, in train_lstm_model
      history = model.fit(
    File &quot;/Users/zeus/PycharmProjects/machineLearning/venv1/lib/python3.8/site-packages/keras/utils/traceback_utils.py&quot;, line 67, in error_handler
      raise e.with_traceback(filtered_tb) from None
    File &quot;/Users/zeus/PycharmProjects/machineLearning/venv1/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py&quot;, line 1147, in autograph_handler
      raise e.ag_error_metadata.to_exception(e)
  ValueError: in user code:
  
      File &quot;/Users/zeus/PycharmProjects/machineLearning/venv1/lib/python3.8/site-packages/keras/engine/training.py&quot;, line 1021, in train_function  *
          return step_function(self, iterator)
      File &quot;/Users/zeus/PycharmProjects/machineLearning/venv1/lib/python3.8/site-packages/keras/engine/training.py&quot;, line 1010, in step_function  **
          outputs = model.distribute_strategy.run(run_step, args=(data,))
      File &quot;/Users/zeus/PycharmProjects/machineLearning/venv1/lib/python3.8/site-packages/keras/engine/training.py&quot;, line 1000, in run_step  **
          outputs = model.train_step(data)
      File &quot;/Users/zeus/PycharmProjects/machineLearning/venv1/lib/python3.8/site-packages/keras/engine/training.py&quot;, line 860, in train_step
          loss = self.compute_loss(x, y, y_pred, sample_weight)
      File &quot;/Users/zeus/PycharmProjects/machineLearning/venv1/lib/python3.8/site-packages/keras/engine/training.py&quot;, line 918, in compute_loss
          return self.compiled_loss(
      File &quot;/Users/zeus/PycharmProjects/machineLearning/venv1/lib/python3.8/site-packages/keras/engine/compile_utils.py&quot;, line 201, in __call__
          loss_value = loss_obj(y_t, y_p, sample_weight=sw)
      File &quot;/Users/zeus/PycharmProjects/machineLearning/venv1/lib/python3.8/site-packages/keras/losses.py&quot;, line 141, in __call__
          losses = call_fn(y_true, y_pred)
      File &quot;/Users/zeus/PycharmProjects/machineLearning/venv1/lib/python3.8/site-packages/keras/losses.py&quot;, line 245, in call  **
          return ag_fn(y_true, y_pred, **self._fn_kwargs)
      File &quot;/Users/zeus/PycharmProjects/machineLearning/venv1/lib/python3.8/site-packages/keras/losses.py&quot;, line 1932, in binary_crossentropy
          backend.binary_crossentropy(y_true, y_pred, from_logits=from_logits),
      File &quot;/Users/zeus/PycharmProjects/machineLearning/venv1/lib/python3.8/site-packages/keras/backend.py&quot;, line 5247, in binary_crossentropy
          return tf.nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)
  
      ValueError: `logits` and `labels` must have the same shape, received ((None, 250, 1) vs (None,)).
  </code></pre>
  <p>Code:</p>
  <pre><code> model = keras.Sequential([
          encoder,
          tf.keras.layers.Embedding(
              input_dim=len(encoder.get_vocabulary()),
              output_dim=64,
              # Use masking to handle the variable sequence lengths
              mask_zero=True),
          tf.keras.layers.LSTM(64, return_sequences=True),
          tf.keras.layers.LSTM(64, return_sequences=True),
          tf.keras.layers.Dense(64, activation='relu'),
          tf.keras.layers.Dense(1)
      ])
      model.summary()
      model.compile(loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
                    optimizer=tf.keras.optimizers.Adam(1e-4),
                    metrics=['accuracy'])
  
      nptrain_ds = np.asarray(train_dataset)
      nptest_ds = np.asarray(test_dataset)
  
      history = model.fit(
          x=nptrain_ds[0], y=nptrain_ds[1].astype(float),
          epochs=epochs,
          # validation_data=nptest_ds,
          validation_split=validation_split)
  </code></pre>
  <p>Model summary:</p>
  <pre><code>_________________________________________________________________
   Layer (type)                Output Shape              Param #   
  =================================================================
   text_vectorization (TextVec  (None, 250)              1         
   torization)                                                     
                                                                   
   embedding (Embedding)       (None, 250, 64)           16000     
                                                                   
   lstm (LSTM)                 (None, 250, 64)           33024     
                                                                   
   lstm_1 (LSTM)               (None, 250, 64)           33024     
                                                                   
   dense (Dense)               (None, 250, 64)           4160      
                                                                   
   dense_1 (Dense)             (None, 250, 1)            65    
  </code></pre>