Multi-Class Classification Using PyTorch 1.12.1-CPU on MacOS

I do most of my work on Windows OS machines. One morning I noticed that my MacBook laptop in my office was collecting dust so I figured I’d upgrade the existing PyTorch 1.10.0 to version 1.12.1 to make sure there were no breaking changes, and also to refresh my memory of working with MacOS. Switching between Windows and MacOS is easier for me if I stay in practice.

Windows          Mac
--------------------------------
Notepad          TextEdit
cmd              Terminal (bash)
Ctrl-c           Command-c
PrtScn key       Shift-Command-3
File Explorer    Finder
Chrome           Safari

I fired up my MacBook and then open a Terminal (bash) shell and I checked my existing Python 3.7.6 installation and it was good. Next I went to download.pytorch.org/whl/torch_stable.html and clicked on the link to the cpu/torch-1.12.1-cp37-non-macosx_10_9_x86_64.whl file which downloaded it. In the shell I uninstalled my existing PyTorch 1.10.1 with the command “pip uninstall torch”. Then I navigated to the Downloads directory and installed using the command “pip install torch-1.12.1-cp37-non-macosx_10_9_x86_64.whl”. Installation worked without any problems. Amazing.

To test the PyTorch installation, I did one of my standard multi-class classification demos. The goal is to predict a person’s political type (conservative = 0, moderate = 1, liberal = 2) from sex, age, state (Michigan, Nebraska, Oklahoma), and income. See the data and program for the Windows version at https://jamesmccaffrey.wordpress.com/2022/09/01/multi-class-classification-using-pytorch-1-12-1-on-windows-10-11/.

I copied the training and test data from the page-link above and saved as people_train.txt and people_test.txt. The data looks like:

 1,0.24,1,0,0,0.2950,2
-1,0.39,0,0,1,0.5120,1
 1,0.63,0,1,0,0.7580,0
-1,0.36,1,0,0,0.4450,1
. . .

The network definition looks like:

class Net(T.nn.Module):
  def __init__(self):
    super(Net, self).__init__()
    self.hid1 = T.nn.Linear(6, 10)  # 6-(10-10)-3
    self.hid2 = T.nn.Linear(10, 10)
    self.oupt = T.nn.Linear(10, 3)

    T.nn.init.xavier_uniform_(self.hid1.weight)
    T.nn.init.zeros_(self.hid1.bias)
    T.nn.init.xavier_uniform_(self.hid2.weight)
    T.nn.init.zeros_(self.hid2.bias)
    T.nn.init.xavier_uniform_(self.oupt.weight)
    T.nn.init.zeros_(self.oupt.bias)

  def forward(self, x):
    z = T.tanh(self.hid1(x))
    z = T.tanh(self.hid2(z))
    z = T.log_softmax(self.oupt(z), dim=1)  # NLLLoss() 
    return z
 

Anyway, after saving the data and PyTorch program, I ran the program and . . . it almost worked first time. I forgot to change the Windows “\\” file path separators to the Linux-based “/” separators. I made the changes and then the program worked. Minor miracle.



Predicting a horse race is either a multi-class classification problem or a ranking problem, depending on your point of view. Three fantastic old electric horse race games.

Left: Merit Electric Derby (UK) from the 1960s. A battery powered motor flicks one ball bearing in each track which knocks the horse up the incline. The process is random due to the physics involved. Strangely wonderful.

Center: Peers Hardy Horse Racing Derby (UK) from the 1990s. Each horse has an electric motor under the field, which attaches via magnets. Battery powered. The game is quite sophisticated for the time. An electronic board plays music and shows the final win-place-show results. Notice the clockwise direction — common in Europe but non-existent in the US.

Right: Tudor Electric Horse Race Game (US) from the 1960s. Made by the company best known for Electric Football. An electric motor vibrates the field which causes the horses to move forward. Some paths are shorter than others so the result is randomized.


This entry was posted in PyTorch. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s