/
loss_modules.py
397 lines (343 loc) · 15.5 KB
/
loss_modules.py
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# coding=utf-8
# Copyright (c) 2019 Uber Technologies, Inc.
#
# 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 numpy as np
import tensorflow.compat.v1 as tf
import tensorflow_addons as tfa
from tensorflow.python.ops.losses.losses_impl import Reduction
#
# Custom classes to support Tensorflow 2
#
class BWCEWLoss(tf.keras.losses.Loss):
def __init__(
self,
positive_class_weight=1,
robust_lambda=0,
confidence_penalty=0
):
super(BWCEWLoss, self).__init__()
self.positive_class_weight = positive_class_weight
self.robust_lambda = robust_lambda
self.confidence_penalty = confidence_penalty
def call(self, y_true, y_pred):
logits = y_pred
# weighted cross entropy
train_loss = tf.nn.weighted_cross_entropy_with_logits(
targets=tf.cast(y_true, tf.float32),
logits=logits,
pos_weight=self.positive_class_weight
)
# robust lambda
if self.robust_lambda > 0:
train_loss = ((1 - self.robust_lambda) * train_loss +
self.robust_lambda / 2)
train_mean_loss = tf.reduce_mean(
train_loss
)
# confidence penalty
if self.confidence_penalty > 0:
probabilities = tf.nn.sigmoid(logits)
mean_penalty = mean_confidence_penalty(probabilities, 2)
train_mean_loss += self.confidence_penalty * mean_penalty
return train_mean_loss
# end of custom classes
def softmax_cross_entropy_with_class_weighting(logits, one_hot_labels,
class_weights,
labels_smoothing=0.0):
class_weights_const = tf.expand_dims(
tf.constant(class_weights, dtype=tf.float32), 0)
sample_weights = tf.reduce_sum(
tf.multiply(one_hot_labels, class_weights_const), 1)
return tf.losses.softmax_cross_entropy(onehot_labels=one_hot_labels,
logits=logits,
label_smoothing=labels_smoothing,
weights=sample_weights,
reduction=tf.losses.Reduction.NONE)
def sigmoid_cross_entropy_with_class_weighting(logits, multi_class_labels,
class_weights,
labels_smoothing=0.0):
class_weights_const = tf.expand_dims(
tf.constant(class_weights, dtype=tf.float32), 0)
sample_weights = tf.reduce_sum(
tf.multiply(multi_class_labels, class_weights_const), 1)
return tf.losses.sigmoid_cross_entropy(
multi_class_labels=multi_class_labels,
logits=logits,
label_smoothing=labels_smoothing,
weights=sample_weights,
reduction=tf.losses.Reduction.NONE)
def mean_confidence_penalty(probabilities, num_classes):
max_entropy = tf.constant(np.log(num_classes), dtype=tf.float32)
# clipping needed for avoiding log(0) = -inf
entropy_per_class = tf.maximum(- probabilities *
tf.log(tf.clip_by_value(probabilities, 1e-10,
1)), 0)
entropy = tf.reduce_sum(entropy_per_class, -1)
penalty = (max_entropy - entropy) / max_entropy
return tf.reduce_mean(penalty)
def seq2seq_sequence_loss(targets, targets_sequence_length, logits,
softmax_function=None):
batch_max_targets_sequence_length = tf.shape(targets)[1]
batch_max_logits_sequence_length = tf.shape(logits)[1]
difference = tf.maximum(0,
batch_max_targets_sequence_length - batch_max_logits_sequence_length)
padded_logits = tf.pad(logits, [[0, 0], [0, difference], [0, 0]])
padded_logits = padded_logits[:, :batch_max_targets_sequence_length, :]
with tf.variable_scope('sequence_loss'):
sequence_loss = tfa.seq2seq.sequence_loss(
padded_logits,
targets,
tf.sequence_mask(targets_sequence_length,
batch_max_targets_sequence_length,
dtype=tf.float32),
average_across_timesteps=True,
average_across_batch=False,
softmax_loss_function=softmax_function
)
# batch_max_seq_length = tf.shape(logits)[1]
# unpadded_targets = targets[:, :tf.shape(logits)[1]]
# with tf.variable_scope('sequence_loss'):
# sequence_loss = tfa.seq2seq.sequence_loss(
# logits,
# unpadded_targets,
# tf.sequence_mask(targets_sequence_length, batch_max_seq_length, dtype=tf.float32),
# average_across_timesteps=True,
# average_across_batch=False,
# softmax_loss_function=softmax_function
# )
return sequence_loss
# manual implementation of sequence loss
def cross_entropy_sequence_loss(logits, targets, sequence_length):
"""Calculates the per-example cross-entropy loss for a sequence of logits and
masks out all losses passed the sequence length.
Args:
logits: Logits of shape `[B, T, vocab_size]`
targets: Target classes of shape `[B, T]`
sequence_length: An int32 tensor of shape `[B]` corresponding
to the length of each input
Returns:
A tensor of shape [T, B] that contains the loss per example, per time step.
"""
with tf.variable_scope('sequence_loss'):
losses = tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=logits, labels=targets)
# Mask out the losses we don't care about
loss_mask = tf.sequence_mask(
tf.cast(sequence_length, tf.int32),
tf.cast(tf.shape(targets)[1], tf.int32)
)
losses = losses * tf.cast(loss_mask, tf.float32)
return losses
def sampled_softmax_cross_entropy(output_placeholder, feature_hidden, logits,
vector_labels, class_weights,
class_biases, loss, num_classes):
output_exp = tf.cast(tf.expand_dims(output_placeholder, -1), tf.int64)
if loss['sampler'] == 'fixed_unigram':
sampled_values = tf.nn.fixed_unigram_candidate_sampler(
true_classes=output_exp,
num_true=1,
num_sampled=loss['negative_samples'],
unique=loss['unique'],
range_max=num_classes,
unigrams=loss['class_counts'],
distortion=loss['distortion']
)
elif loss['sampler'] == 'uniform':
sampled_values = tf.nn.uniform_candidate_sampler(
true_classes=output_exp,
num_true=1,
num_sampled=loss['negative_samples'],
unique=loss['unique'],
range_max=num_classes
)
elif loss['sampler'] == 'log_uniform':
sampled_values = tf.nn.log_uniform_candidate_sampler(
true_classes=output_exp,
num_true=1,
num_sampled=loss['negative_samples'],
unique=loss['unique'],
range_max=num_classes
)
elif loss['sampler'] == 'learned_unigram':
sampled_values = tf.nn.fixed_unigram_candidate_sampler(
true_classes=output_exp,
num_true=1,
num_sampled=loss['negative_samples'],
unique=loss['unique'],
range_max=num_classes,
unigrams=loss['class_counts'],
distortion=loss['distortion']
)
else:
raise ValueError('Unsupported sampler {}'.format(loss['sampler']))
train_loss = tf.nn.sampled_softmax_loss(weights=tf.transpose(class_weights),
biases=class_biases,
labels=output_exp,
inputs=feature_hidden,
num_sampled=loss[
'negative_samples'],
num_classes=num_classes,
sampled_values=sampled_values)
eval_loss = tf.losses.softmax_cross_entropy(onehot_labels=vector_labels,
logits=logits,
label_smoothing=loss[
'labels_smoothing'],
reduction=Reduction.NONE)
return train_loss, eval_loss
def sequence_sampled_softmax_cross_entropy(targets, targets_sequence_length,
eval_logits, train_logits,
class_weights,
class_biases, loss,
num_classes):
batch_max_targets_sequence_length = tf.shape(targets)[1]
batch_max_train_logits_sequence_length = tf.shape(train_logits)[1]
difference_train = batch_max_targets_sequence_length - batch_max_train_logits_sequence_length
padded_train_logits = tf.pad(train_logits,
[[0, 0], [0, difference_train], [0, 0]])
batch_max_eval_logits_sequence_length = tf.shape(eval_logits)[1]
difference_eval = batch_max_targets_sequence_length - batch_max_eval_logits_sequence_length
padded_eval_logits = tf.pad(eval_logits,
[[0, 0], [0, difference_eval], [0, 0]])
# batch_max_seq_length = tf.shape(train_logits)[1]
# unpadded_targets = targets[:, :batch_max_seq_length]
# output_exp = tf.cast(tf.reshape(unpadded_targets, [-1, 1]), tf.int64)
output_exp = tf.cast(tf.reshape(targets, [-1, 1]), tf.int64)
if loss['sampler'] == 'fixed_unigram':
sampled_values = tf.nn.fixed_unigram_candidate_sampler(
true_classes=output_exp,
num_true=1,
num_sampled=loss['negative_samples'],
unique=loss['unique'],
range_max=num_classes,
unigrams=loss['class_counts'],
distortion=loss['distortion']
)
elif loss['sampler'] == 'uniform':
sampled_values = tf.nn.uniform_candidate_sampler(
true_classes=output_exp,
num_true=1,
num_sampled=loss['negative_samples'],
unique=loss['unique'],
range_max=num_classes
)
elif loss['sampler'] == 'log_uniform':
sampled_values = tf.nn.log_uniform_candidate_sampler(
true_classes=output_exp,
num_true=1,
num_sampled=loss['negative_samples'],
unique=loss['unique'],
range_max=num_classes
)
elif loss['sampler'] == 'learned_unigram':
sampled_values = tf.nn.fixed_unigram_candidate_sampler(
true_classes=output_exp,
num_true=1,
num_sampled=loss['negative_samples'],
unique=loss['unique'],
range_max=num_classes,
unigrams=loss['class_counts'],
distortion=loss['distortion']
)
else:
raise ValueError('Unsupported sampler {}'.format(loss['sampler']))
def _sampled_loss(labels, logits):
labels = tf.cast(labels, tf.int64)
labels = tf.reshape(labels, [-1, 1])
logits = tf.cast(logits, tf.float32)
return tf.cast(
tf.nn.sampled_softmax_loss(weights=tf.transpose(class_weights),
biases=class_biases,
labels=labels,
inputs=logits,
num_sampled=loss['negative_samples'],
num_classes=num_classes,
sampled_values=sampled_values),
tf.float32)
train_loss = tfa.seq2seq.sequence_loss(
padded_train_logits,
targets,
tf.sequence_mask(targets_sequence_length,
batch_max_targets_sequence_length, dtype=tf.float32),
average_across_timesteps=True,
average_across_batch=False,
softmax_loss_function=_sampled_loss
)
# batch_max_seq_length_eval = tf.shape(eval_logits)[1]
# unpadded_targets_eval = targets[:, :batch_max_seq_length_eval]
eval_loss = tfa.seq2seq.sequence_loss(
padded_eval_logits,
targets,
tf.sequence_mask(targets_sequence_length,
batch_max_targets_sequence_length, dtype=tf.float32),
average_across_timesteps=True,
average_across_batch=False
)
return train_loss, eval_loss
def weighted_softmax_cross_entropy(logits, vector_labels, loss):
use_class_weights = not isinstance(loss['class_weights'], (int, float))
if use_class_weights:
train_loss = softmax_cross_entropy_with_class_weighting(
logits,
vector_labels,
loss['class_weights'],
loss['labels_smoothing']
)
else:
train_loss = tf.losses.softmax_cross_entropy(
onehot_labels=vector_labels,
logits=logits,
label_smoothing=loss[
'labels_smoothing'],
reduction=Reduction.NONE)
return train_loss
def loss_multilabel(logits, vector_labels, loss):
# input: `logits` and `labels` must have the same shape `[batch_size, num_classes]`
# output: A 1-D `Tensor` of length `batch_size` of the same type as `logits` with the softmax cross entropy loss.
# let `x = logits`, `z = labels`. The logistic loss is:z * -log(sigmoid(x)) + (1 - z) * -log(1 - sigmoid(x))
use_class_weights = not isinstance(loss['class_weights'], (int, float))
if use_class_weights:
train_loss = sigmoid_cross_entropy_with_class_weighting(
logits,
vector_labels,
loss['class_weights'],
loss['labels_smoothing']
)
else:
train_loss = tf.losses.sigmoid_cross_entropy(
multi_class_labels=vector_labels,
logits=logits,
label_smoothing=loss[
'labels_smoothing'],
reduction=Reduction.NONE)
return train_loss
def absolute_loss(y, y_hat):
return tf.abs(tf.subtract(y, y_hat))
def squared_loss(y, y_hat):
return tf.square(tf.subtract(y, y_hat))
def mean_absolute_error(y, y_hat, weight=1.0):
return tf.reduce_mean(tf.multiply(absolute_loss(y, y_hat), weight))
def mean_squared_error(y, y_hat, weight=1.0):
return tf.reduce_mean(tf.multiply(squared_loss(y, y_hat), weight))
# todo tf2: fix this!
# regularizer_registry = {'l1': tf2.keras.regularizers.l1,
# 'l2': tf2.keras.regularizers.l2,
# 'l1_l2': tf2.keras.regularizers.l1_l2,
# 'None': lambda x: None,
# None: lambda x: None}
regularizer_registry = {'l1': lambda x: None,
'l2': lambda x: None,
'l1_l2': lambda x: None,
'None': lambda x: None,
None: lambda x: None}