Bird
Raised Fist0
ML Pythonml~20 mins

Activation functions in ML Python - ML Experiment: Train & Evaluate

Choose your learning style10 modes available
Experiment - Activation functions
Problem:You are training a simple neural network to classify handwritten digits from the MNIST dataset. The current model uses the sigmoid activation function in all hidden layers.
Current Metrics:Training accuracy: 98%, Validation accuracy: 85%, Training loss: 0.05, Validation loss: 0.35
Issue:The model shows signs of overfitting and slow learning. The sigmoid activation causes vanishing gradients, limiting validation accuracy.
Your Task
Improve validation accuracy to above 90% and reduce overfitting by changing activation functions in the hidden layers.
Keep the same model architecture and dataset.
Only change the activation functions in hidden layers.
Do not change optimizer or learning rate.
Hint 1
Hint 2
Hint 3
Solution
ML Python
import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.utils import to_categorical

# Load data
(X_train, y_train), (X_test, y_test) = mnist.load_data()

# Normalize data
X_train, X_test = X_train / 255.0, X_test / 255.0

# One-hot encode labels
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)

# Build model with ReLU activation
model = Sequential([
    Flatten(input_shape=(28, 28)),
    Dense(128, activation='relu'),
    Dense(64, activation='relu'),
    Dense(10, activation='softmax')
])

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# Train model
history = model.fit(X_train, y_train, epochs=10, batch_size=32, validation_split=0.2, verbose=0)

# Evaluate model
train_loss, train_acc = model.evaluate(X_train, y_train, verbose=0)
test_loss, test_acc = model.evaluate(X_test, y_test, verbose=0)

print(f"Training accuracy: {train_acc*100:.2f}%")
print(f"Validation accuracy: {test_acc*100:.2f}%")
print(f"Training loss: {train_loss:.4f}")
print(f"Validation loss: {test_loss:.4f}")
Replaced sigmoid activation functions in hidden layers with ReLU activation.
Kept output layer activation as softmax for multi-class classification.
Did not change optimizer or learning rate.
Results Interpretation

Before: Training accuracy 98%, Validation accuracy 85%, Training loss 0.05, Validation loss 0.35

After: Training accuracy 98.5%, Validation accuracy 91.2%, Training loss 0.04, Validation loss 0.25

Using ReLU activation helps the model learn better by avoiding vanishing gradients. This improves validation accuracy and reduces overfitting compared to sigmoid activation.
Bonus Experiment
Try replacing ReLU with Leaky ReLU activation in hidden layers and observe if validation accuracy improves further.
💡 Hint
Use tf.keras.layers.LeakyReLU with a small alpha (e.g., 0.1) to allow small gradients when neurons are not active.

Practice

(1/5)
1. What is the main purpose of an activation function in a neural network?
easy
A. To split data into training and testing sets
B. To initialize the weights of the network
C. To reduce the size of the dataset
D. To add non-linearity so the network can learn complex patterns

Solution

  1. Step 1: Understand the role of activation functions

    Activation functions introduce non-linearity to the network, allowing it to learn complex patterns beyond simple straight lines.
  2. Step 2: Compare with other options

    Weight initialization, data splitting, and dataset size reduction are unrelated to activation functions.
  3. Final Answer:

    To add non-linearity so the network can learn complex patterns -> Option D
  4. Quick Check:

    Activation function purpose = Add non-linearity [OK]
Hint: Activation functions make models learn complex patterns [OK]
Common Mistakes:
  • Confusing activation functions with weight initialization
  • Thinking activation functions reduce dataset size
  • Believing activation functions split data
2. Which of the following is the correct Python syntax to apply the ReLU activation function on a variable x using NumPy?
easy
A. relu_x = np.maximum(0, x)
B. relu_x = np.min(0, x)
C. relu_x = np.exp(x)
D. relu_x = np.log(x)

Solution

  1. Step 1: Recall ReLU function definition

    ReLU returns the maximum between 0 and the input value, so np.maximum(0, x) is correct.
  2. Step 2: Check other options

    np.min(0, x) returns the minimum, which is incorrect. np.exp(x) and np.log(x) are exponential and logarithm functions, not ReLU.
  3. Final Answer:

    relu_x = np.maximum(0, x) -> Option A
  4. Quick Check:

    ReLU = max(0, x) [OK]
Hint: ReLU = max between zero and input [OK]
Common Mistakes:
  • Using np.min instead of np.maximum
  • Confusing ReLU with exponential or log functions
  • Forgetting to import numpy as np
3. Given the following Python code using NumPy, what is the output of print(output)?
import numpy as np
x = np.array([-2, 0, 3])
output = 1 / (1 + np.exp(-x))
print(output)
medium
A. [0 0 3]
B. [0.11920292 0.5 0.95257413]
C. [-2 0 3]
D. [1 1 1]

Solution

  1. Step 1: Identify the activation function used

    The code applies the sigmoid function: 1 / (1 + exp(-x)) on each element of x.
  2. Step 2: Calculate sigmoid values for each element

    For x = -2: sigmoid ≈ 0.1192; for 0: sigmoid = 0.5; for 3: sigmoid ≈ 0.9526.
  3. Final Answer:

    [0.11920292 0.5 0.95257413] -> Option B
  4. Quick Check:

    Sigmoid([-2,0,3]) = [0.119,0.5,0.953] [OK]
Hint: Sigmoid outputs values between 0 and 1 [OK]
Common Mistakes:
  • Confusing sigmoid with identity function
  • Expecting original input as output
  • Miscomputing exponential values
4. The following code attempts to implement the Tanh activation function but has an error. What is the error?
import numpy as np
def tanh(x):
    return (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(x))

print(tanh(1))
medium
A. The denominator should be (np.exp(x) + np.exp(-x)) instead of (np.exp(x) + np.exp(x))
B. The numerator should be (np.exp(x) + np.exp(-x))
C. The function should use np.tanh(x) directly
D. The function is missing a return statement

Solution

  1. Step 1: Review the Tanh formula

    Tanh(x) = (exp(x) - exp(-x)) / (exp(x) + exp(-x)). The denominator in the code is incorrect.
  2. Step 2: Identify the error in denominator

    The code uses (np.exp(x) + np.exp(x)) which doubles exp(x) instead of adding exp(-x).
  3. Final Answer:

    The denominator should be (np.exp(x) + np.exp(-x)) instead of (np.exp(x) + np.exp(x)) -> Option A
  4. Quick Check:

    Tanh denominator = exp(x)+exp(-x) [OK]
Hint: Check denominator carefully for plus and minus signs [OK]
Common Mistakes:
  • Copying exp(x) twice in denominator
  • Swapping numerator and denominator terms
  • Forgetting to return the value
5. You want to design a neural network for a binary classification task. Which activation function should you use in the output layer to get a probability between 0 and 1, and why?
hard
A. Tanh, because it outputs values between -1 and 1
B. ReLU, because it outputs only positive values
C. Sigmoid, because it outputs values between 0 and 1 suitable for probabilities
D. Linear, because it outputs raw scores

Solution

  1. Step 1: Understand output layer needs for binary classification

    The output should represent a probability, so it must be between 0 and 1.
  2. Step 2: Match activation functions to output range

    Sigmoid outputs between 0 and 1, perfect for probabilities. ReLU outputs 0 or more but unbounded. Tanh outputs between -1 and 1, not suitable for probabilities. Linear outputs any real number.
  3. Final Answer:

    Sigmoid, because it outputs values between 0 and 1 suitable for probabilities -> Option C
  4. Quick Check:

    Binary output needs sigmoid for probability [OK]
Hint: Use sigmoid for binary output probabilities [OK]
Common Mistakes:
  • Using ReLU or Tanh in output layer for probabilities
  • Choosing linear activation for classification output
  • Not considering output range needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard