Skip to content

Commit

Permalink
Add tf.losses.hingeLoss (tensorflow#1008)
Browse files Browse the repository at this point in the history
FEATURE
  • Loading branch information
manrajgrover authored and dsmilkov committed May 11, 2018
1 parent 3c7acce commit ab91b23
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/ops/loss_ops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,36 @@ export class LossOps {
const losses = one.sub(labels.mul(predictions).sum(axis, true));
return LossOps.computeWeightedLoss(losses, weights, reduction);
}

/**
* Computes the Hinge loss between two tensors.
*
* @param labels The ground truth output tensor, same dimensions as
* 'predictions'.
* @param predictions The predicted outputs.
* @param weights Tensor whose rank is either 0, or the same rank as
* `labels`, and must be broadcastable to `labels` (i.e., all dimensions
* must be either `1`, or the same as the corresponding `losses`
* dimension).
* @param reduction Type of reduction to apply to loss. Should be of type
* `Reduction`
*/
@doc({heading: 'Training', subheading: 'Losses', namespace: 'losses'})
@operation
static hingeLoss<T extends Tensor, O extends Tensor>(
labels: T, predictions: T, weights?: Tensor,
reduction = Reduction.SUM_BY_NONZERO_WEIGHTS): O {
util.assertArgumentsAreTensors({labels, predictions}, 'hingeLoss');
if (weights != null) {
util.assertArgumentsAreTensors({weights}, 'hingeLoss');
}
util.assertShapesMatch(
labels.shape, predictions.shape, 'Error in hingeLoss: ');

const one = ops.scalar(1);
// Convert binary labels to (-1, 1)
labels = ops.scalar(2).mul(labels).sub(one);
const losses = one.sub(labels.mul(predictions)).relu();
return LossOps.computeWeightedLoss(losses, weights, reduction);
}
}
149 changes: 149 additions & 0 deletions src/ops/loss_ops_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -748,3 +748,152 @@ describeWithFlags('cosineDistance', ALL_ENVS, () => {
.toThrowError(e);
});
});

describeWithFlags('hingeLoss', ALL_ENVS, () => {
it('1D', () => {
const predictions = tf.tensor1d([0, 0, 1, 1]);
const label = tf.tensor1d([0, 1, 0, 1]);

const y = tf.losses.hingeLoss(label, predictions);

expect(y.shape).toEqual([]);
expectNumbersClose(y.get(), 1.0);
});

it('1D - weighted - Reduction.SUM_BY_NONZERO_WEIGHTS', () => {
const predictions = tf.tensor1d([0, 0, 1, 1]);
const label = tf.tensor1d([0, 1, 0, 1]);
const weights = tf.tensor1d([0.1, 0.2, 0.3, 0.4]);

const y = tf.losses.hingeLoss(label, predictions, weights);

expect(y.shape).toEqual([]);
expectNumbersClose(y.get(), 0.225);
});

it('1D - weighted - Reduction.NONE', () => {
const predictions = tf.tensor1d([0, 0, 1, 1]);
const label = tf.tensor1d([0, 1, 0, 1]);
const weights = tf.tensor1d([0.1, 0.2, 0.3, 0.4]);

const y =
tf.losses.hingeLoss(label, predictions, weights, tf.Reduction.NONE);

expect(y.shape).toEqual([4]);
expectArraysClose(y, [0.1, 0.2, 0.6, 0.0]);
});

it('1D - Reduction.MEAN', () => {
const predictions = tf.tensor1d([0, 0, 1, 1]);
const label = tf.tensor1d([0, 1, 0, 1]);

const y =
tf.losses.hingeLoss(label, predictions, undefined, tf.Reduction.MEAN);

expect(y.shape).toEqual([]);
expectNumbersClose(y.get(), 1.0);
});

it('1D - weighted - Reduction.MEAN', () => {
const predictions = tf.tensor1d([0, 0, 1, 1]);
const label = tf.tensor1d([0, 1, 0, 1]);
const weights = tf.tensor1d([0.1, 0.2, 0.3, 0.4]);

const y =
tf.losses.hingeLoss(label, predictions, weights, tf.Reduction.MEAN);

expect(y.shape).toEqual([]);
expectNumbersClose(y.get(), 0.9);
});

it('2D', () => {
const predictions = tf.tensor2d([0, 0, 0, 1, 1, 1], [2, 3]);
const label = tf.tensor2d([0, 1, 0, 1, 0, 1], [2, 3]);

const y = tf.losses.hingeLoss(label, predictions);

expect(y.shape).toEqual([]);
expectNumbersClose(y.get(), 0.8333333);
});

it('2D - weighted - Reduction.SUM_BY_NONZERO_WEIGHTS', () => {
const predictions = tf.tensor2d([0, 0, 0, 1, 1, 1], [2, 3]);
const label = tf.tensor2d([0, 1, 0, 1, 0, 1], [2, 3]);
const weights = tf.tensor2d([0.1, 0.2, 0.3, 0.4, 0.5, 0.6], [2, 3]);

const y = tf.losses.hingeLoss(label, predictions, weights);

expect(y.shape).toEqual([]);
expectNumbersClose(y.get(), 0.26666668);
});

it('2D - weighted - Reduction.NONE', () => {
const predictions = tf.tensor2d([0, 0, 0, 1, 1, 1], [2, 3]);
const label = tf.tensor2d([0, 1, 0, 1, 0, 1], [2, 3]);
const weights = tf.tensor2d([0.1, 0.2, 0.3, 0.4, 0.5, 0.6], [2, 3]);

const y =
tf.losses.hingeLoss(label, predictions, weights, tf.Reduction.NONE);

expect(y.shape).toEqual([2, 3]);
expectArraysClose(y, [0.1, 0.2, 0.3, 0, 1, 0]);
});

it('2D - Reduction.MEAN', () => {
const predictions = tf.tensor2d([0, 0, 0, 1, 1, 1], [2, 3]);
const label = tf.tensor2d([0, 1, 0, 1, 0, 1], [2, 3]);

const y =
tf.losses.hingeLoss(label, predictions, undefined, tf.Reduction.MEAN);

expect(y.shape).toEqual([]);
expectNumbersClose(y.get(), 0.8333333);
});

it('2D - weighted - Reduction.MEAN', () => {
const predictions = tf.tensor2d([0, 0, 0, 1, 1, 1], [2, 3]);
const label = tf.tensor2d([0, 1, 0, 1, 0, 1], [2, 3]);
const weights = tf.tensor2d([0.1, 0.2, 0.3, 0.4, 0.5, 0.6], [2, 3]);

const y =
tf.losses.hingeLoss(label, predictions, weights, tf.Reduction.MEAN);

expect(y.shape).toEqual([]);
expectNumbersClose(y.get(), 0.76190484);
});

it('throws when passed label as a non-tensor', () => {
const predictions = tf.tensor2d([1, 0, 1, 0, 1, 0], [2, 3]);
const weights = tf.tensor2d([1, 0, 1, 0, 1, 0], [2, 3]);

const e = /Argument 'labels' passed to 'hingeLoss' must be a Tensor/;
expect(
() => tf.losses.hingeLoss(
{} as tf.Tensor, predictions, weights, tf.Reduction.MEAN))
.toThrowError(e);
});

it('throws when passed label as a non-tensor', () => {
const label = tf.tensor2d([1, 0, 1, 0, 1, 0], [2, 3]);
const weights = tf.tensor2d([1, 0, 1, 0, 1, 0], [2, 3]);

const e = new RegExp(
'Argument \'predictions\' passed to \'hingeLoss\' ' +
'must be a Tensor');
expect(
() => tf.losses.hingeLoss(
label, {} as tf.Tensor, weights, tf.Reduction.MEAN))
.toThrowError(e);
});

it('throws when passed weights as a non-tensor', () => {
const predictions = tf.tensor2d([1, 0, 1, 0, 1, 0], [2, 3]);
const label = tf.tensor2d([1, 0, 1, 0, 1, 0], [2, 3]);

const e = /Argument 'weights' passed to 'hingeLoss' must be a Tensor/;
expect(
() => tf.losses.hingeLoss(
label, predictions, {} as tf.Tensor, tf.Reduction.MEAN))
.toThrowError(e);
});
});
1 change: 1 addition & 0 deletions src/ops/ops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ export const losses = {
absoluteDifference: LossOps.absoluteDifference,
computeWeightedLoss: LossOps.computeWeightedLoss,
cosineDistance: LossOps.cosineDistance,
hingeLoss: LossOps.hingeLoss,
meanSquaredError: LossOps.meanSquaredError,
softmaxCrossEntropy: SoftmaxOps.softmaxCrossEntropy
};
Expand Down

0 comments on commit ab91b23

Please sign in to comment.