NLP C3 W2 Assignment model training failure

When creating a post, please add:

Hi,

I am passing all the unit tests but getting a failure when training the model.
The provided code for model.fit():

tf.keras.utils.set_random_seed(33) ## Setting again a random seed to ensure reproducibility

BATCH_SIZE = 64

model.fit(train_dataset.batch(BATCH_SIZE),
          validation_data = val_dataset.batch(BATCH_SIZE),
          shuffle=True,
          epochs = 2)

The error:

Epoch 1/2
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[114], line 5
      1 tf.keras.utils.set_random_seed(33) ## Setting again a random seed to ensure reproducibility
      3 BATCH_SIZE = 64
----> 5 model.fit(train_dataset.batch(BATCH_SIZE),
      6           validation_data = val_dataset.batch(BATCH_SIZE),
      7           shuffle=True,
      8           epochs = 2)

File /usr/local/lib/python3.8/dist-packages/keras/src/utils/traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs)
     67     filtered_tb = _process_traceback_frames(e.__traceback__)
     68     # To get the full stack trace, call:
     69     # `tf.debugging.disable_traceback_filtering()`
---> 70     raise e.with_traceback(filtered_tb) from None
     71 finally:
     72     del filtered_tb

File /tmp/__autograph_generated_fileuzq8fx1k.py:15, in outer_factory.<locals>.inner_factory.<locals>.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.8/dist-packages/keras/src/engine/training.py", line 1338, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.8/dist-packages/keras/src/engine/training.py", line 1322, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.8/dist-packages/keras/src/engine/training.py", line 1303, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.8/dist-packages/keras/src/engine/training.py", line 1085, in train_step
        return self.compute_metrics(x, y, y_pred, sample_weight)
    File "/usr/local/lib/python3.8/dist-packages/keras/src/engine/training.py", line 1179, in compute_metrics
        self.compiled_metrics.update_state(y, y_pred, sample_weight)
    File "/usr/local/lib/python3.8/dist-packages/keras/src/engine/compile_utils.py", line 605, in update_state
        metric_obj.update_state(y_t, y_p, sample_weight=mask)
    File "/usr/local/lib/python3.8/dist-packages/keras/src/utils/metrics_utils.py", line 77, in decorated
        update_op = update_state_fn(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras/src/metrics/base_metric.py", line 140, in update_state_fn
        return ag_update_state(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras/src/metrics/base_metric.py", line 728, in update_state  **
        return super().update_state(matches, sample_weight=sample_weight)
    File "/usr/local/lib/python3.8/dist-packages/keras/src/metrics/base_metric.py", line 504, in update_state
        ) = losses_utils.squeeze_or_expand_dimensions(
    File "/usr/local/lib/python3.8/dist-packages/keras/src/utils/losses_utils.py", line 224, in squeeze_or_expand_dimensions
        sample_weight = tf.squeeze(sample_weight, [-1])

    ValueError: Can not squeeze dim[1], expected a dimension of 1, got 104 for '{{node Squeeze}} = Squeeze[T=DT_FLOAT, squeeze_dims=[-1]](Cast_7)' with input shapes: [?,104].

BTW, I’m getting 0/10 points for the masked_accuracy test when submitted the notebook, so maybe I need to fix it? I’m passing all the given unit tests though. If there’s any guidance on this, I would really appreciate it!

image
I

I was putting a wrong argument for tf.reduce_sum in the masked_accuracy, and it solved the both model training issue & the masked_accuracy points.

3 Likes