title: Keras custom accuracy function incompatible shapes
body: <p>I am trying to use the following custom accuracy function in my model:</p>
  <pre><code>def acc_fn(pred, gt):
      pred_occupy = pred[..., 1] &gt;= config.IOU_THRESHOLD
      I1 = tf.reduce_sum(tf.cast(tf.math.logical_and(pred_occupy, tf.cast(gt, tf.bool)), tf.float32))
      I2 = tf.reduce_sum(tf.cast(tf.math.logical_or(pred_occupy, tf.cast(gt, tf.bool)), tf.float32))
      IoU = tf.math.divide(I1, I2, name = &quot;IoU&quot;)
      tf.summary.scalar(&quot;IoU&quot;, IoU)
      return IoU
  </code></pre>
  <p>invoked in keras like:</p>
  <pre><code>model.compile(loss=loss_fn, #categorical crossentropy
                  optimizer=keras.optimizers.Adam(learning_rate=config.LR),
                  metrics=[acc_fn])
  </code></pre>
  <p>I am getting incompatible shapes error while fitting the model:</p>
  <pre><code>tensorflow.python.framework.errors_impl.InvalidArgumentError:  Incompatible shapes: [1,128,128,128,2] vs. [1,128,128,128]
  </code></pre>
  <p>My model outputs a hot encoded layer of shape <code>[1,128,128,128,2]</code> and the ground truth array is hot-encoded as well and of the same shape!</p>
  <p>the last couple of layers in my model</p>
  <pre><code>add_10 (Add)                    (None, 128, 128, 128 0           conv3d_11[0][0]
                                                                   conv3d_13[0][0]
  __________________________________________________________________________________________________
  conv3d_14 (Conv3D)              (None, 128, 128, 128 433         add_10[0][0]
  __________________________________________________________________________________________________
  lambda_2 (Lambda)               (None, 128, 128, 128 0           conv3d_14[0][0]
  __________________________________________________________________________________________________
  concatenate_1 (Concatenate)     (None, 128, 128, 128 0           conv3d_14[0][0]
                                                                   lambda_2[0][0]
  __________________________________________________________________________________________________
  softmax_1 (Softmax)             (None, 128, 128, 128 0           concatenate_1[0][0]
  ==================================================================================================
  Total params: 176,458,081
  Trainable params: 176,451,105
  Non-trainable params: 6,976
  </code></pre>