How to create a confusion matrix with TensorBoard and PyTorch

Christian Bernecker
2 min readJul 28, 2021

--

Confusion Matrix Tensorboard

In this article I’ll explain how you can create a confusion matrix with TensorBoard and PyTroch. At the end of this article you will find the link to this code on my GITHub. If you need a confustion matrix without TensorBoard you can jump to the following tutorial here:

Let’s start and load the data:

Loading the FashionMNIST datatset.

The confusion Matrix:

The Conv-Net:

This is a simple architecture of a Conv-Net. Not fancy but it works!

Convolutional Neural Network.

Train the data:

Feed the Conv-Net with the data. Reduce the epochs if you have a slow CPU.

You will get something like this:

Epoch-1 lr: 0.001 
Training loss 0.225 Accuracy 0.354 Steps: 999
Training loss 0.163 Accuracy 0.522 Steps: 1999
Training loss 0.137 Accuracy 0.595 Steps: 2999
Training loss 0.122 Accuracy 0.638 Steps: 3999
Training loss 0.111 Accuracy 0.669 Steps: 4999
Training loss 0.103 Accuracy 0.691 Steps: 5999
Training loss 0.0976 Accuracy 0.708 Steps: 6999
Accuracy: 42945/7500 (71.6 %) Loss: 0.0951

In the meantime you can open Tensorboard via CMD:

tensorboard --logdir=runs

or I recommend to use Visual Studio Code with the Python Extension that contains a Tensorboard extension that you can use out of the box:

Click on the image tab and you should finally see:

Confusion Matrix Tensorboard

Congrats you got it!!!

You can donwload the full notebook here: https://github.com/cbernecker/medium/blob/main/confusion_matrix_tensorboard.ipynb

--

--

Christian Bernecker

AI enthusiast, speaker, and software developer passionate about leveraging technology to improve the world. Always happy to share knowledge and connect