-
Notifications
You must be signed in to change notification settings - Fork 0
/
loss_accuracy.py
44 lines (31 loc) · 1.23 KB
/
loss_accuracy.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
# import the necessary packages
import tensorflow as tf
from tensorflow.keras.losses import SparseCategoricalCrossentropy
def masked_loss(label, prediction):
# mask positions where the label is not equal to 0
mask = label != 0
# build the loss object and apply it to the labels
lossObject = SparseCategoricalCrossentropy(from_logits=True, reduction="none")
loss = lossObject(label, prediction)
# mask the loss
mask = tf.cast(mask, dtype=loss.dtype)
loss *= mask
# average the loss over the batch and return it
loss = tf.reduce_sum(loss) / tf.reduce_sum(mask)
return loss
def masked_accuracy(label, prediction):
# mask positions where the label is not equal to 0
mask = label != 0
# get the argmax from the logits
prediction = tf.argmax(prediction, axis=2)
# cast the label into the prediction datatype
label = tf.cast(label, dtype=prediction.dtype)
# calculate the matches
match = label == prediction
match = match & mask
# cast the match and masks
match = tf.cast(match, dtype=tf.float32)
mask = tf.cast(mask, dtype=tf.float32)
# average the match over the batch and return it
match = tf.reduce_sum(match) / tf.reduce_sum(mask)
return match