Class CosineSimilarity
java.lang.Object
org.tensorflow.framework.losses.CosineSimilarity
- All Implemented Interfaces:
Loss
Computes the cosine similarity between labels and predictions.
Note that it is a number between -1 and 1. When it is a negative
number between -1 and 0, 0 indicates orthogonality and
values closer to -1indicate greater similarity. The values closer to 1
indicate greater dissimilarity. This makes it usable as a loss function in a setting where you
try to maximize the proximity between predictions and targets. If either labels or
predictions is a zero vector, cosine similarity will be 0 regardless of
the proximity between predictions and targets.
loss = -sum(l2Norm(labels) * l2Norm(predictions))
Standalone usage:
Operand<TFloat32> labels =
tf.constant(new float[][] {{0.f, 1.f}, {1.f, 1.f}});
Operand<TFloat32> predictions =
tf.constant(new float[][] {{1.f, 0.f}, {1.f, 1.f}});
CosineSimilarity cosineLoss = new CosineSimilarity(tf);
Operand<TFloat32> result = cosineLoss.call(Ops tf, labels, predictions);
// produces -0.5
Calling with sample weight:
Operand<TFloat32> sampleWeight = tf.constant(new float[] {0.8f, 0.2f});
Operand<TFloat32> result = cosineLoss.call(Ops tf, labels, predictions, sampleWeight);
// produces -0.0999f
Using SUM reduction type:
CosineSimilarity cosineLoss = new CosineSimilarity(tf, Reduction.SUM); Operand<TFloat32> result = cosineLoss.call(Ops tf, labels, predictions); // produces -0.999f
Using NONE reduction type:
CosineSimilarity cosineLoss = new CosineSimilarity(tf, Reduction.NONE); Operand<TFloat32> result = cosineLoss.call(Ops tf, labels, predictions); // produces [-0.f, -0.999f]
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates a Cosine Similarity AbstractLoss usingClass.getSimpleName()as the loss name, an axis ofDEFAULT_AXIS, and a AbstractLoss Reduction ofDEFAULT_REDUCTIONCosineSimilarity(int axis) Creates a Cosine Similarity AbstractLoss usingClass.getSimpleName()as the loss name, and a AbstractLoss Reduction ofDEFAULT_REDUCTIONCosineSimilarity(int[] axis) Creates a Cosine Similarity AbstractLoss usingClass.getSimpleName()as the loss name, and a AbstractLoss Reduction ofDEFAULT_REDUCTIONCosineSimilarity(int[] axis, Reduction reduction) Creates a Cosine Similarity AbstractLoss usingClass.getSimpleName()as the loss nameCosineSimilarity(int axis, Reduction reduction) Creates a Cosine Similarity AbstractLoss usingClass.getSimpleName()as the loss nameCosineSimilarity(String name) Creates a Cosine Similarity AbstractLoss using an axis ofDEFAULT_AXIS, and a AbstractLoss Reduction ofDEFAULT_REDUCTIONCosineSimilarity(String name, int axis) Creates a Cosine Similarity AbstractLoss using a AbstractLoss Reduction ofDEFAULT_REDUCTIONCosineSimilarity(String name, int[] axis) Creates a Cosine Similarity AbstractLoss using a AbstractLoss Reduction ofDEFAULT_REDUCTIONCosineSimilarity(String name, int[] axis, Reduction reduction) Creates a Cosine Similarity AbstractLossCosineSimilarity(String name, int axis, Reduction reduction) Creates a Cosine Similarity AbstractLossCosineSimilarity(String name, Reduction reduction) Creates a Cosine Similarity AbstractLoss using an axis ofDEFAULT_AXISCosineSimilarity(Reduction reduction) Creates a Cosine Similarity AbstractLoss usingClass.getSimpleName()as the loss name and an axis ofDEFAULT_AXIS -
Method Summary
Modifier and TypeMethodDescriptionCalculates the lossGenerates an Operand that calculates the loss.getName()Gets the name for this lossGets the loss reduction
-
Field Details
-
DEFAULT_AXIS
public static final int DEFAULT_AXIS- See Also:
-
DEFAULT_REDUCTION
-
REDUCTION_DEFAULT
-
reduction
-
-
Constructor Details
-
CosineSimilarity
public CosineSimilarity()Creates a Cosine Similarity AbstractLoss usingClass.getSimpleName()as the loss name, an axis ofDEFAULT_AXIS, and a AbstractLoss Reduction ofDEFAULT_REDUCTION -
CosineSimilarity
Creates a Cosine Similarity AbstractLoss using an axis ofDEFAULT_AXIS, and a AbstractLoss Reduction ofDEFAULT_REDUCTION- Parameters:
name- the name of the loss
-
CosineSimilarity
public CosineSimilarity(int axis) Creates a Cosine Similarity AbstractLoss usingClass.getSimpleName()as the loss name, and a AbstractLoss Reduction ofDEFAULT_REDUCTION- Parameters:
axis- The dimension along which the cosine similarity is computed.
-
CosineSimilarity
public CosineSimilarity(int[] axis) Creates a Cosine Similarity AbstractLoss usingClass.getSimpleName()as the loss name, and a AbstractLoss Reduction ofDEFAULT_REDUCTION- Parameters:
axis- The dimension along which the cosine similarity is computed.
-
CosineSimilarity
Creates a Cosine Similarity AbstractLoss using a AbstractLoss Reduction ofDEFAULT_REDUCTION- Parameters:
name- the name of the lossaxis- The dimension along which the cosine similarity is computed.
-
CosineSimilarity
Creates a Cosine Similarity AbstractLoss using a AbstractLoss Reduction ofDEFAULT_REDUCTION- Parameters:
name- the name of the lossaxis- The dimension along which the cosine similarity is computed.
-
CosineSimilarity
Creates a Cosine Similarity AbstractLoss usingClass.getSimpleName()as the loss name and an axis ofDEFAULT_AXIS- Parameters:
reduction- Type of Reduction to apply to the loss.
-
CosineSimilarity
Creates a Cosine Similarity AbstractLoss using an axis ofDEFAULT_AXIS- Parameters:
name- the name of the lossreduction- Type of Reduction to apply to the loss.
-
CosineSimilarity
Creates a Cosine Similarity AbstractLoss usingClass.getSimpleName()as the loss name- Parameters:
axis- The dimension along which the cosine similarity is computed.reduction- Type of Reduction to apply to the loss.
-
CosineSimilarity
Creates a Cosine Similarity AbstractLoss usingClass.getSimpleName()as the loss name- Parameters:
axis- The dimension along which the cosine similarity is computed.reduction- Type of Reduction to apply to the loss.
-
CosineSimilarity
-
CosineSimilarity
-
-
Method Details
-
call
public <T extends TNumber> Operand<T> call(Ops tf, Operand<? extends TNumber> labels, Operand<T> predictions, Operand<T> sampleWeights) Generates an Operand that calculates the loss.- Type Parameters:
T- The data type of the predictions, sampleWeights and loss.- Parameters:
tf- the TensorFlow Opslabels- the truth values or labelspredictions- the predictionssampleWeights- Optional sampleWeights acts as a coefficient for the loss. If a scalar is provided, then the loss is simply scaled by the given value. If SampleWeights is a tensor of size [batch_size], then the total loss for each sample of the batch is rescaled by the corresponding element in the SampleWeights vector. If the shape of SampleWeights is [batch_size, d0, .. dN-1] (or can be broadcast to this shape), then each loss element of predictions is scaled by the corresponding value of SampleWeights. (Note on dN-1: all loss functions reduce by 1 dimension, usually axis=-1.)- Returns:
- the loss
-
call
public <T extends TNumber> Operand<T> call(Ops tf, Operand<? extends TNumber> labels, Operand<T> predictions) Calculates the loss- Type Parameters:
T- The data type of the predictions and loss.- Parameters:
tf- the TensorFlow Opslabels- the truth values or labelspredictions- the predictions- Returns:
- the loss
-
getReduction
-
getName
-