Complete the code to define a custom loss function that calculates mean squared error.
def custom_loss(y_true, y_pred): return tf.reduce_mean(tf.square(y_true [1] y_pred))
The mean squared error loss calculates the average of the squared differences between true and predicted values. We subtract y_pred from y_true before squaring.
Complete the code to define a custom loss function that returns the mean absolute error.
def custom_loss(y_true, y_pred): return tf.reduce_mean(tf.abs(y_true [1] y_pred))
Mean absolute error calculates the average of the absolute differences between true and predicted values. We subtract y_pred from y_true before taking the absolute value.
Fix the error in the custom loss function that calculates mean squared logarithmic error.
def custom_loss(y_true, y_pred): return tf.reduce_mean(tf.square(tf.math.log1p(y_true) [1] tf.math.log1p(y_pred)))
Mean squared logarithmic error calculates the average squared difference of the log-transformed true and predicted values. We subtract the logs before squaring.
Fill both blanks to create a custom loss function that calculates weighted mean squared error.
def custom_loss(y_true, y_pred, weights): error = y_true [1] y_pred weighted_error = tf.multiply(tf.square(error), [2]) return tf.reduce_mean(weighted_error)
Weighted mean squared error multiplies the squared error by weights before averaging. We subtract y_pred from y_true to get the error, square it, then multiply by weights.
Fill all three blanks to define a custom loss function that combines mean squared error and mean absolute error with a balancing factor.
def custom_loss(y_true, y_pred, alpha): mse = tf.reduce_mean(tf.square(y_true [1] y_pred)) mae = tf.reduce_mean(tf.abs(y_true [2] y_pred)) return [3] * mse + (1 - [3]) * mae
This custom loss combines mean squared error (MSE) and mean absolute error (MAE) using a balancing factor alpha. Both errors use subtraction between true and predicted values.
