## Binary Classification Using PyTorch: Defining a Network

I wrote an article titled “Binary Classification Using PyTorch: Defining a Network” in the October 2020 edition of the online Visual Studio Magazine. See https://visualstudiomagazine.com/articles/2020/10/14/pytorch-define-network.aspx. The article is the second in a series of four articles that give a complete end-to-end example of binary classification. I’ve written articles on binary classification before, in print magazines. With print, there are always article length constraints. But with an online channel, I was free to give many useful details that I’d always have t leave out in traditional print media.

The example problem is to classify a banknote as authentic (0) or forgery (1) based on four numeric predictor values derived from a digital image of each banknote — variance, skewness, kurtosis, entropy. My article focused on the neural network definition. Design decisions include number of nodes (I used a 4-(8-8)-1 network), weights and biases initialization (I used Xavier / Zero), activations (I used tanh() on hidden nodes and sigmoid() on the output node), and loss function (I used the BCELoss() function). Left: The complete system in action. Right: Testing the network input-output part of the system.

In my article, I mention a topic that can be very confusing to guys who are new to PyTorch. You can define a PyTorch neural network in two completely different ways. The first approach, which I strongly recommend, looks like this for a 4-7-1 network:

```class Net(T.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.hid1 = T.nn.Linear(4, 7)  # 4-7-1
self.oupt = T.nn.Linear(7, 1)

def forward(self, x):
z = T.tanh(self.hid1(x))
z = T.sigmoid(self.oupt(z))
return z

net = Net().to(device)
```

The second approach looks like:

```net = T.nn.Sequential(
T.nn.Linear(4,7),
T.nn.Tanh(),
T.nn.Linear(7,1),
T.nn.Sigmoid()
).to(device)
```

If you’re new to PyTorch, the two approaches look very different. Anyway, in my article I explain why I think the first approach is better. Left: The torch cactus (Trichocereus grandifloras). Center: The torch lily (Kniphofia uvaria). Right: The torch coral (Euphylia glabrescens).

This entry was posted in PyTorch. Bookmark the permalink.