What is the purpose of an activation function in a neural network?
An activation function adds non-linearity to the model, allowing it to learn complex patterns and make decisions beyond simple linear relationships.
Click to reveal answer
beginner
Describe the ReLU activation function and its output behavior.
ReLU (Rectified Linear Unit) outputs the input directly if it is positive; otherwise, it outputs zero. It helps models learn faster and reduces the chance of vanishing gradients.
Click to reveal answer
beginner
What is the range of the Sigmoid activation function and why is it useful?
Sigmoid outputs values between 0 and 1, making it useful for models that predict probabilities or binary outcomes.
Click to reveal answer
intermediate
Explain the Softmax activation function and when it is typically used.
Softmax converts a vector of numbers into probabilities that sum to 1. It is commonly used in the output layer for multi-class classification problems.
Click to reveal answer
intermediate
What problem does ReLU help to solve compared to Sigmoid or Tanh?
ReLU helps reduce the vanishing gradient problem by allowing gradients to flow when inputs are positive, unlike Sigmoid or Tanh which can squash gradients to near zero.
Click to reveal answer
Which activation function outputs values strictly between 0 and 1?
ASoftmax
BReLU
CLinear
DSigmoid
✗ Incorrect
Sigmoid outputs values between 0 and 1, making it suitable for probability predictions.
What does the ReLU function output when the input is negative?
AThe input value
BNegative of the input
CZero
DOne
✗ Incorrect
ReLU outputs zero for any negative input.
Softmax is mainly used in which part of a neural network?
AOutput layer for multi-class classification
BHidden layers
CInput layer
DOutput layer for regression
✗ Incorrect
Softmax converts outputs into probabilities for multi-class classification in the output layer.
Which activation function can cause the vanishing gradient problem?
ASigmoid
BReLU
CSoftmax
DLeaky ReLU
✗ Incorrect
Sigmoid can squash gradients to near zero, causing vanishing gradients.
What is a key benefit of using ReLU over Sigmoid?
AOutputs probabilities
BNon-linear but faster to compute and reduces vanishing gradients
CAlways outputs positive values
DUsed for multi-class classification
✗ Incorrect
ReLU is simple, fast, and helps reduce vanishing gradients compared to Sigmoid.
Explain how ReLU, Sigmoid, and Softmax activation functions differ in their output and typical use cases.
Think about the output range and where in the network each is used.
You got /3 concepts.
Describe why activation functions are important in neural networks and what would happen without them.
Consider what linear vs non-linear means for learning.
You got /3 concepts.
Practice
(1/5)
1. Which activation function is best suited for the output layer of a binary classification model in PyTorch?
easy
A. Sigmoid
B. ReLU
C. Softmax
D. Tanh
Solution
Step 1: Understand the output requirements for binary classification
Binary classification outputs a probability between 0 and 1 for one class, so the activation must squash values into this range.
Step 2: Match activation functions to output needs
Sigmoid outputs values between 0 and 1, making it ideal for binary classification outputs. ReLU outputs zero or positive values without upper bound, Softmax is for multi-class, and Tanh outputs between -1 and 1.
Final Answer:
Sigmoid -> Option A
Quick Check:
Binary output needs probability = Sigmoid [OK]
Hint: Binary output? Use Sigmoid for probability between 0 and 1 [OK]
Common Mistakes:
Choosing ReLU which doesn't limit output to 0-1
Using Softmax for binary instead of multi-class
Confusing Tanh with Sigmoid output range
2. Which of the following is the correct way to apply the ReLU activation function to a tensor x in PyTorch?
easy
A. torch.activation.relu(x)
B. torch.nn.functional.relu(x)
C. torch.nn.ReLU(x)
D. torch.relu(x)
Solution
Step 1: Recall PyTorch ReLU usage
ReLU can be applied using the functional API or as a module. The functional API call is torch.nn.functional.relu(x).
Step 2: Check each option
torch.relu(x) is incorrect because torch.relu does not exist. torch.activation.relu(x) is invalid syntax. torch.nn.ReLU(x) tries to call the module directly without instantiation. torch.nn.functional.relu(x) correctly uses the functional API.
Final Answer:
torch.nn.functional.relu(x) -> Option B
Quick Check:
Use functional API for ReLU = torch.nn.functional.relu(x) [OK]
Hint: Use torch.nn.functional.relu(x) for direct ReLU call [OK]
Common Mistakes:
Trying to call torch.relu which doesn't exist
Using torch.nn.ReLU without creating an instance
Confusing module and functional API usage
3. What is the output of the following PyTorch code?
import torch
import torch.nn.functional as F
x = torch.tensor([-1.0, 0.0, 2.0])
output = F.relu(x)
print(output.tolist())
medium
A. [-1.0, 0.0, 2.0]
B. [0.0, 1.0, 1.0]
C. [0.0, 0.0, 2.0]
D. [-1.0, 0.0, 0.0]
Solution
Step 1: Understand ReLU behavior on input tensor
ReLU sets negative values to zero and keeps positive values unchanged.
Step 2: Apply ReLU to each element
-1.0 becomes 0.0, 0.0 stays 0.0, 2.0 stays 2.0. So output is [0.0, 0.0, 2.0].
Final Answer:
[0.0, 0.0, 2.0] -> Option C
Quick Check:
ReLU zeroes negatives = [0.0, 0.0, 2.0] [OK]
Hint: ReLU zeroes negatives, keeps positives same [OK]
Common Mistakes:
Leaving negative values unchanged
Confusing ReLU with Sigmoid output
Assuming ReLU changes zeros to ones
4. The following code aims to apply Softmax to a tensor x along dimension 1 but throws an error. What is the error and how to fix it?
import torch
import torch.nn.functional as F
x = torch.tensor([[1.0, 2.0, 3.0], [1.0, 2.0, 3.0]])
output = F.softmax(x, dim=2)
print(output)
medium
A. Missing activation import; fix by importing torch.nn
B. Softmax requires 1D tensor; fix by flattening x
C. Softmax cannot be applied to float tensors; convert to int
D. Dimension 2 is out of range for tensor of shape (2,3); fix by using dim=1
Solution
Step 1: Check tensor shape and dimension argument
Tensor x has shape (2, 3), so valid dims are 0 and 1. Using dim=2 is invalid and causes an error.
Step 2: Correct the dimension argument
To apply Softmax across columns (features), use dim=1. This fixes the error.
Final Answer:
Dimension 2 is out of range for tensor of shape (2,3); fix by using dim=1 -> Option D
Quick Check:
Tensor shape (2,3) means max dim=1 [OK]
Hint: Check tensor shape before choosing dim for Softmax [OK]
Common Mistakes:
Using invalid dimension index for Softmax
Thinking Softmax needs 1D input only
Ignoring error message about dimension out of range
5. You have a multi-class classification problem with 4 classes. Which activation function should you use in the output layer and why?
Choose the best option.
hard
A. Softmax, because it outputs probabilities that sum to 1 across all classes
B. Sigmoid, because it outputs probabilities for each class independently
C. Tanh, because it outputs values between -1 and 1 for better gradients
D. ReLU, because it outputs positive values for all classes
For multi-class classification, the output layer should produce probabilities for each class that sum to 1, representing a valid probability distribution.
Step 2: Match activation functions to multi-class needs
Softmax converts raw scores into probabilities that sum to 1 across classes. ReLU does not produce probabilities, Sigmoid treats classes independently (not summing to 1), and Tanh outputs values between -1 and 1, not probabilities.
Final Answer:
Softmax, because it outputs probabilities that sum to 1 across all classes -> Option A