Transfer Learning with MobileNetV2 Error: 'Accuracy' object is not subscriptable

Hi, I’m doing the Assignment 2 of Week 2 of the course. In the exercise 3 you are supposed to define the optimizer as Adam, Binary Cross entropy as loss function and Accuracy as metric but when I run the cell that tests the model it displays an error that says “Accuracy” object not subscriptable. However, I ran the following cells that fit the model and display the graphs of Training and validation accuracy and Training and validation loss, I can see that the model works fine the way I coded the metric accuracy.

This is how I coded the accuracy: metrics=tf.keras.metrics.Accuracy()

Hope you can help me with this issue, please.

Best regards.

2 Likes

It’s much simpler than that.

# Use accuracy as evaluation metric
metrics=['accuracy']
7 Likes

Thanks for your help!

Thanks @TMosh for this solution
.
Had the same issue and I did the following:
metrics=[ tf.keras.metrics.Accuracy( name =‘accuracy’) ]

Just curious as to why what I’ve done and what @lili94dg has done won’t work. For the optimizer and the loss function we define using the tf.keras.optimizers and tf.keras.losses package. Why is it not the same for metrics.

1 Like

Thanks for your response @TMosh

So in general practice there should be no issue if I did the following?

metrics=tf.keras.metrics.Accuracy()

Thanks for asking this question, it helped me a lot.
I also used metrics=tf.keras.metrics.Accuracy() and got the error message.
For the new ones who will struggle, I found a suitable answer to understand the issue.

You should check the documentation here :

You can look to the example for model.compile
Then you read : * Typically you will use metrics=['accuracy']*

The issue why .Accuracy() is not working might be explained by :

When you pass the strings ‘accuracy’ or ‘acc’, we convert this to one of [ tf.keras.metrics.BinaryAccuracy ], [ tf.keras.metrics.CategoricalAccuracy ], [ tf.keras.metrics.SparseCategoricalAccuracy ] based on the loss function used and the model output shape

So it means that metrics expect sth in bracket and that metrics = ‘accuracy’ doesn’t correspond to .Accuracy() but one of the 3 previous mentionned functions.

Then I tried thoses function that corresponded to metrics = ‘accuracy’ but coudn’t find a working solution like : metrics = [tf.keras.metrics.BinaryAccuracy()]

Hope it can help

5 Likes

@Leonard_Truscello provides nice additional information about this issue that many people seem to run into. Using the string name version allows the compile function to enforce consistency with the loss function, which makes sense. However, in this case, the answer about why to use one syntax versus the other is merely that the course developer used the string version in writing the test case assertion. If you fail to make the metrics a Python list and just straight assign an Accuracy instance you will hit the not subscriptable runtime error, but even if you use the list structure but don’t use the string name syntax option you will fail the assertion because an Accuracy instance in metrics[0] and the string literal ‘accuracy’ will not evaluate as equivalent.

1 Like

This had me completely stumped :laughing:
Thanks for asking this question here, @lili94dg

Actually, quite nonsensical after the other layers which were derived from tf.keras. But thanks anyways

That’s the key - this is an implementation choice for the test case and the grader.