-
Notifications
You must be signed in to change notification settings - Fork 0
/
losses.py
executable file
·34 lines (28 loc) · 1.19 KB
/
losses.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
import numpy as np
import tensorflow as tf
class LDAMLoss():
def __init__(self, cls_num_list, max_m=0.5, weight=None, s=30):
super(LDAMLoss, self).__init__()
m_list = 1.0 / np.sqrt(np.sqrt(cls_num_list))
m_list = m_list * (max_m / np.max(m_list))
m_list = tf.convert_to_tensor(m_list, dtype=tf.float32)
self.m_list = m_list
assert s > 0
self.s = s
self.weight = weight
self.n_classes = len(cls_num_list)
def __call__(self, target, x):
# contrary to pytorch implemenation, our labels are already one hot encoded
index_float = target
batch_m = tf.matmul(self.m_list[None, :], tf.transpose(index_float))
batch_m = tf.reshape(batch_m, (-1, 1))
x_m = x - batch_m
# if condition is true, return x_m[index], otherwise return x[index]
index_bool = tf.cast(index_float, tf.bool)
output = tf.where(index_bool, x_m, x)
labels = index_float
logits = output
#print("labels : \n", labels, "\n logits : \n", logits)
loss = tf.nn.softmax_cross_entropy_with_logits(
labels=labels, logits=logits*self.s)
return tf.reduce_mean(loss)