title: TensorFlow: logits and labels must have the same first dimension, got logits shape [120,4] and labels shape [480]
body: <p>I created an object detection model with hand written digits images, my model has two output layers first is to classification between digits, and second one is to detect/localize the bounding box coordinates of digit.</p>
  <p>Output = [Output1, Output2]</p>
  <p>Output1 = [0, 1, 2, 3, 4, 5, 6, 7, 8 ,9] (Dense layer with 10 unit)</p>
  <p>Output2 = [x, y, w, h] (Dense layer with 4 units for bounding box coordinates)</p>
  <p>I am facing am error regarding logits and label shape, I am not getting why this error is occurring, help me out.</p>
  <p><a href="https://i.stack.imgur.com/byQez.png" rel="nofollow noreferrer">This is the SS of csv file labels</a></p>
  <p><a href="https://i.stack.imgur.com/SVrM7.png" rel="nofollow noreferrer">Training image look like</a></p>
  <pre><code>import os
  os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
  import tensorflow as tf
  import pandas as pd
  
  data = pd.read_csv('train_label.csv')
  
  image = data.iloc[:,0].values
  label = data.iloc[:,1:].values
  
  train = tf.data.Dataset.from_tensor_slices((image, label))
  
  def collector(images_file, label):
      image = tf.io.read_file('train\\'+images_file)
      image = tf.image.decode_image(image, channels=1, dtype=tf.float32)
  
      labels = {'label': label[0], 'coordinates': label[1:]}
      return image, labels
  
  train = train.map(collector).batch(120)
  
  input = tf.keras.Input(shape=(75, 75, 1))
  x = tf.keras.layers.Conv2D(32, kernel_size=3, activation='relu')(input)
  x = tf.keras.layers.MaxPooling2D((3,3), strides=(1,1), padding='same')(x)
  x = tf.keras.layers.BatchNormalization()(x)
  x = tf.keras.layers.Conv2D(64, kernel_size=3, activation='relu')(x)
  x = tf.keras.layers.MaxPooling2D((3,3), strides=(1,1), padding='same')(x)
  x = tf.keras.layers.BatchNormalization()(x)
  x = tf.keras.layers.Conv2D(128, kernel_size=3, activation='relu')(x)
  x = tf.keras.layers.MaxPooling2D((3,3), strides=(1,1), padding='same')(x)
  x = tf.keras.layers.BatchNormalization()(x)
  x = tf.keras.layers.Flatten()(x)
  x = tf.keras.layers.Dense(128, activation='relu')(x)
  output1 = tf.keras.layers.Dense(10, activation='softmax', name=&quot;label&quot;)(x)
  output2 = tf.keras.layers.Dense(4, name=&quot;coordinates&quot;)(x)
  
  model = tf.keras.Model(inputs=input, outputs=[output1, output2])
  
  model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
                optimizer='adam', metrics=['accuracy'])
  
  model.fit(train, batch_size=120, epochs=5, verbose=1)
  </code></pre>
  <pre><code>Epoch 1/5
  Traceback (most recent call last):
    File &quot;D:/obj_detection_and_classification/train_model.py&quot;, line 42, in &lt;module&gt;
      model.fit(train, batch_size=120, epochs=5, verbose=1)
    File &quot;C:\Users\Devanshu\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py&quot;, line 1100, in fit
      tmp_logs = self.train_function(iterator)
    File &quot;C:\Users\Devanshu\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\def_function.py&quot;, line 828, in __call__
      result = self._call(*args, **kwds)
    File &quot;C:\Users\Devanshu\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\def_function.py&quot;, line 888, in _call
      return self._stateless_fn(*args, **kwds)
    File &quot;C:\Users\Devanshu\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\function.py&quot;, line 2942, in __call__
      return graph_function._call_flat(
    File &quot;C:\Users\Devanshu\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\function.py&quot;, line 1918, in _call_flat
      return self._build_call_outputs(self._inference_function.call(
    File &quot;C:\Users\Devanshu\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\function.py&quot;, line 555, in call
      outputs = execute.execute(
    File &quot;C:\Users\Devanshu\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\execute.py&quot;, line 59, in quick_execute
      tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
  tensorflow.python.framework.errors_impl.InvalidArgumentError:  logits and labels must have the same first dimension, got logits shape [120,4] and labels shape [480]
       [[node sparse_categorical_crossentropy_1/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits (defined at D:/obj_detection_and_classification/train_model.py:42) ]] [Op:__inference_train_function_1524]
  
  Function call stack:
  train_function
  
  </code></pre>