This repository has been archived by the owner on Aug 15, 2019. It is now read-only.
/
loss_ops.ts
267 lines (247 loc) · 10.4 KB
/
loss_ops.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
* @license
* Copyright 2018 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import {doc} from '../doc';
import {Tensor} from '../tensor';
import * as util from '../util';
import {operation} from './operation';
import * as ops from './ops';
export enum Reduction {
NONE,
MEAN,
SUM,
SUM_BY_NONZERO_WEIGHTS
}
export class LossOps {
/**
* Computes the weighted loss between two tensors.
*
* @param losses Tensor of shape `[batch_size, d1, ... dN]`.
* @param weights Tensor whose rank is either 0, or the same rank as
* `losses`, and must be broadcastable to `losses` (i.e., all
* dimensions must be either `1`, or the same as the corresponding
* `losses` dimension).
*/
@doc({heading: 'Training', subheading: 'Losses', namespace: 'losses'})
@operation
static computeWeightedLoss<T extends Tensor, O extends Tensor>(
losses: T, weights?: Tensor,
reduction = Reduction.SUM_BY_NONZERO_WEIGHTS): O {
util.assertArgumentsAreTensors({losses}, 'computeWeightedLoss');
if (weights != null) {
util.assertArgumentsAreTensors({weights}, 'computeWeightedLoss');
}
const weightedLoss = (weights == null) ? losses : losses.mul(weights);
if (reduction === Reduction.NONE) {
return weightedLoss as O;
}
if (reduction === Reduction.SUM) {
return weightedLoss.sum();
}
if (reduction === Reduction.MEAN) {
return (weights == null) ? weightedLoss.mean() :
weightedLoss.sum().div(weights.sum());
}
if (reduction === Reduction.SUM_BY_NONZERO_WEIGHTS) {
if (weights == null) {
return weightedLoss.sum().div(ops.scalar(losses.size));
} else {
const numNonZeros = weights.notEqual(ops.scalar(0)).sum().toFloat();
return weightedLoss.sum().div(numNonZeros);
}
}
throw Error(`Unknown reduction: ${reduction}`);
}
/**
* Computes the absolute difference 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 absoluteDifference<T extends Tensor, O extends Tensor>(
labels: T, predictions: T, weights?: Tensor,
reduction = Reduction.SUM_BY_NONZERO_WEIGHTS): O {
util.assertArgumentsAreTensors({labels, predictions}, 'absoluteDifference');
if (weights != null) {
util.assertArgumentsAreTensors({weights}, 'absoluteDifference');
}
util.assertShapesMatch(
labels.shape, predictions.shape, 'Error in absoluteDifference: ');
const losses = labels.sub(predictions).abs();
return LossOps.computeWeightedLoss(losses, weights, reduction);
}
/**
* Computes the mean squared error 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 meanSquaredError<T extends Tensor, O extends Tensor>(
labels: T, predictions: T, weights?: Tensor,
reduction = Reduction.SUM_BY_NONZERO_WEIGHTS): O {
util.assertArgumentsAreTensors({labels, predictions}, 'meanSquaredError');
if (weights != null) {
util.assertArgumentsAreTensors({weights}, 'meanSquaredError');
}
util.assertShapesMatch(
labels.shape, predictions.shape, 'Error in meanSquaredError: ');
const losses = labels.squaredDifference(predictions);
return LossOps.computeWeightedLoss(losses, weights, reduction);
}
/**
* Computes the cosine distance loss between two tensors.
*
* @param labels The ground truth output tensor, same dimensions as
* 'predictions'.
* @param predictions The predicted outputs.
* @param axis The dimension along which the cosine distance is computed.
* @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 cosineDistance<T extends Tensor, O extends Tensor>(
labels: T, predictions: T, axis: number, weights?: Tensor,
reduction = Reduction.SUM_BY_NONZERO_WEIGHTS): O {
util.assertArgumentsAreTensors({labels, predictions}, 'cosineDistance');
if (weights != null) {
util.assertArgumentsAreTensors({weights}, 'cosineDistance');
}
util.assertShapesMatch(
labels.shape, predictions.shape, 'Error in cosineDistance: ');
const one = ops.scalar(1);
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);
}
/**
* Computes the log 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 epsilon A small increment to avoid taking log of zero
* @param reduction Type of reduction to apply to loss. Should be of type
* `Reduction`
*/
@doc({heading: 'Training', subheading: 'Losses', namespace: 'losses'})
@operation
static logLoss<T extends Tensor, O extends Tensor>(
labels: T, predictions: T, weights?: Tensor, epsilon = 1e-7,
reduction = Reduction.SUM_BY_NONZERO_WEIGHTS): O {
util.assertArgumentsAreTensors({labels, predictions}, 'logLoss');
if (weights != null) {
util.assertArgumentsAreTensors({weights}, 'logLoss');
}
util.assertShapesMatch(
labels.shape, predictions.shape, 'Error in logLoss: ');
const one = ops.scalar(1);
const epsilonScalar = ops.scalar(epsilon);
const losses = labels.mul(predictions.add(epsilonScalar).log())
.neg()
.sub(one.sub(labels).mul(
one.sub(predictions).add(epsilonScalar).log()));
return LossOps.computeWeightedLoss(losses, weights, reduction);
}
/**
* Computes the huber 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 delta Point where huber loss changes from quadratic to linear.
* @param reduction Type of reduction to apply to loss. Should be of type
* `Reduction`.
*/
@doc({heading: 'Training', subheading: 'Losses', namespace: 'losses'})
@operation
static huberLoss<T extends Tensor, O extends Tensor>(
labels: T, predictions: T, weights?: Tensor, delta = 1.0,
reduction = Reduction.SUM_BY_NONZERO_WEIGHTS): O {
util.assertArgumentsAreTensors({labels, predictions}, 'huberLoss');
if (weights != null) {
util.assertArgumentsAreTensors({weights}, 'huberLoss');
}
util.assertShapesMatch(
labels.shape, predictions.shape, 'Error in huberLoss: ');
const deltaScalar = ops.scalar(delta);
const error = predictions.sub(labels).abs();
const quadratic = ops.minimum(error, deltaScalar);
const linear = error.sub(quadratic);
const losses =
ops.scalar(0.5).mul(quadratic.square()).add(deltaScalar.mul(linear));
return LossOps.computeWeightedLoss(losses, weights, reduction);
}
}