How can I make a neural network that has multiple outputs using pytorch?

If you are looking at multiclass classification and for a simple neural network, you could do it multiple ways, as a beginner try creating a class as a subclass of nn.Module in PyTorch to start with

class Network(nn.Module):
    def __init__(self):
        super().__init__()

        # Inputs to hidden layer linear transformation
        self.hidden = nn.Linear(784, 256)
        # Output layer, 10 units - one for each digit
        self.output = nn.Linear(256, 10)

        # Define sigmoid activation and softmax output 
        self.sigmoid = nn.Sigmoid()
        self.softmax = nn.Softmax(dim=1)

    def forward(self, x):
        # Pass the input tensor through each of our operations
        x = self.hidden(x)
        x = self.sigmoid(x)
        x = self.output(x)
        x = self.softmax(x)

        return x
model = Network()

There are a couple of ways to construct a Neural Network for classification using PyTorch.

import torch
from torch import nn
import torch.nn.functional as F

class Network(nn.Module):
    def __init__(self):
        super().__init__()
        # Inputs to hidden layer linear transformation
        self.hidden = nn.Linear(784, 256)
        # Output layer, 10 units - one for each digit
        self.output = nn.Linear(256, 10)
        
    def forward(self, x):
        # Hidden layer with sigmoid activation
        x = F.sigmoid(self.hidden(x))
        # Output layer with softmax activation
        x = F.softmax(self.output(x), dim=1)
        
        return x

This Network class is designed to process digit images, and it allows more customisation compared to the nn.Sequential. The last number for self.output is 10, which means we will have 10 outputs, 1 output for each digit. We put the output through softmax function to calculate class probabilities, ie. to see which digit has the highest probability for a certain digit image.

The reason we add dim=1 option for softmax is to make the calculation across the columns, so probability sums for each row will add up to 1.

Example:

>>> input = torch.tensor([[1., 2., 3.], [2., 1., 3.], [4., 2., 6.]])
>>> F.softmax(input, dim=1)
tensor([[0.0900, 0.2447, 0.6652],
    [0.2447, 0.0900, 0.6652],
    [0.1173, 0.0159, 0.8668]])
# dim=1, row sums add up to 1

>>> F.softmax(input, dim=0)
tensor([[0.0420, 0.4223, 0.0453],
    [0.1142, 0.1554, 0.0453],
    [0.8438, 0.4223, 0.9094]])
# dim=0, column sums add up to 1

Same model can be constructed using nn.Sequential, a quick way to do the same thing:

# Hyperparameters for our network
input_size = 784
hidden_sizes = [128, 64]
output_size = 10

# Build a feed-forward network
model = nn.Sequential(nn.Linear(input_size, hidden_sizes[0]),
                      nn.ReLU(),
                      nn.Linear(hidden_sizes[0], hidden_sizes[1]),
                      nn.ReLU(),
                      nn.Linear(hidden_sizes[1], output_size),
                      nn.Softmax(dim=1))

You can check out Udacity's Neural Networks in PyTorch Notebook tutorial for more explanation. Part of their Deep Learning Nanodegree content is also available as a free course, Intro to Deep Learning with PyTorch.


Multiple outputs can be trivially achieved with pytorch.

Here is one such network.

import torch.nn as nn

class NeuralNetwork(nn.Module):
  def __init__(self):
    super(NeuralNetwork, self).__init__()
    self.linear1 = nn.Linear(in_features = 3, out_features = 1)
    self.linear2 = nn.Linear(in_features = 3,out_features = 2)

  def forward(self, x):
    output1 = self.linear1(x)
    output2 = self.linear2(x)
    return output1, output2

Tags:

Pytorch