public class SDLoss extends SDOps
SameDiff.loss()
Modifier and Type | Method and Description |
---|---|
SDVariable |
absoluteDifference(String name,
SDVariable label,
SDVariable predictions)
|
SDVariable |
absoluteDifference(String name,
SDVariable label,
SDVariable predictions,
LossReduce lossReduce)
|
SDVariable |
absoluteDifference(String name,
SDVariable label,
SDVariable predictions,
SDVariable weights,
LossReduce lossReduce)
Absolute difference loss: {@code sum_i abs( label[i] - predictions[i] )
|
SDVariable |
cosineDistance(String name,
SDVariable label,
SDVariable predictions,
int dimension)
|
SDVariable |
cosineDistance(String name,
SDVariable label,
SDVariable predictions,
LossReduce lossReduce,
int dimension)
|
SDVariable |
cosineDistance(String name,
SDVariable label,
SDVariable predictions,
SDVariable weights,
LossReduce lossReduce,
int dimension)
Cosine distance loss:
1 - cosineSimilarity(x,y) or 1 - sum_i label[i] * prediction[i] , which is
equivalent to cosine distance when both the predictions and labels are normalized.Note: This loss function assumes that both the predictions and labels are normalized to have unit l2 norm. |
SDVariable |
hingeLoss(String name,
SDVariable label,
SDVariable predictions)
|
SDVariable |
hingeLoss(String name,
SDVariable label,
SDVariable predictions,
LossReduce lossReduce)
|
SDVariable |
hingeLoss(String name,
SDVariable label,
SDVariable predictions,
SDVariable weights,
LossReduce lossReduce)
Hinge loss: a loss function used for training classifiers.
|
SDVariable |
huberLoss(String name,
SDVariable label,
SDVariable predictions,
double delta)
|
SDVariable |
huberLoss(String name,
SDVariable label,
SDVariable predictions,
LossReduce lossReduce,
double delta)
|
SDVariable |
huberLoss(String name,
SDVariable label,
SDVariable predictions,
SDVariable weights,
LossReduce lossReduce,
double delta)
Huber loss function, used for robust regression.
|
SDVariable |
l2Loss(SDVariable var)
L2 loss: 1/2 * sum(x^2)
|
SDVariable |
l2Loss(String name,
SDVariable var)
L2 loss: 1/2 * sum(x^2)
|
SDVariable |
logLoss(String name,
SDVariable label,
SDVariable predictions)
|
SDVariable |
logLoss(String name,
SDVariable label,
SDVariable predictions,
LossReduce lossReduce)
|
SDVariable |
logLoss(String name,
SDVariable label,
SDVariable predictions,
SDVariable weights,
LossReduce lossReduce,
double epsilon)
Log loss, i.e., binary cross entropy loss, usually used for binary multi-label classification.
|
SDVariable |
logPoisson(String name,
SDVariable label,
SDVariable predictions)
|
SDVariable |
logPoisson(String name,
SDVariable label,
SDVariable predictions,
LossReduce lossReduce)
|
SDVariable |
logPoisson(String name,
SDVariable label,
SDVariable predictions,
SDVariable weights,
LossReduce lossReduce)
Log poisson loss: a loss function used for training classifiers.
|
SDVariable |
logPoissonFull(String name,
SDVariable label,
SDVariable predictions)
|
SDVariable |
logPoissonFull(String name,
SDVariable label,
SDVariable predictions,
LossReduce lossReduce)
|
SDVariable |
logPoissonFull(String name,
SDVariable label,
SDVariable predictions,
SDVariable weights,
LossReduce lossReduce)
Log poisson loss: a loss function used for training classifiers.
|
SDVariable |
meanPairwiseSquaredError(String name,
SDVariable label,
SDVariable predictions,
LossReduce lossReduce)
|
SDVariable |
meanPairwiseSquaredError(String name,
SDVariable label,
SDVariable predictions,
SDVariable weights,
LossReduce lossReduce)
Mean pairwise squared error.
MPWSE loss calculates the difference between pairs of consecutive elements in the predictions and labels arrays. |
SDVariable |
meanSquaredError(String name,
SDVariable label,
SDVariable predictions)
|
SDVariable |
meanSquaredError(String name,
SDVariable label,
SDVariable predictions,
LossReduce lossReduce)
|
SDVariable |
meanSquaredError(String name,
SDVariable label,
SDVariable predictions,
SDVariable weights,
LossReduce lossReduce)
Mean squared error loss function.
|
SDVariable |
sigmoidCrossEntropy(String name,
SDVariable label,
SDVariable predictions)
|
SDVariable |
sigmoidCrossEntropy(String name,
SDVariable label,
SDVariable predictions,
LossReduce lossReduce)
|
SDVariable |
sigmoidCrossEntropy(String name,
SDVariable label,
SDVariable predictionLogits,
SDVariable weights,
LossReduce lossReduce,
double labelSmoothing)
Sigmoid cross entropy: applies the sigmoid activation function on the input logits (input "pre-sigmoid preductions")
and implements the binary cross entropy loss function.
|
SDVariable |
softmaxCrossEntropy(String name,
SDVariable label,
SDVariable predictions)
|
SDVariable |
softmaxCrossEntropy(String name,
SDVariable label,
SDVariable predictions,
LossReduce lossReduce)
|
SDVariable |
softmaxCrossEntropy(String name,
SDVariable oneHotLabels,
SDVariable logitPredictions,
SDVariable weights,
LossReduce lossReduce,
double labelSmoothing)
Applies the softmax activation function to the input, then implement multi-class cross entropy:
-sum_classes label[i] * log(p[c]) where p = softmax(logits) If LossReduce.NONE is used, returned shape is [numExamples] out for [numExamples, numClasses] predicitons/labels;
otherwise, the output is a scalar. |
SDVariable |
sparseSoftmaxCrossEntropy(SDVariable logits,
SDVariable labels)
|
SDVariable |
sparseSoftmaxCrossEntropy(String name,
SDVariable logits,
SDVariable labels)
As per
softmaxCrossEntropy(String, SDVariable, SDVariable, LossReduce) but the labels variable
is represented as an integer array instead of the equivalent one-hot array.i.e., if logits are rank N, then labels have rank N-1 |
SDVariable |
weightedCrossEntropyWithLogits(SDVariable targets,
SDVariable inputs,
SDVariable weights)
TODO
|
SDVariable |
weightedCrossEntropyWithLogits(String name,
SDVariable targets,
SDVariable inputs,
SDVariable weights)
TODO
|
f, updateVariableNameAndReference
public SDLoss(SameDiff sameDiff)
public SDVariable absoluteDifference(String name, @NonNull SDVariable label, @NonNull SDVariable predictions)
public SDVariable absoluteDifference(String name, @NonNull SDVariable label, @NonNull SDVariable predictions, SDVariable weights, @NonNull LossReduce lossReduce)
name
- Name of the operationlabel
- Label arraypredictions
- Predictions arrayweights
- Weights array. May be null. If null, a weight of 1.0 is usedlossReduce
- Reduction type for the loss. See LossReduce
for more details. Default: LossReduce.MEAN_BY_NONZERO_WEIGHT_COUNT
public SDVariable absoluteDifference(String name, @NonNull SDVariable label, @NonNull SDVariable predictions, @NonNull LossReduce lossReduce)
public SDVariable cosineDistance(String name, @NonNull SDVariable label, @NonNull SDVariable predictions, int dimension)
public SDVariable cosineDistance(String name, @NonNull SDVariable label, @NonNull SDVariable predictions, SDVariable weights, @NonNull LossReduce lossReduce, int dimension)
1 - cosineSimilarity(x,y)
or 1 - sum_i label[i] * prediction[i]
, which is
equivalent to cosine distance when both the predictions and labels are normalized.SDBaseOps.norm2(String, SDVariable, boolean, int...)
along the cosine distance dimension (with keepDims=true).name
- Name of the operationlabel
- Label arraypredictions
- Predictions arrayweights
- Weights array. May be null. If null, a weight of 1.0 is usedlossReduce
- Reduction type for the loss. See LossReduce
for more details. Default: LossReduce.MEAN_BY_NONZERO_WEIGHT_COUNT
dimension
- Dimension to perform the cosine distance overpublic SDVariable cosineDistance(String name, @NonNull SDVariable label, @NonNull SDVariable predictions, @NonNull LossReduce lossReduce, int dimension)
public SDVariable hingeLoss(String name, @NonNull SDVariable label, @NonNull SDVariable predictions)
public SDVariable hingeLoss(String name, @NonNull SDVariable label, @NonNull SDVariable predictions, SDVariable weights, @NonNull LossReduce lossReduce)
L = max(0, 1 - t * predictions)
where t is the label values after internally converting to {-1,1}
from the user specified {0,1}. Note that Labels should be provided with values {0,1}.name
- Name of the operationlabel
- Label array. Each value should be 0.0 or 1.0 (internally -1 to 1 is used)predictions
- Predictions arrayweights
- Weights array. May be null. If null, a weight of 1.0 is usedlossReduce
- Reduction type for the loss. See LossReduce
for more details. Default: LossReduce.MEAN_BY_NONZERO_WEIGHT_COUNT
public SDVariable hingeLoss(String name, @NonNull SDVariable label, @NonNull SDVariable predictions, @NonNull LossReduce lossReduce)
public SDVariable huberLoss(String name, @NonNull SDVariable label, @NonNull SDVariable predictions, double delta)
public SDVariable huberLoss(String name, @NonNull SDVariable label, @NonNull SDVariable predictions, SDVariable weights, @NonNull LossReduce lossReduce, double delta)
L = 0.5 * (label[i] - predictions[i])^2 if abs(label[i] - predictions[i]) < delta
L = delta * abs(label[i] - predictions[i]) - 0.5 * delta^2 otherwise
name
- Name of the operationlabel
- Label arraypredictions
- Predictions arrayweights
- Weights array. May be null. If null, a weight of 1.0 is usedlossReduce
- Reduction type for the loss. See LossReduce
for more details. Default: LossReduce.MEAN_BY_NONZERO_WEIGHT_COUNT
delta
- Loss function delta valuepublic SDVariable huberLoss(String name, @NonNull SDVariable label, @NonNull SDVariable predictions, @NonNull LossReduce lossReduce, double delta)
public SDVariable l2Loss(@NonNull SDVariable var)
var
- Variable to calculate L2 loss ofpublic SDVariable l2Loss(String name, @NonNull SDVariable var)
name
- Name of the output variablevar
- Variable to calculate L2 loss ofpublic SDVariable logLoss(String name, @NonNull SDVariable label, @NonNull SDVariable predictions)
public SDVariable logLoss(String name, @NonNull SDVariable label, @NonNull SDVariable predictions, SDVariable weights, @NonNull LossReduce lossReduce, double epsilon)
-1/numExamples * sum_i (labels[i] * log(predictions[i] + epsilon) + (1-labels[i]) * log(1-predictions[i] + epsilon))
name
- Name of the operationlabel
- Label arraypredictions
- Predictions arrayweights
- Weights array. May be null. If null, a weight of 1.0 is usedlossReduce
- Reduction type for the loss. See LossReduce
for more details. Default: LossReduce.MEAN_BY_NONZERO_WEIGHT_COUNT
public SDVariable logLoss(String name, @NonNull SDVariable label, @NonNull SDVariable predictions, @NonNull LossReduce lossReduce)
public SDVariable logPoisson(String name, @NonNull SDVariable label, @NonNull SDVariable predictions)
public SDVariable logPoisson(String name, @NonNull SDVariable label, @NonNull SDVariable predictions, SDVariable weights, @NonNull LossReduce lossReduce)
L = exp(c) - z * c
where c is log(predictions) and z is labels.name
- Name of the operationlabel
- Label array. Each value should be 0.0 or 1.0predictions
- Predictions array (has to be log(x) of actual predictions)weights
- Weights array. May be null. If null, a weight of 1.0 is usedlossReduce
- Reduction type for the loss. See LossReduce
for more details. Default: LossReduce.MEAN_BY_NONZERO_WEIGHT_COUNT
public SDVariable logPoisson(String name, @NonNull SDVariable label, @NonNull SDVariable predictions, @NonNull LossReduce lossReduce)
public SDVariable logPoissonFull(String name, @NonNull SDVariable label, @NonNull SDVariable predictions)
public SDVariable logPoissonFull(String name, @NonNull SDVariable label, @NonNull SDVariable predictions, SDVariable weights, @NonNull LossReduce lossReduce)
L = exp(c) - z * c + z * log(z) - z + 0.5 * log(2 * pi * z)
where c is log(predictions) and z is labels.name
- Name of the operationlabel
- Label array. Each value should be 0.0 or 1.0predictions
- Predictions array (has to be log(x) of actual predictions)weights
- Weights array. May be null. If null, a weight of 1.0 is usedlossReduce
- Reduction type for the loss. See LossReduce
for more details. Default: LossReduce.MEAN_BY_NONZERO_WEIGHT_COUNT
public SDVariable logPoissonFull(String name, @NonNull SDVariable label, @NonNull SDVariable predictions, @NonNull LossReduce lossReduce)
public SDVariable meanPairwiseSquaredError(String name, @NonNull SDVariable label, @NonNull SDVariable predictions, @NonNull LossReduce lossReduce)
public SDVariable meanPairwiseSquaredError(String name, @NonNull SDVariable label, @NonNull SDVariable predictions, SDVariable weights, @NonNull LossReduce lossReduce)
[((p0-p1) - (l0-l1))^2 + ((p0-p2) - (l0-l2))^2 + ((p1-p2) - (l1-l2))^2] / 3
name
- Name of the operationlabel
- Label arraypredictions
- Predictions arrayweights
- Weights array. May be null. If null, a weight of 1.0 is used. Must be either null, scalar, or have shape [batchSize]public SDVariable meanSquaredError(String name, @NonNull SDVariable label, @NonNull SDVariable predictions)
public SDVariable meanSquaredError(String name, @NonNull SDVariable label, @NonNull SDVariable predictions, SDVariable weights, @NonNull LossReduce lossReduce)
(label[i] - prediction[i])^2
- i.e., squared error on a per-element basis.
When averaged (using LossReduce.MEAN_BY_WEIGHT
or LossReduce.MEAN_BY_NONZERO_WEIGHT_COUNT
(the default))
this is the mean squared error loss function.name
- Name of the operationlabel
- Label arraypredictions
- Predictions arrayweights
- Weights array. May be null. If null, a weight of 1.0 is usedlossReduce
- Reduction type for the loss. See LossReduce
for more details. Default: LossReduce.MEAN_BY_NONZERO_WEIGHT_COUNT
public SDVariable meanSquaredError(String name, @NonNull SDVariable label, @NonNull SDVariable predictions, @NonNull LossReduce lossReduce)
public SDVariable sigmoidCrossEntropy(String name, @NonNull SDVariable label, @NonNull SDVariable predictions)
public SDVariable sigmoidCrossEntropy(String name, @NonNull SDVariable label, @NonNull SDVariable predictionLogits, SDVariable weights, @NonNull LossReduce lossReduce, double labelSmoothing)
-1/numExamples * sum_i (labels[i] * log(sigmoid(logits[i])) + (1-labels[i]) * log(1-sigmoid(logits[i])))
though this is done in a mathematically equivalent but more numerical stable form.
numClasses = labels.size(1);
label = (1.0 - labelSmoothing) * label + 0.5 * labelSmoothing
name
- Name of the operationlabel
- Label arraypredictionLogits
- Predictions arrayweights
- Weights array. May be null. If null, a weight of 1.0 is usedlossReduce
- Reduction type for the loss. See LossReduce
for more details. Default: LossReduce.MEAN_BY_NONZERO_WEIGHT_COUNT
public SDVariable sigmoidCrossEntropy(String name, @NonNull SDVariable label, @NonNull SDVariable predictions, @NonNull LossReduce lossReduce)
public SDVariable softmaxCrossEntropy(String name, @NonNull SDVariable label, @NonNull SDVariable predictions)
public SDVariable softmaxCrossEntropy(String name, @NonNull SDVariable oneHotLabels, @NonNull SDVariable logitPredictions, SDVariable weights, @NonNull LossReduce lossReduce, double labelSmoothing)
-sum_classes label[i] * log(p[c])
where p = softmax(logits)
LossReduce.NONE
is used, returned shape is [numExamples] out for [numExamples, numClasses] predicitons/labels;
otherwise, the output is a scalar.
When label smoothing is > 0, the following label smoothing is used:
numClasses = labels.size(1);
oneHotLabel = (1.0 - labelSmoothing) * oneHotLabels + labelSmoothing/numClasses
name
- Name of the operationoneHotLabels
- Label array. Should be one-hot per example and same shape as predictions (for example, [mb, nOut])logitPredictions
- Predictions array (pre-softmax)weights
- Weights array. May be null. If null, a weight of 1.0 is usedlossReduce
- Reduction type for the loss. See LossReduce
for more details. Default: LossReduce.MEAN_BY_NONZERO_WEIGHT_COUNT
labelSmoothing
- Label smoothing value. Default value: 0public SDVariable softmaxCrossEntropy(String name, @NonNull SDVariable label, @NonNull SDVariable predictions, @NonNull LossReduce lossReduce)
public SDVariable sparseSoftmaxCrossEntropy(@NonNull SDVariable logits, @NonNull SDVariable labels)
public SDVariable sparseSoftmaxCrossEntropy(String name, @NonNull SDVariable logits, @NonNull SDVariable labels)
softmaxCrossEntropy(String, SDVariable, SDVariable, LossReduce)
but the labels variable
is represented as an integer array instead of the equivalent one-hot array.name
- Name of the output variable. May be nulllogits
- Logits array ("pre-softmax activations")labels
- Labels array. Must be an integer type.public SDVariable weightedCrossEntropyWithLogits(SDVariable targets, SDVariable inputs, SDVariable weights)
targets
- inputs
- weights
- public SDVariable weightedCrossEntropyWithLogits(String name, SDVariable targets, SDVariable inputs, SDVariable weights)
name
- targets
- inputs
- weights
- Copyright © 2019. All rights reserved.