Required broadcastable shapes

During training, after 1 epoch i get following error

    File "/usr/local/lib/python3.10/dist-packages/keras/optimizers/optimizer_experimental/", line 259, in compute_gradients
      grads = tape.gradient(loss, var_list)
Node: 'gradient_tape/sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/mul'
required broadcastable shapes
	 [[{{node gradient_tape/sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/mul}}]] [Op:__inference_train_function_36362]

I am fitting my model during training using my custom dataset in batches,

input and output has the following shapes

img_shape:  (24, 64, 160, 3)
label_shape:  (24,)

my model has following input

Layer (type)                   Output Shape         Param #     Connected to                     
 input_2 (InputLayer)           [(24, 64, 160, 3)]   0           []                               

and following output

dense (Dense)                  (24, 34)             34034       ['tf.compat.v1.squeeze_1[0][0]'] 

I am using sparse_categorical_crossentropy loss

Hi @hlacik, Could you please provide the standalone code to reproduce issues. Thank You.


here is the link for google collab notebook with model/compile/fit

Google Colab Notebook

Hello all,
Any updates on this? I am encountering a similar error. Thanks in advance!

In my case, there was something wrong with the data generator, basically it ran through the dataset once and during the second epoch, the batch dimension had a different dimension, which caused the model output to be of different dimension which eventually screwed up the loss calculation. Hope that helps!

basically same issue on my side - dataset batching.