top of page
Gradient With Circle
Image by Nick Morrison

Insights Across Technology, Software, and AI

Discover articles across technology, software, and AI. From core concepts to modern tech and practical implementations.

Mean Squared Logarithmic Error (MSLE) in Machine Learning: Formula, Properties, and Python Implementation

  • 15 minutes ago
  • 9 min read

Regression models are not all evaluated most effectively with the same loss function. Mean Squared Error (MSE) and Mean Absolute Error (MAE) measure prediction error directly in the original target scale, but this can be less suitable when target values span a wide range or when relative differences matter more than absolute differences. Mean Squared Logarithmic Error (MSLE) addresses this by applying a logarithmic transformation to the target and prediction before measuring squared error. It is a regression loss and evaluation metric that measures discrepancies in logarithmic space rather than treating raw numerical differences as the primary quantity of error. Scikit-learn defines MSLE as the expected squared logarithmic error and provides it as a regression loss metric.


In this article, we examine the definition of Mean Squared Logarithmic Error, its mathematical formulation, and the way its logarithmic transformation changes regression loss behavior. We will also discuss its mathematical properties, interpretation, advantages, limitations, and the conditions that make MSLE appropriate for a regression problem. Finally, we will implement MSLE in Python with NumPy and scikit-learn, followed by a Keras implementation for neural network training.


Mean Squared Logarithmic Error (MSLE) in Machine Learning

What Is Mean Squared Logarithmic Error (MSLE)?

Mean Squared Logarithmic Error, commonly abbreviated as MSLE, is a regression loss that measures the average squared difference between the logarithm of the true target values and the logarithm of the predicted values after adding one to both quantities. The addition of one makes zero a valid input because log(1) is defined as zero. The standard formulation uses the natural logarithm. Scikit-learn defines MSLE as the mean squared difference between log(1 + y_true) and log(1 + y_pred). For n observations, MSLE is:


Mean Squared Logarithmic Error(MSLE)

Here, yi represents the true target, ŷi represents the corresponding prediction, and n is the number of observations. MSLE is non-negative because each logarithmic error is squared. A value of zero represents exact agreement between the transformed targets and predictions.


The key distinction between MSLE and ordinary squared-error losses is the scale on which

the discrepancy is measured. MSE is defined as:


mean square error (mse)_

MSLE instead measures the squared difference after the logarithmic transformation. Because the logarithm compresses large numerical values, large absolute deviations do not dominate the loss to the same degree as they can under MSE. As a result, MSLE is particularly useful when proportional or relative differences are more important than raw absolute differences. Scikit-learn specifically identifies targets with exponential growth as a suitable setting for this metric.


MSLE VS MSE

This plot compares how Mean Squared Logarithmic Error (MSLE) and Mean Squared Error (MSE) penalize prediction errors for a true target of y = 10. While MSE forms a symmetric parabola that penalizes errors quadratically regardless of direction, MSLE scales logarithmically to flatten penalty growth for over-predictions while heavily penalizing under-predictions (ŷ < 10). This demonstrates MSLE's core advantages: measuring relative rather than absolute deviation, resisting large-scale outlier distortion, and enforcing an asymmetric penalty ideal for scenarios where underestimating costs more than overestimating.


MSLE can therefore be understood as a scale-aware regression criterion. Instead of asking only how far a prediction is from the target numerically, it asks how far the prediction is from the target after both have been represented on a logarithmic scale.


Mathematical Formulation and Behavior of MSLE

The Mean Squared Logarithmic Error (MSLE) formula performs three main operations: logarithmic transformation, subtraction, and squaring. First, the actual and predicted values are transformed using log(1 + x). Their transformed values are then compared, the resulting difference is squared, and the squared logarithmic errors are averaged across all observations. The standard MSLE formula is:


MSLE = ( 1 / n ) × Σ [ log ( 1 + yi ) - log ( 1 + ŷi ) ] ²


Here, yi represents the actual target value, ŷi represents the predicted value, and n represents the total number of observations.


The structure of the MSLE formula has several important consequences. First, MSLE is always non-negative because the logarithmic difference is squared. Its minimum value is zero, which occurs when the predicted values are identical to the corresponding actual values. Second, the logarithmic transformation compresses the scale of large target values. As a result, the loss is less dominated by large absolute errors than MSE and places greater emphasis on differences expressed on a relative or multiplicative scale.


The logarithmic transformation also changes how prediction deviations are weighted. Because the logarithm is nonlinear, an equal absolute change in two different regions of the target scale does not necessarily produce the same change after transformation. This allows MSLE to evaluate errors according to their logarithmic distance rather than their raw numerical distance. Scikit-learn also notes that MSLE penalizes under-predicted values more strongly than over-predicted values for comparable deviations, which results from the asymmetric behavior introduced by the logarithmic transformation.


The effect of the logarithmic transformation becomes clearer when considering the derivative of the loss. For a single observation, the individual MSLE loss is:


Li = [ log (1 + yi ) - log ( 1 + ŷi ) ] ²


Differentiating this expression with respect to the predicted value ŷi gives:


∂Li / ∂ŷi = 2 × [ log ( 1 + ŷi ) - log ( 1 + yi ) ] / ( 1 + ŷi )


This derivative describes how the loss changes as the prediction changes. The denominator, 1 + ŷi, makes the gradient dependent on the prediction scale. As the predicted value becomes larger, this denominator also increases, reducing the magnitude of the gradient for a given logarithmic difference.


This behavior distinguishes MSLE from Mean Squared Error. For MSE, the derivative with respect to the prediction is:


∂Li / ∂ŷi = 2 × ( ŷi - yi )


The MSE gradient depends directly on the raw prediction error, while the MSLE gradient depends on the logarithmic prediction error and the predicted-value scale. Consequently, the two losses can produce substantially different optimization behavior even when they are applied to the same regression problem.


Another important property of MSLE is its mathematical domain. The logarithmic component log(1 + x), the value inside the logarithm must be greater than zero .i.e. 1 + x > 0

and therefore x > -1.


The logarithmic transformation also means that MSLE is particularly suited to objectives where relative differences are more informative than absolute differences. The transformation reduces the numerical influence of very large values while retaining meaningful differences between smaller values. This property is one of the main reasons MSLE is used for regression problems involving highly skewed or rapidly increasing target distributions.


Root Mean Squared Logarithmic Error, commonly abbreviated as RMSLE, is derived by taking the square root of MSLE:


RMSLE = √MSLE


Using the full formulation:


RMSLE = √ [ ( 1 / n ) × Σ [ log ( 1 + yi ) - log ( 1 + ŷi ) ] ² ]


Taking the square root converts the squared logarithmic error back to the logarithmic error scale. RMSLE is therefore related directly to MSLE but can provide a more interpretable magnitude on the transformed error scale. Scikit-learn provides RMSLE as a separate regression metric for evaluating prediction performance.


When to Use MSLE: Advantages and Limitations

The choice of a regression loss should follow the objective of the prediction task. A suitable loss function should reflect the type of prediction error that the model is expected to minimize. Mean Squared Logarithmic Error (MSLE) is particularly appropriate when the target values are non-negative and relative differences are more important than absolute differences. Rather than evaluating errors directly on the original scale, MSLE measures the difference after applying a logarithmic transformation to both the true and predicted values.


A major advantage of MSLE is its reduced sensitivity to the raw scale of large target values.

The logarithmic transformation compresses the range of the data before the errors are squared. This prevents very large target values from dominating the loss to the same extent that they can under Mean Squared Error (MSE). As a result, MSLE can be useful for regression problems involving strongly skewed or rapidly increasing target values, particularly when proportional accuracy is more meaningful than absolute numerical distance.


The logarithmic transformation provides several important properties:


  • Large target values are compressed before the error is calculated.

  • Relative differences can have greater importance than raw absolute differences.

  • Non-negative targets across a wide numerical range can be evaluated on a more compressed scale.

  • MSLE can be suitable for targets that exhibit exponential or rapidly increasing behavior.


MSLE can also be useful when the target distribution spans several orders of magnitude.

The logarithmic function grows much more slowly than the original value.

Because of this slower growth, the transformed representation reduces the influence of extremely large target values. This allows prediction differences to be evaluated across different scales without allowing the largest numerical values to disproportionately determine the overall loss.


Another useful property of MSLE is its treatment of zero-valued targets. The formula uses log(1 + x), when x = 0, the transformation becomes:


log ( 1 + 0 ) = log ( 1 ) = 0


This means that zero can be processed directly without encountering the undefined log(0) operation that would occur with a conventional logarithmic transformation.


Despite these advantages, MSLE is not a universal replacement for MSE or Mean Absolute Error (MAE). Its logarithmic formulation imposes restrictions on the valid input values. Since the argument of the logarithm must be greater than zero .i.e. 1 + x > 0. Therefore x > -1.

The main limitations of MSLE can therefore be summarized as follows:


  • It is unsuitable for target or prediction values less than or equal to -1.

  • It may not reflect the desired objective when absolute error is more important than relative error.

  • Its numerical score cannot be directly compared with MSE or MAE because each metric measures error differently.

  • Its logarithmic transformation can make the resulting loss less intuitive when results need to be interpreted in the original target units.


MSLE should therefore be selected according to the definition of accuracy required by the machine learning problem. A smaller MSLE value is not inherently better than a larger MSE or MAE value because these metrics operate on different scales and represent different notions of prediction error.


Finally, MSLE should be interpreted in logarithmic space. A low MSLE indicates that the predicted values are close to the actual values after logarithmic transformation. It does not necessarily mean that the absolute differences between predictions and targets are small in their original units. This distinction is important when comparing regression models, selecting hyperparameters, or communicating model performance.


Overall, MSLE is most appropriate when the regression objective is centered on relative accuracy, non-negative targets, and prediction behavior across a broad or rapidly growing numerical range. Its advantages come directly from the logarithmic transformation, but the same transformation also defines its limitations and determines when it should be preferred over MSE or MAE.


Python Implementation of MSLE

Scikit-learn provides mean_squared_log_error for computing MSLE directly. The function accepts true targets and predictions and returns a non-negative regression loss. It also supports sample weights and multi-output regression.

from sklearn.metrics import mean_squared_log_error

# y_true: true target values
# y_pred: model predictions

msle = mean_squared_log_error(y_true, y_pred)

print("MSLE:", msle)

For a direct implementation of the mathematical definition, NumPy can be used with log1p, which computes log⁡(1+x)\log(1+x):

import numpy as np

def mean_squared_logarithmic_error(y_true, y_pred):
    y_true = np.asarray(y_true, dtype=float)
    y_pred = np.asarray(y_pred, dtype=float)

    if y_true.shape != y_pred.shape:
        raise ValueError("y_true and y_pred must have the same shape.")

    if np.any(y_true <= -1) or np.any(y_pred <= -1):
        raise ValueError(
            "MSLE requires y_true and y_pred to be greater than -1."
        )

    return np.mean(
        (np.log1p(y_true) - np.log1p(y_pred)) ** 2
    )

This implementation follows the definition directly: transform both arrays, compute the squared logarithmic difference, and average the result. np.log1p is preferable to explicitly calculating np.log(1 + x) because it is designed for accurate computation when xx is close to zero.

MSLE is also available as a built-in regression loss in Keras. Keras lists MeanSquaredLogarithmicError among its regression losses, and its loss API is designed to provide quantities that a model minimizes during training.

import keras

msle_loss = keras.losses.MeanSquaredLogarithmicError()

model.compile(
    optimizer="adam",
    loss=msle_loss
)

When MSLE is used as the training objective, the optimization process attempts to reduce prediction discrepancies in logarithmic target space. When it is used for evaluation, it measures model performance according to the same transformed notion of error.

Mean Squared Logarithmic Error is best understood as a specialized regression loss for problems where target scale and relative structure matter. Its logarithmic transformation reduces the influence of large absolute values, provides a defined treatment of zero through log⁡(1+x)\log(1+x), and evaluates prediction quality differently from ordinary squared or absolute error. Understanding these properties helps us choose MSLE for regression objectives where logarithmic-scale accuracy is the quantity that matters.


Conclusion

Mean Squared Logarithmic Error (MSLE) is an important regression loss function for machine learning problems where relative differences are more meaningful than absolute differences. By applying the log(1 + x) transformation before calculating squared error, MSLE reduces the influence of large target values and evaluates prediction accuracy in logarithmic space. This makes it particularly useful for non-negative targets with skewed distributions or values that span a wide numerical range.


Understanding the mathematical behavior of MSLE is essential when selecting it as a loss or evaluation metric. Its non-negative formulation, scale-dependent gradient, logarithmic domain, and relationship with RMSLE distinguish it from traditional losses such as MSE and MAE. At the same time, MSLE is not suitable for every regression problem, especially when absolute error in the original target units is the primary objective.


Ultimately, the effectiveness of MSLE depends on how prediction error should be defined for the specific machine learning task. Choosing MSLE when logarithmic-scale and relative accuracy are important can provide a more appropriate optimization and evaluation objective, helping regression models better reflect the behavior that matters in the underlying data.


Get in touch for customized mentorship, research and freelance solutions tailored to your needs.

bottom of page