C2W4 program assignment; model

I am getting this error:
Epoch 1/15

ValueError Traceback (most recent call last)
in <cell line: 5>()
4 # Train your model
----> 5 history = model.fit(train_generator,
6 epochs=15,
7 validation_data=validation_generator)

1 frames
/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py in tf__train_function(iterator)
13 try:
14 do_return = True
—> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False

ValueError: in user code:

File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1401, in train_function  *
    return step_function(self, iterator)
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1384, in step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1373, in run_step  **
    outputs = model.train_step(data)
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1151, in train_step
    loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1209, in compute_loss
    return self.compiled_loss(
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/compile_utils.py", line 277, in __call__
    loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.10/dist-packages/keras/src/losses.py", line 143, in __call__
    losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.10/dist-packages/keras/src/losses.py", line 270, in call  **
    return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.10/dist-packages/keras/src/losses.py", line 2221, in categorical_crossentropy
    return backend.categorical_crossentropy(
File "/usr/local/lib/python3.10/dist-packages/keras/src/backend.py", line 5573, in categorical_crossentropy

ValueError: Shapes (None, 1) and (None, 26) are incompatible

Note that my output shape is tf.keras.layers.Dense(26, activation=‘softmax’) as required. All previous assignments passed. I do not know why it is expected to get shape (None, 1); this is not binary classification assignment. Hope to get help from someone.

I’m not a mentor for this course and have not taken it, but my guess is that maybe the labels are still in “categorical” form. If so, you have two choices:

  1. Convert them to “one hot” form before applying the loss function.
  2. Use the SparseCategoricalCrossentropy loss function which can handle the labels in categorical form.

Thanks, Paul! :handshake: After I made sure labels are one-hot by using tf.keras.utils.to_categorical, the problem was removed. Fixed another small bug along the way, not worth mentioning.
But how can you tell this was the issue??? The error message from TF was totally in a different direction.

The error just said you were comparing two tensors, one had 1 element in the last dimension and the other had 26 and you mentioned that you were dealing with a 26 class softmax output. That just seemed like one obvious way to hit that error. There was also another thread recently where this issue came up on a different assignment, so it was fresh in my mind.

Great, thanks! Yeah, nothing beats real experience.