Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Keras · Class

losses.Huber

Huber is a regression loss that combines squared-error behavior for small errors with absolute-error behavior for large errors. The delta parameter sets the transition between these regions.

How it works

Huber computes the error as the difference between targets and predictions. It uses a quadratic penalty when the absolute error is at most delta and a linear penalty for larger errors, then averages the resulting values over the final axis. The loss object can be called directly or supplied to model compilation.

Syntax

keras.losses.Huber(delta=1.0, reduction="sum_over_batch_size", name="huber_loss", dtype=None)

Parameters

NameTypeDescription
deltafloatThreshold where the loss changes from quadratic to linear behavior.
reductionstringHow individual losses are aggregated; the default is sum_over_batch_size.
namestringName assigned to the loss object.
dtypestring or dtypeOptional data type used by the loss.

Returns

A loss value computed from y_true and y_pred; the documented formula averages the per-element values over the last axis.

Key points

  • The loss compares target values y_true with model outputs y_pred.
  • For each error value, the calculation applies one of two piecewise formulas.
  • Errors at or below delta use the quadratic branch.
  • Errors greater than delta use the linear branch.
  • After applying the piecewise formula, values are averaged along the last axis.

Common mistakes

  • Choosing delta changes where the loss switches from quadratic behavior to linear behavior; it is not merely a scaling factor.

Example: Compile a model with Huber loss

import keras

loss_fn = keras.losses.Huber(delta=1.0)
model = keras.Sequential([keras.layers.Dense(1)])
model.compile(optimizer="sgd", loss=loss_fn)

Creates a Huber loss with the default transition point and uses it when compiling a regression model.

When to use it

Reduce sensitivity to outliers

Regression with occasional large residuals.

Configure training loss

A model compiled with a configurable loss object.

Compute regression error

Direct evaluation of target and prediction differences.

Interview questions

QHow does Huber loss compute its value?

Huber is piecewise: for errors whose absolute value is at most delta, it contributes 0.5 times the squared error; for larger errors, it contributes delta times the absolute error minus 0.5 times delta squared. The per-sample values are averaged over the last axis.

Follow-up: What does delta control? It sets the error magnitude at which the quadratic region changes to the linear region.

QWhat are the important constructor defaults?

The constructor defaults delta to 1.0, uses sum_over_batch_size reduction, and names the loss huber_loss. Its callable form accepts true and predicted targets.

Follow-up: How would you configure a different transition point? Instantiate keras.losses.Huber with the desired delta value.

QHow is Huber typically used in Keras?

Huber is a regression loss and can be passed as a loss object when compiling a Keras model. The documented constructor is keras.losses.Huber(delta=1.0, reduction="sum_over_batch_size", name="huber_loss", dtype=None).

Follow-up: Why use the class rather than only a function? The class lets you configure constructor arguments such as delta and reduction before calling it.

  • keras.losses.huber
  • keras.losses.LogCosh
  • keras.losses.MeanAbsoluteError
  • keras.losses.MeanSquaredError

Source: Regression losses - Keras. Checked 2026-10-15.