Running a forward pass

Introduction to Deep Learning with PyTorch

Jasmin Ludolf

Senior Data Science Content Developer, DataCamp

What is a forward pass?

$$

  • Input data flows through layers
  • Calculations performed at each layer
  • Final layer generates outputs

$$

  • Outputs produced based on weights and biases
  • Used for training and making predictions

Forward pass representation

Introduction to Deep Learning with PyTorch

What is a forward pass?

$$

Possible outputs:

  • Binary classification
  • Multi-class classification
  • Regressions

Forward pass representation with final output highlighted

Introduction to Deep Learning with PyTorch

Binary classification: forward pass

Coding block with added comments

# Create binary classification model
model = nn.Sequential(
  nn.Linear(6, 4), # First linear layer
  nn.Linear(4, 1), # Second linear layer
  nn.Sigmoid() # Sigmoid activation function
)
Introduction to Deep Learning with PyTorch

Binary classification: forward pass

# Pass input data through model
output = model(input_data)
print(output)
tensor([[0.5188], [0.3761], [0.5015], [0.3718], [0.4663]], 
    grad_fn=<SigmoidBackward0>)
  • Output: five probabilities between 0 and 1, one for each animal

  • Classification (0.5 threshold):

    • Class = 1 (mammal) for values ≥ 0.5 (0.5188, 0.5015)
    • Class = 0 (not mammal) for values < 0.5 (0.3761, 0.3718, 0.4633)
Introduction to Deep Learning with PyTorch

Multi-class classification: forward pass

  • Class 1 - mammal, class 2 - bird, class 3 - reptile
n_classes = 3


# Create multi-class classification model model = nn.Sequential( nn.Linear(6, 4), # First linear layer nn.Linear(4, n_classes), # Second linear layer
nn.Softmax(dim=-1) # Softmax activation )
# Pass input data through model output = model(input_data) print(output.shape)
torch.Size([5, 3])
Introduction to Deep Learning with PyTorch

Multi-class classification: forward pass

multiclass.jpg

  • Each row sums to one
  • Predicted label = class with the highest probability
  • Row 1 = class 1 (mammal), row 2 = class 1 (mammal), row 3 = class 3 (reptile)
Introduction to Deep Learning with PyTorch

Regression: forward pass

# Create regression model
model = nn.Sequential(
  nn.Linear(6, 4), # First linear layer
  nn.Linear(4, 1) # Second linear layer
)

# Pass input data through model
output = model(input_data)

# Return output
print(output)
tensor([[0.3818],
        [0.0712],
        [0.3376],
        [0.0231],
        [0.0757]], 
        grad_fn=<AddmmBackward0>)
Introduction to Deep Learning with PyTorch

Let's practice!

Introduction to Deep Learning with PyTorch

Preparing Video For Download...