CategoricalAccuracy' object has no attribute 'reset_states

When download jupyter to run on my laptop

def model(X_train, Y_train, X_test, Y_test, learning_rate = 0.0001,
num_epochs = 1500, minibatch_size = 32, print_cost = True):

It give error
"---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[33], line 1
----> 1 parameters, costs, train_acc, test_acc = model(new_train, new_y_train, new_test, new_y_test, num_epochs=100)

Cell In[32], line 58, in model(X_train, Y_train, X_test, Y_test, learning_rate, num_epochs, minibatch_size, print_cost)
55 epoch_total_loss = 0.
57 #We need to reset object to start measuring from 0 the accuracy each epoch
—> 58 train_accuracy.reset_states()
60 for (minibatch_X, minibatch_Y) in minibatches:
62 with tf.GradientTape() as tape:
63 # 1. predict

AttributeError: ‘CategoricalAccuracy’ object has no attribute ‘reset_states’
"

the ver of tensorflow is 2.16.1

So what is the issue here?

Perhaps you are not using compatible version of the tools.

Hello @quoc,

One thing to try is to google the error message like with the part I quoted in above. I can find a git issue for a similar problem on the first page of my google result that suggests a version mismatch. If you want to verify that suggestion, you might check out those version’s documentation on the Tensorflow site.

Cheers,
Raymond

Any suggestion so far?

(base) C:\Users\PHAN>pip uninstall tensorflow
Found existing installation: tensorflow 2.16.1
Uninstalling tensorflow-2.16.1:
Would remove:
c:\users\phan\anaconda3\lib\site-packages\tensorflow-2.16.1.dist-info*
Proceed (Y/n)? y
Successfully uninstalled tensorflow-2.16.1

(base) C:\Users\PHAN>pip install tensorflow==2.3.0
ERROR: Could not find a version that satisfies the requirement tensorflow==2.3.0 (from versions: 2.5.0, 2.5.1, 2.5.2, 2.5.3, 2.6.0rc0, 2.6.0rc1, 2.6.0rc2, 2.6.0, 2.6.1, 2.6.2, 2.6.3, 2.6.4, 2.6.5, 2.7.0rc0, 2.7.0rc1, 2.7.0, 2.7.1, 2.7.2, 2.7.3, 2.7.4, 2.8.0rc0, 2.8.0rc1, 2.8.0, 2.8.1, 2.8.2, 2.8.3, 2.8.4, 2.9.0rc0, 2.9.0rc1, 2.9.0rc2, 2.9.0, 2.9.1, 2.9.2, 2.9.3, 2.10.0rc0, 2.10.0rc1, 2.10.0rc2, 2.10.0rc3, 2.10.0, 2.10.1, 2.11.0rc0, 2.11.0rc1, 2.11.0rc2, 2.11.0, 2.11.1, 2.12.0rc0, 2.12.0rc1, 2.12.0, 2.12.1, 2.13.0rc0, 2.13.0rc1, 2.13.0rc2, 2.13.0, 2.13.1, 2.14.0rc0, 2.14.0rc1, 2.14.0, 2.14.1, 2.15.0rc0, 2.15.0rc1, 2.15.0, 2.15.1, 2.16.0rc0, 2.16.1)
ERROR: No matching distribution found for tensorflow==2.3.0

Looks like there is no 2.3.0? so what should I do?

  1. Google the above error message
  2. Look for git issue or webpages that discuss that or similar problem
  3. If it was a problem with version mismatch, find out what has changed between the versions
  4. Based on the change, think about what options you have.

If you can share what has changed between the versions, we can do step 4 together.

That assignment uses TF version 2.3.0. A lot must have changed between that and 2.16.1. They do not update the assignments every 2 weeks to use whatever the current version of all the packages is: that would be a huge effort and ultimately a waste of their time.

So that means that there is no guarantee that you can just run things locally with whatever versions you have installed and expect things to “just work”. There is no official solution to this issue. Here’s a thread which discusses some ways to approach this problem by duplicating environments, but that is the best we can offer.

Interesting. Well, you could try 2.5.0 and maybe you get lucky and the incompatible change happened > 2.5.x …

If that fails, then your only recourse will be to read the TF release notes for the releases between 2.3.0 and 2.5.x and see if they give any guidance about how to migrate to the new APIs (e.g. what is the new equivalent of the reset_states() method of an accuracy metric, if any).

I googled “tf keras accuracy metric” and got this top level page on the 2.16.1 document site. There is a highlighted topic visible right there entitled “Migrate metrics and optimizers”. Sounds worth a look! :grin:

1 Like

Hello @quoc,

Since you are running labs in your local environment and all kinds of version mismatch can happen. Whenever you need some hints, the following steps are the quickest way:

If we google the error message:

the first result showed up to be quite similar to our issue, and this time, there someone had got an idea of what happened:

image

From the message, we understand that it seems to be a change to the method name. In particular, reset_states() had been updated to reset_state().

Since that message mentioned TF2.4, so if we want to be careful, we can reference back to the documentation for that version:

and find out that indeed it used reset_states() with states in its pural form.

Now, we have a decision to make:

  1. we may try to remove that pural “s” from reset_states() to reset_state() in our code, and if everything runs fine, then we are fine.

  2. or we install the TF version being used by the lab.

Yes, you are right that we will need TF 2.3, but you couldn’t install it.

Again, you might investigate into this problem by googling the error:

But for this time, I know it is going to take way more steps, so please take a look at this:

So, we find 2.3 among the available versions.

I suspect that it was because your python version was too new to see the older Tensorflow on your list. On this page, you can find the dependence between tensorflow and python, in particular:

image

@quoc, hope these will help you figure something out quickly yourself in the next time :wink:

For this time, we might try the first one first, and if it doesn’t work, the second one:

Cheers,
Raymond

Thank you all for your help

I switching to reset_state (removing s) works

However, looks like the entire Course 2 cannot work properly in my laptop. I think I would like to find the requirements.txt file so i can set up a virtual environment with the exact package ver used in Course 2.

May I ask where to find that requirements.txt file?

Thank you

Thank you

But now I am searching for the requirements.txt file to make sure the virtual env has the same installed package with Coursera.

I manage to fix TF to 2.3.0 but there will be other issues related to other packages as well.

There is no “requirements.txt” file for the assignments.

Yes, as Tom says, those are not published anywhere. You need to generate them yourself. That was covered on the thread that I gave you earlier in this discussion.

The other key point here is that there is no single version that works with all assignments. Here’s another recent thread showing that some assignments use TF 2.3.0 and some use TF 2.9.1 at the moment. And that may also evolve over time. You need to do it for each assignment individually at the time that you save it locally. That also implies that you may need the ability to maintain parallel environments. One way to do that is to use conda, which is also discussed on that first linked thread.

2 Likes

I have managed to run it locally using TensorFlow 2.18. There are two steps that you need to do. First of all, you need to change the import of tf.keras.optimizers.Adam and tf.keras.metrics.CategoricalAccuracy and use keras.src.optimizers.Adam and keras.src.metrics.CategoricalAccuracy instead. The second change is the name of the function; in the function model, it is called reset_states (plural), and it seems to have been changed to reset_state. With these two changes, I was able to run it successfully