Lesson 2: AttributeError when trying to use plot_confusion_matrix

I’m following along with the fastai book. I encountered an error plotting the confusion matrix in chapter 2.

After training the learner for the first time I run the following code:

interp = ClassificationInterpretation.from_learner(learner)
learner.plot_confusion_matrix()

But I get the following error:

---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-15-5ed97f736e90> in <cell line: 2>()
      1 interp = ClassificationInterpretation.from_learner(learner)
----> 2 learner.plot_confusion_matrix()

1 frames

/usr/local/lib/python3.9/dist-packages/torch/nn/modules/module.py in __getattr__(self, name)
   1612             if name in modules:
   1613                 return modules[name]
-> 1614         raise AttributeError("'{}' object has no attribute '{}'".format(
   1615             type(self).__name__, name))
   1616 

AttributeError: 'Sequential' object has no attribute 'plot_confusion_matrix'

You can see the complete notebook here:

Any ideas why this might be the case? I’m using Google Colab with the latest version of fastai (2.7.12).

[quote=“enigmeta, post:1, topic:105101”]

uential' object has no attribute 'plot_confusion_matrix'

Hi enigmeta,
you just confused the terms: the classificationinterpreter is (as you called) ‘interp’

So, just write:
interp.plot_confusion_matrix()

it should work :slight_smile:

2 Likes

Hello!

You need to use the Interpretation object in order to use its methods i.e. instead of learner.plot_confusion_matrix(), you need to do interp.plot_confusion_matrix().

I think this will resolve the error.

Hope this helps :slight_smile:

2 Likes

Thanks for the solution! I’ve been banging my head against the wall why it wouldn’t work. It helps to have somebody with fresh eyes to look at it.

Thanks again!

1 Like