Challenge - 5 Problems
Custom Loss Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of a simple custom loss function
What is the output of the following code snippet when computing the loss for y_true = [1.0, 0.0] and y_pred = [0.8, 0.3]?
TensorFlow
import tensorflow as tf def custom_loss(y_true, y_pred): return tf.reduce_mean(tf.square(y_true - y_pred)) loss_value = custom_loss(tf.constant([1.0, 0.0]), tf.constant([0.8, 0.3])) print(loss_value.numpy())
Attempts:
2 left
💡 Hint
Recall that mean squared error is the average of squared differences.
✗ Incorrect
The squared differences are (1.0-0.8)^2 = 0.04 and (0.0-0.3)^2 = 0.09. Their mean is (0.04 + 0.09)/2 = 0.065. The printed output is 0.065.
❓ Model Choice
intermediateChoosing a custom loss for imbalanced classification
You want to train a model on a dataset where positive examples are very rare. Which custom loss function modification helps the model focus more on positive examples?
Attempts:
2 left
💡 Hint
Think about how to make the model pay more attention to rare classes.
✗ Incorrect
Weighting the loss more for positive class errors helps the model learn better on rare positive examples.
❓ Hyperparameter
advancedEffect of a hyperparameter in a custom loss function
Consider a custom loss function: loss = alpha * mean_squared_error + (1 - alpha) * mean_absolute_error. What is the effect of increasing alpha from 0.3 to 0.7?
Attempts:
2 left
💡 Hint
Recall the difference between mean squared and mean absolute errors.
✗ Incorrect
Increasing alpha gives more weight to mean squared error, which penalizes large errors more than mean absolute error.
🔧 Debug
advancedIdentify the error in this custom loss function
What error does the following custom loss function code produce when used in model training?
```python
def custom_loss(y_true, y_pred):
return tf.reduce_sum(y_true - y_pred)
```
Attempts:
2 left
💡 Hint
Loss functions should be non-negative and represent error magnitude.
✗ Incorrect
Using sum of differences can be negative, which is not a valid loss and can cause training issues.
🧠 Conceptual
expertWhy use custom loss functions in deep learning?
Which of the following is the best reason to create a custom loss function instead of using built-in ones?
Attempts:
2 left
💡 Hint
Think about why standard losses might not fit all problems.
✗ Incorrect
Custom losses allow adapting training to unique needs like special error costs or data quirks.
