Loss Functions
All loss functions in ND4J — ILossFunction implementations, usage in output layers, weighted loss, and custom loss functions
Loss functions measure the discrepancy between a network's predictions and the true labels. During training, the optimizer minimizes the loss to improve predictions. All loss functions in the DL4J ecosystem implement the ILossFunction interface at org.nd4j.linalg.lossfunctions.ILossFunction.
Usage
In Output Layers (Preferred)
Pass an ILossFunction instance to the output layer builder:
import org.nd4j.linalg.lossfunctions.impl.LossMCXENT;
new OutputLayer.Builder(new LossMCXENT())
.nIn(256).nOut(10)
.activation(Activation.SOFTMAX)
.build()Using the LossFunction Enum (Legacy)
The convenience enum still works but instantiating ILossFunction directly is preferred in M2.1:
import org.nd4j.linalg.lossfunctions.LossFunctions.LossFunction;
new OutputLayer.Builder(LossFunction.MSE)
.nIn(256).nOut(1)
.activation(Activation.IDENTITY)
.build()In SameDiff
Use the sd.loss namespace:
Classification Loss Functions
LossMCXENT — Multi-Class Cross Entropy
The standard loss for multi-class classification tasks. Measures the cross entropy between the true distribution (one-hot labels) and the predicted distribution (softmax outputs).
Formula: L = -sum(y_true * log(y_pred))
Pair with: Activation.SOFTMAX
Class weighting: Pass an INDArray of shape [1, numClasses] to handle class imbalance:
LossSparseMCXENT — Sparse Multi-Class Cross Entropy
Same as LossMCXENT but accepts integer labels instead of one-hot encoded labels. Labels should be a column vector of class indices (shape [batchSize, 1]).
Pair with: Activation.SOFTMAX
LossNegativeLogLikelihood — Negative Log Likelihood
Functionally equivalent to LossMCXENT for softmax outputs. The difference is in how the gradient is computed internally.
Pair with: Activation.SOFTMAX
LossBinaryXENT — Binary Cross Entropy
For binary classification or multi-label classification where each output is an independent binary decision.
Formula: L = -[y * log(p) + (1-y) * log(1-p)]
Pair with: Activation.SIGMOID
LossHinge — Hinge Loss
SVM-style loss for classification. Labels should be -1 or +1.
Formula: L = max(0, 1 - y_true * y_pred)
Pair with: Activation.TANH (output range -1 to 1)
LossSquaredHinge — Squared Hinge Loss
Smooth variant of hinge loss: L = max(0, 1 - y_true * y_pred)^2
LossFMeasure — F-Measure Loss
Directly optimizes the F-measure (F1 score by default). For binary classification only.
beta = 1.0(default): F1 score (equal weight to precision and recall)beta < 1.0: Favors precisionbeta > 1.0: Favors recall
LossMultiLabel — Multi-Label Loss
Specialized loss for multi-label ranking tasks.
Regression Loss Functions
LossMSE — Mean Squared Error
Standard regression loss. Heavily penalizes large errors.
Formula: L = mean((y_true - y_pred)^2)
Pair with: Activation.IDENTITY
LossMAE — Mean Absolute Error
More robust to outliers than MSE.
Formula: L = mean(|y_true - y_pred|)
Pair with: Activation.IDENTITY
LossL1 — L1 Loss
Sum of absolute differences (not averaged). Encourages sparse predictions.
Formula: L = sum(|y_true - y_pred|)
LossL2 — L2 Loss
Sum of squared differences (not averaged).
Formula: L = sum((y_true - y_pred)^2)
LossMSLE — Mean Squared Logarithmic Error
Useful when target values span several orders of magnitude.
Formula: L = mean((log(y_true + 1) - log(y_pred + 1))^2)
Pair with: Activation.IDENTITY (predictions should be non-negative)
LossMAPE — Mean Absolute Percentage Error
Percentage-based regression error.
Formula: L = mean(|y_true - y_pred| / |y_true|) * 100
LossPoisson — Poisson Loss
For count data regression where the target follows a Poisson distribution.
Formula: L = mean(y_pred - y_true * log(y_pred))
Distribution and Similarity Loss Functions
LossKLD — Kullback-Leibler Divergence
Measures the divergence between two probability distributions. Used in variational autoencoders and distribution matching.
Formula: L = sum(y_true * log(y_true / y_pred))
LossCosineProximity — Cosine Proximity Loss
Measures the cosine distance between predictions and labels. Useful for similarity learning tasks where direction matters more than magnitude.
Formula: L = -sum(y_true * y_pred) / (||y_true|| * ||y_pred||)
LossWasserstein — Wasserstein Loss
Earth Mover's Distance loss. Commonly used in WGAN (Wasserstein GAN) training.
Formula: L = mean(y_true * y_pred)
Specialized Loss Functions
LossMixtureDensity — Mixture Density Network Loss
For mixture density networks that output parameters of a Gaussian mixture model (means, variances, mixing coefficients).
Quick Reference
Multi-class classification
LossMCXENT
SOFTMAX
One-hot
Multi-class (integer labels)
LossSparseMCXENT
SOFTMAX
Integer indices
Binary classification
LossBinaryXENT
SIGMOID
0/1
Multi-label classification
LossBinaryXENT
SIGMOID
Binary vector
Regression
LossMSE
IDENTITY
Continuous
Robust regression
LossMAE
IDENTITY
Continuous
Log-scale regression
LossMSLE
IDENTITY
Positive continuous
Count data
LossPoisson
IDENTITY
Non-negative integer
Distribution matching
LossKLD
SOFTMAX
Probabilities
Similarity learning
LossCosineProximity
varies
Normalized vectors
SVM-style
LossHinge
TANH
-1/+1
GAN (Wasserstein)
LossWasserstein
IDENTITY
-1/+1
Optimize F1 directly
LossFMeasure
SIGMOID
0/1
Custom Loss Functions
Implement ILossFunction:
Use it like any built-in loss:
Last updated
Was this helpful?