Binary Classification Using PyTorch: Training

I wrote an article titled “Binary Classification Using PyTorch: Training” in the November 2020 edition of the online Microsoft Visual Studio Magazine. See https://visualstudiomagazine.com/articles/2020/11/04/pytorch-training.aspx.

The article is the third in a series of four articles where I present a complete end-to-end example of binary classification using the PyTorch neural network code library. The problem is to classify a banknote (think euro or dollar bill) as authentic (class = 0) or forgery (class = 1) based on four predictor values extracted from a digital image of the banknote.


Testing the part of the system that does the training


In the first two articles in the series I explained how to prepare the training data and set up a Dataset object to serve up the data in batches. In the second article, I explained how to define a deep neural network for binary classification.

In the third article, I explain how to train the network. Somewhat surprisingly, training is extremely complex but PyTorch does most of the hard work for you. Probably the most difficult part of training is monitoring the loss/error during training so that you can tell if training is working or not. In pseudo-code, the training process is:

 loop max_epochs times
    loop until all batches processed
      read a batch of training data (inputs, targets)
      compute outputs using the inputs
      compute error between outputs and targets
      use error to update weights and biases
    end-loop (all batches)
  end-loop (all epochs)

In general, I’m old-school in the sense that I love printed documentation that I can hold in my hand. But for some topics, like explaining neural networks, online publication is better. I can provide code so readers can copy-paste. But the biggest advantage is that I’m not constrained by inherent print factors — print publication is extremely expensive so there’s always a word-count limit, and there are limits on the number of pictures that can be used. An online article allows me to drive into more details and reveal the underlying beauty of a system.


The game of chess is a closed system and therefore has inherent limitations. But chess can be beautiful on different levels. Here are two nice chess-themed images I found on the Internet.

This entry was posted in PyTorch. Bookmark the permalink.