Introduction

Welcome back to our course on the MLP architecture: activations & initialization! You're making excellent progress, having now completed two lessons in which we built a flexible MLP architecture and implemented the powerful ReLU activation function.

In this third lesson, we'll focus on a critical aspect of neural networks: output layer activation functions. While we've been using activation functions in the hidden layers to introduce non-linearity and enhance the network's learning capabilities, the activation function in the output layer serves a different purpose. The output layer activation function determines the type of prediction your network can make, and choosing the appropriate one is essential for your model's success.

We'll explore two key output activation functions:

  1. Softmax: For multi-class classification problems, converting raw outputs into probabilities.
  2. Linear: For regression problems, allowing the model to predict unbounded continuous values.

By the end of this lesson, you'll understand when and why to use these activation functions, implement them efficiently, and apply them in different neural network architectures for classification and regression tasks.

Understanding Output Layer Activation Functions

The activation function in the output layer plays a fundamentally different role compared to those in hidden layers. While hidden layer activations primarily introduce non-linearity to help the network learn complex patterns, output layer activations transform the network's raw outputs into the desired format for your specific task.

The choice of output activation depends on the type of problem you're solving:

  • Classification problems: We need outputs that represent probabilities or confidence scores.
    • Binary classification: Sigmoid activation (which we've already implemented) squashes values to the range [0, 1]. This means the output can be interpreted as the probability of the input belonging to the positive class, making it easy to set a threshold (like 0.5) for decision-making.
    • Multi-class classification: Softmax activation converts raw scores into a probability distribution across all classes. Each output neuron represents a class, and the softmax ensures the outputs sum to 1, so you can directly interpret them as the model's confidence in each class.
  • Regression problems: We need to predict continuous unbounded values.
    • Linear activation (or no activation) preserves the raw output of the network. This allows the network to predict any real-valued number, which is essential for tasks where the target variable is continuous and unbounded, such as predicting prices or measurements.

Understanding this distinction is crucial because using the wrong output activation can lead to poor model performance, even if the rest of your network architecture is sound. For example, using a sigmoid activation for regression would limit your predictions to the range [0, 1], which would be problematic if you're trying to predict values like house prices or temperatures.

Let's implement these output activation functions and see how they transform our MLP's capabilities.

The Softmax Activation Function
The Linear Activation Function
Enhancing Our DenseLayer Class
Building Multi-Class Classification Networks: Architecture
Building Multi-Class Classification Networks: Output Interpretation

Now, let's pass our sample data through the network and examine the output:

# Forward propagate through the network
out_softmax <- mlp_softmax$forward(X_clf)
cat("Output (Softmax) (shape", paste(dim(out_softmax), collapse = " x "), "):\n")
print(out_softmax)
cat("Sum of probs per sample:", rowSums(out_softmax), "\n")

The output shows the probability distribution across our three classes for each of the two input samples:

Output (Softmax) (shape 2 x 3):
     [,1]      [,2]      [,3]
[1,] 0.33334939 0.33380076 0.33284985
[2,] 0.33333349 0.33340775 0.33325876
Sum of probs per sample: 1 1

Notice two important aspects:

  1. Each output value is between 0 and 1.
  2. The sum of probabilities for each sample is exactly 1, confirming that softmax produces a valid probability distribution.

This example uses random initial weights, so the model hasn't been trained yet — that's why the probabilities are roughly equal across all classes. After training, we would expect the model to assign higher probabilities to the correct classes.

Building Regression Networks: Architecture
Building Regression Networks: Output Interpretation

Let's pass our sample data through the network and examine the output:

# Forward propagate through the network
out_linear <- mlp_linear$forward(X_reg)
cat("Output (Linear) (shape", paste(dim(out_linear), collapse = " x "), "):\n")
print(out_linear)

The output is a single unbounded value for our input sample:

Output (Linear) (shape 1 x 1):
     [,1]
[1,] 0.01877798

Unlike the softmax output, this value is not constrained to any specific range. It could be any real number, positive or negative, depending on the network's weights and the input data. This is precisely what we want for regression problems — the ability to predict any value on the real number line.

Conclusion and Next Steps

Excellent work! You've now expanded your neural network toolkit with two crucial output layer activation functions: softmax for multi-class classification and linear for regression tasks. We've seen how these different activations enable your networks to produce either probability distributions or unbounded continuous values, depending on your specific prediction needs. The ability to choose the right output activation is a fundamental skill that will help you design effective neural networks for a wide range of real-world problems.

In the upcoming practice section, you'll have the opportunity to solidify your understanding by implementing and experimenting with these activation functions. Following this practice, our next lesson will focus on weight initialization strategies — a crucial aspect that can significantly impact how quickly and effectively your neural networks learn. Proper initialization can mean the difference between a model that learns efficiently and one that struggles to converge, so this will be an important addition to your deep learning toolkit.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal