title: InvalidArgumentError: required broadcastable shapes at loc(unknown)
body: <p><strong>Background</strong></p>
  <p>I am totally new to Python and to machine learning. I just tried to set up a UNet from code I found on the internet and wanted to adapt it to the case I'm working on bit for bit. When trying to <code>.fit</code> the UNet to the training data, I received the following error:</p>
  <pre><code>InvalidArgumentError:  required broadcastable shapes at loc(unknown)
       [[node Equal (defined at &lt;ipython-input-68-f1422c6f17bb&gt;:1) ]] [Op:__inference_train_function_3847]
  </code></pre>
  <p>I get a lot of results when I search for it, but mostly they are different errors.</p>
  <p>What does this mean? And, more importantly, how can I fix it?</p>
  <p><strong>The code that caused the error</strong></p>
  <p>The context of this error is as follows:
  I want to segment images and label the different classes.
  I set up directories &quot;trn&quot;, &quot;tst&quot; and &quot;val&quot; for training, test, and validation data. The <code>dir_dat()</code> function applies <code>os.path.join()</code> to get the full path to the respective <a href="https://www.dropbox.com/sh/g4yr4qjgjtsytmi/AABJXVehoCw5In9wQvL7Llu2a?dl=0" rel="noreferrer">data set</a>. Each of the 3 folders has sub directories for each class labeled with integers. In each folder, there are some <code>.tif</code> images for the respective class.</p>
  <p>I defined the following image data generators (training data is sparse, hence the augmentation):</p>
  <pre><code>classes = np.array([ 0,  2,  4,  6,  8, 11, 16, 21, 29, 30, 38, 39, 51])
  bs = 15 # batch size
  
  augGen = ks.preprocessing.image.ImageDataGenerator(rotation_range = 365,
                                                     width_shift_range = 0.05,
                                                     height_shift_range = 0.05,
                                                     horizontal_flip = True,
                                                     vertical_flip = True,
                                                     fill_mode = &quot;nearest&quot;) \
      .flow_from_directory(directory = dir_dat(&quot;trn&quot;),
                           classes = [str(x) for x in classes.tolist()],
                           class_mode = &quot;categorical&quot;,
                           batch_size = bs, seed = 42)
      
  tst_batches = ks.preprocessing.image.ImageDataGenerator() \
      .flow_from_directory(directory = dir_dat(&quot;tst&quot;),
                           classes = [str(x) for x in classes.tolist()],
                           class_mode = &quot;categorical&quot;,
                           batch_size = bs, shuffle = False)
  
  val_batches = ks.preprocessing.image.ImageDataGenerator() \
      .flow_from_directory(directory = dir_dat(&quot;val&quot;),
                           classes = [str(x) for x in classes.tolist()],
                           class_mode = &quot;categorical&quot;,
                           batch_size = bs)
  </code></pre>
  <p>Then I set up the UNet based on <a href="https://github.com/bnsreenu/python_for_microscopists/blob/master/074-Defining%20U-net%20in%20Python%20using%20Keras.py" rel="noreferrer">this example</a>. Here, I altered a few parameters to adapt the UNet to the situation (multiple classes), namely activation in the last layer and the loss function:</p>
  <pre><code>layer_in = ks.layers.Input(shape = (imgr, imgc, imgdim))
  # convert pixel integer values to float
  inVals = ks.layers.Lambda(lambda x: x / 255)(layer_in)
  
  # Contraction path
  c1 = ks.layers.Conv2D(16, (3, 3), activation = &quot;relu&quot;,
                              kernel_initializer = &quot;he_normal&quot;, padding = &quot;same&quot;)(inVals)
  c1 = ks.layers.Dropout(0.1)(c1)
  c1 = ks.layers.Conv2D(16, (3, 3), activation = &quot;relu&quot;,
                              kernel_initializer = &quot;he_normal&quot;, padding = &quot;same&quot;)(c1)
  p1 = ks.layers.MaxPooling2D((2, 2))(c1)
  
  c2 = ks.layers.Conv2D(32, (3, 3), activation = &quot;relu&quot;,
                              kernel_initializer = &quot;he_normal&quot;, padding = &quot;same&quot;)(p1)
  c2 = ks.layers.Dropout(0.1)(c2)
  c2 = ks.layers.Conv2D(32, (3, 3), activation = &quot;relu&quot;,
                              kernel_initializer = &quot;he_normal&quot;, padding = &quot;same&quot;)(c2)
  p2 = ks.layers.MaxPooling2D((2, 2))(c2)
   
  c3 = ks.layers.Conv2D(64, (3, 3), activation = &quot;relu&quot;,
                              kernel_initializer = &quot;he_normal&quot;, padding = &quot;same&quot;)(p2)
  c3 = ks.layers.Dropout(0.2)(c3)
  c3 = ks.layers.Conv2D(64, (3, 3), activation = &quot;relu&quot;,
                              kernel_initializer = &quot;he_normal&quot;, padding = &quot;same&quot;)(c3)
  p3 = ks.layers.MaxPooling2D((2, 2))(c3)
   
  c4 = ks.layers.Conv2D(128, (3, 3), activation = &quot;relu&quot;,
                              kernel_initializer = &quot;he_normal&quot;, padding = &quot;same&quot;)(p3)
  c4 = ks.layers.Dropout(0.2)(c4)
  c4 = ks.layers.Conv2D(128, (3, 3), activation = &quot;relu&quot;,
                              kernel_initializer = &quot;he_normal&quot;, padding = &quot;same&quot;)(c4)
  p4 = ks.layers.MaxPooling2D(pool_size = (2, 2))(c4)
   
  c5 = ks.layers.Conv2D(256, (3, 3), activation = &quot;relu&quot;,
                              kernel_initializer = &quot;he_normal&quot;, padding = &quot;same&quot;)(p4)
  c5 = ks.layers.Dropout(0.3)(c5)
  c5 = ks.layers.Conv2D(256, (3, 3), activation = &quot;relu&quot;,
                              kernel_initializer = &quot;he_normal&quot;, padding = &quot;same&quot;)(c5)
  
  # Expansive path 
  u6 = ks.layers.Conv2DTranspose(128, (2, 2), strides = (2, 2), padding = &quot;same&quot;)(c5)
  u6 = ks.layers.concatenate([u6, c4])
  c6 = ks.layers.Conv2D(128, (3, 3), activation = &quot;relu&quot;,
                              kernel_initializer = &quot;he_normal&quot;, padding = &quot;same&quot;)(u6)
  c6 = ks.layers.Dropout(0.2)(c6)
  c6 = ks.layers.Conv2D(128, (3, 3), activation = &quot;relu&quot;,
                              kernel_initializer = &quot;he_normal&quot;, padding = &quot;same&quot;)(c6)
   
  u7 = ks.layers.Conv2DTranspose(64, (2, 2), strides = (2, 2), padding = &quot;same&quot;)(c6)
  u7 = ks.layers.concatenate([u7, c3])
  c7 = ks.layers.Conv2D(64, (3, 3), activation = &quot;relu&quot;,
                              kernel_initializer = &quot;he_normal&quot;, padding = &quot;same&quot;)(u7)
  c7 = ks.layers.Dropout(0.2)(c7)
  c7 = ks.layers.Conv2D(64, (3, 3), activation = &quot;relu&quot;,
                              kernel_initializer = &quot;he_normal&quot;, padding = &quot;same&quot;)(c7)
   
  u8 = ks.layers.Conv2DTranspose(32, (2, 2), strides = (2, 2), padding = &quot;same&quot;)(c7)
  u8 = ks.layers.concatenate([u8, c2])
  c8 = ks.layers.Conv2D(32, (3, 3), activation = &quot;relu&quot;,
                              kernel_initializer = &quot;he_normal&quot;, padding = &quot;same&quot;)(u8)
  c8 = ks.layers.Dropout(0.1)(c8)
  c8 = ks.layers.Conv2D(32, (3, 3), activation = &quot;relu&quot;,
                              kernel_initializer = &quot;he_normal&quot;, padding = &quot;same&quot;)(c8)
   
  u9 = ks.layers.Conv2DTranspose(16, (2, 2), strides = (2, 2), padding = &quot;same&quot;)(c8)
  u9 = ks.layers.concatenate([u9, c1], axis = 3)
  c9 = ks.layers.Conv2D(16, (3, 3), activation = &quot;relu&quot;,
                              kernel_initializer = &quot;he_normal&quot;, padding = &quot;same&quot;)(u9)
  c9 = ks.layers.Dropout(0.1)(c9)
  c9 = ks.layers.Conv2D(16, (3, 3), activation = &quot;relu&quot;,
                              kernel_initializer = &quot;he_normal&quot;, padding = &quot;same&quot;)(c9)
   
  out = ks.layers.Conv2D(1, (1, 1), activation = &quot;softmax&quot;)(c9)
   
  model = ks.Model(inputs = layer_in, outputs = out)
  model.compile(optimizer = &quot;adam&quot;, loss = &quot;sparse_categorical_crossentropy&quot;, metrics = [&quot;accuracy&quot;])
  model.summary()
  </code></pre>
  <p>Finally, I defined callbacks and ran the training, which produced the error:</p>
  <pre><code>cllbs = [
      ks.callbacks.EarlyStopping(patience = 4),
      ks.callbacks.ModelCheckpoint(dir_out(&quot;Checkpoint.h5&quot;), save_best_only = True),
      ks.callbacks.TensorBoard(log_dir = './logs'),# log events for TensorBoard
      ]
  
  model.fit(augGen, epochs = 5, validation_data = val_batches, callbacks = cllbs)
  </code></pre>
  <p><strong>Full console output</strong></p>
  <p>This is the full output when running the last line (in case it helps solving the issue):</p>
  <pre><code>trained = model.fit(augGen, epochs = 5, validation_data = val_batches, callbacks = cllbs)
  Epoch 1/5
  Traceback (most recent call last):
  
    File &quot;&lt;ipython-input-68-f1422c6f17bb&gt;&quot;, line 1, in &lt;module&gt;
      trained = model.fit(augGen, epochs = 5, validation_data = val_batches, callbacks = cllbs)
  
    File &quot;c:\users\manuel\python\lib\site-packages\tensorflow\python\keras\engine\training.py&quot;, line 1183, in fit
      tmp_logs = self.train_function(iterator)
  
    File &quot;c:\users\manuel\python\lib\site-packages\tensorflow\python\eager\def_function.py&quot;, line 889, in __call__
      result = self._call(*args, **kwds)
  
    File &quot;c:\users\manuel\python\lib\site-packages\tensorflow\python\eager\def_function.py&quot;, line 950, in _call
      return self._stateless_fn(*args, **kwds)
  
    File &quot;c:\users\manuel\python\lib\site-packages\tensorflow\python\eager\function.py&quot;, line 3023, in __call__
      return graph_function._call_flat(
  
    File &quot;c:\users\manuel\python\lib\site-packages\tensorflow\python\eager\function.py&quot;, line 1960, in _call_flat
      return self._build_call_outputs(self._inference_function.call(
  
    File &quot;c:\users\manuel\python\lib\site-packages\tensorflow\python\eager\function.py&quot;, line 591, in call
      outputs = execute.execute(
  
    File &quot;c:\users\manuel\python\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,
  
  InvalidArgumentError:  required broadcastable shapes at loc(unknown)
       [[node Equal (defined at &lt;ipython-input-68-f1422c6f17bb&gt;:1) ]] [Op:__inference_train_function_3847]
  
  Function call stack:
  train_function
  </code></pre>