Course 5 Week 4: Unable to train in Named-Entity Recognition notebook

I just ran this notebook on google colab verbatim without change, and placing all the required files.

At train.fit(…), i got:

AttributeError: in user code:

    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.7/dist-packages/transformers/modeling_tf_utils.py", line 947, in compute_loss  *
        return super().compute_loss(*args, **kwargs)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 919, in compute_loss  **
        y, y_pred, sample_weight, regularization_losses=self.losses)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 199, in __call__
        y_t, y_p, sw = match_dtype_and_rank(y_t, y_p, sw)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 686, in match_dtype_and_rank
        if ((y_t.dtype.is_floating and y_p.dtype.is_floating) or

    AttributeError: 'NoneType' object has no attribute 'dtype'

Anyone tried this recently?

You have to be extremely careful that your non-Coursera environment supports the same version of all of the tools.

Thanks. I no longer have access to the course env, could you please let me know the version of hugging face we are supposed to use?

Sorry, I do not know.

Thats rather unfortunate. All the assignments are locked after you get the certificate, so i can’t go check. But if someone still has an active subscription, please execute this cell and let us know the hugging face version

!pip list |grep transformers

It says:
transformers 4.3.2

Thanks a lot. I tried that and still errored. Next thing may be tensorflow version. I will lower the version and see if i can make it work. If you do also know this, please let me know.

Actually, this combination made it work.

%pip install transformers==4.5.1

%pip install -U tensorflow==2.6.0
%pip install -U keras==2.6

it seems the problem may lie more with the tensorflow version.

Update: There’s a method name conflict with huggingface model method compute_loss and keras TF 2.8 where it introduces compute_loss() as a new method. The way to call this in TF 2.8 is

model.compile(optimizer=optimizer, loss=model.hf_compute_loss, metrics=[‘accuracy’])

1 Like