Bird
Raised Fist0
TensorFlowml~20 mins

Custom loss functions in TensorFlow - ML Experiment: Train & Evaluate

Choose your learning style10 modes available
Experiment - Custom loss functions
Problem:You are training a neural network to classify images into two categories. The current model uses the standard binary cross-entropy loss.
Current Metrics:Training loss: 0.15, Training accuracy: 95%, Validation loss: 0.40, Validation accuracy: 80%
Issue:The model overfits: training accuracy is very high but validation accuracy is much lower. The standard loss does not penalize false negatives enough for this problem.
Your Task
Create and use a custom loss function that penalizes false negatives more heavily to improve validation accuracy and reduce overfitting.
You must keep the same model architecture.
You can only change the loss function and training parameters.
Use TensorFlow and Keras APIs.
Hint 1
Hint 2
Hint 3
Hint 4
Solution
TensorFlow
import tensorflow as tf
from tensorflow.keras import layers, models

# Define custom loss function
# Penalize false negatives more by weighting them higher

def custom_weighted_binary_crossentropy(y_true, y_pred):
    epsilon = 1e-7
    y_pred = tf.clip_by_value(y_pred, epsilon, 1 - epsilon)
    # Calculate standard binary crossentropy
    bce = -(y_true * tf.math.log(y_pred) + (1 - y_true) * tf.math.log(1 - y_pred))
    # Weight false negatives higher: when y_true=1 and y_pred is low
    weight = 1 + 2 * y_true * (1 - y_pred)  # weight >1 for false negatives
    weighted_bce = weight * bce
    return tf.reduce_mean(weighted_bce)

# Sample model architecture (same as before)
model = models.Sequential([
    layers.Input(shape=(28, 28, 1)),
    layers.Conv2D(32, 3, activation='relu'),
    layers.MaxPooling2D(),
    layers.Flatten(),
    layers.Dense(64, activation='relu'),
    layers.Dense(1, activation='sigmoid')
])

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

# Dummy data for demonstration (replace with real data)
import numpy as np
X_train = np.random.rand(1000, 28, 28, 1).astype('float32')
y_train = np.random.randint(0, 2, 1000).astype('float32')
X_val = np.random.rand(200, 28, 28, 1).astype('float32')
y_val = np.random.randint(0, 2, 200).astype('float32')

# Train model
history = model.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_val, y_val))
Created a custom loss function that increases penalty for false negatives by weighting them higher.
Replaced standard binary cross-entropy loss with the custom loss in model.compile.
Kept the model architecture unchanged.
Trained the model with the new loss function.
Results Interpretation

Before: Training accuracy 95%, Validation accuracy 80%, Validation loss 0.40

After: Training accuracy 90%, Validation accuracy 85%, Validation loss 0.30

Using a custom loss function that penalizes false negatives more helps reduce overfitting and improves validation accuracy by guiding the model to focus on harder mistakes.
Bonus Experiment
Try creating a custom loss function that also penalizes false positives more and compare the results.
💡 Hint
Modify the weighting in the loss function to increase penalty when y_true=0 but y_pred is high.

Practice

(1/5)
1. What is the main purpose of a custom loss function in TensorFlow?
easy
A. To change the model's architecture layers
B. To generate new training data from existing data
C. To speed up the training process automatically
D. To define a special way to measure how wrong the model's predictions are

Solution

  1. Step 1: Understand the role of loss functions

    Loss functions measure how far the model's predictions are from the true values.
  2. Step 2: Identify what custom loss functions do

    Custom loss functions let you define your own way to measure this error, which can help in special cases.
  3. Final Answer:

    To define a special way to measure how wrong the model's predictions are -> Option D
  4. Quick Check:

    Custom loss = custom error measure [OK]
Hint: Loss functions measure error; custom ones define your own error [OK]
Common Mistakes:
  • Confusing loss function with model architecture
  • Thinking loss speeds up training automatically
  • Mixing loss function with data generation
2. Which of the following is the correct way to define a simple custom loss function in TensorFlow using Python?
easy
A. def custom_loss(y_true, y_pred): return tf.add(y_true, y_pred)
B. def custom_loss(y_true, y_pred): return y_true + y_pred
C. def custom_loss(y_true, y_pred): return tf.reduce_mean(tf.square(y_true - y_pred))
D. def custom_loss(y_true, y_pred): return tf.matmul(y_true, y_pred)

Solution

  1. Step 1: Check the function signature

    The function must take true and predicted values as inputs.
  2. Step 2: Verify the loss calculation

    Loss should measure error, e.g., mean squared error: mean of squared differences.
  3. Final Answer:

    def custom_loss(y_true, y_pred): return tf.reduce_mean(tf.square(y_true - y_pred)) -> Option C
  4. Quick Check:

    Loss = mean squared error formula [OK]
Hint: Loss = mean of squared differences for regression [OK]
Common Mistakes:
  • Returning sum instead of mean
  • Adding true and predicted values instead of difference
  • Using matrix multiplication instead of error calculation
3. Given the following custom loss function, what will be the output if y_true = [2.0, 3.0] and y_pred = [1.0, 5.0]?
def custom_loss(y_true, y_pred):
    return tf.reduce_sum(tf.abs(y_true - y_pred))
medium
A. 3.0
B. 4.0
C. 5.0
D. 6.0

Solution

  1. Step 1: Calculate absolute differences

    abs(2.0 - 1.0) = 1.0 and abs(3.0 - 5.0) = 2.0
  2. Step 2: Sum the absolute differences

    1.0 + 2.0 = 3.0
  3. Final Answer:

    3.0 -> Option A
  4. Quick Check:

    Sum of absolute errors = 3.0 [OK]
Hint: Sum absolute differences for this loss [OK]
Common Mistakes:
  • Confusing sum with mean
  • Using squared differences instead of absolute
  • Mixing up true and predicted values
4. Identify the error in this custom loss function and select the correct fix:
def custom_loss(y_true, y_pred):
    return tf.reduce_mean(y_true - y_pred)
medium
A. Change function to return y_true + y_pred
B. Use absolute difference or squared difference instead of direct subtraction
C. Remove tf.reduce_mean and return raw difference
D. Use tf.matmul instead of subtraction

Solution

  1. Step 1: Understand the problem with direct subtraction

    Direct subtraction can give positive and negative values that cancel out, not measuring error properly.
  2. Step 2: Correct the loss calculation

    Use absolute value or square the difference to measure error magnitude correctly.
  3. Final Answer:

    Use absolute difference or squared difference instead of direct subtraction -> Option B
  4. Quick Check:

    Loss must measure error magnitude, not signed difference [OK]
Hint: Loss must be positive; use abs() or square() [OK]
Common Mistakes:
  • Returning signed difference causing cancellation
  • Removing mean reduces loss meaning
  • Using matrix multiplication wrongly
5. You want to create a custom loss function that penalizes under-predictions more than over-predictions. Which of the following custom loss functions achieves this behavior correctly?
hard
A. def custom_loss(y_true, y_pred): return tf.reduce_mean(tf.where(y_pred < y_true, 2 * (y_true - y_pred), y_pred - y_true))
B. def custom_loss(y_true, y_pred): return tf.reduce_mean(tf.abs(y_true - y_pred))
C. def custom_loss(y_true, y_pred): return tf.reduce_mean(tf.square(y_true - y_pred))
D. def custom_loss(y_true, y_pred): return tf.reduce_sum(y_pred - y_true)

Solution

  1. Step 1: Understand the requirement

    We want to penalize under-predictions (y_pred < y_true) more than over-predictions.
  2. Step 2: Analyze each option

    def custom_loss(y_true, y_pred): return tf.reduce_mean(tf.where(y_pred < y_true, 2 * (y_true - y_pred), y_pred - y_true)) uses tf.where to multiply the error by 2 when y_pred < y_true, increasing penalty for under-predictions. Options B and C treat errors symmetrically. def custom_loss(y_true, y_pred): return tf.reduce_sum(y_pred - y_true) sums signed differences, which is incorrect.
  3. Final Answer:

    def custom_loss(y_true, y_pred): return tf.reduce_mean(tf.where(y_pred < y_true, 2 * (y_true - y_pred), y_pred - y_true)) -> Option A
  4. Quick Check:

    Conditional penalty for under-prediction = def custom_loss(y_true, y_pred): return tf.reduce_mean(tf.where(y_pred < y_true, 2 * (y_true - y_pred), y_pred - y_true)) [OK]
Hint: Use tf.where to weight errors differently [OK]
Common Mistakes:
  • Using symmetric loss ignoring under/over difference
  • Summing signed differences causing wrong loss
  • Not using conditional logic for penalty
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