-
Notifications
You must be signed in to change notification settings - Fork 101
/
losses.py
60 lines (42 loc) · 1.8 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
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
import theano
import theano.tensor as T
def log_loss(y, t, eps=1e-15):
"""
cross entropy loss, summed over classes, mean over batches
"""
y = T.clip(y, eps, 1 - eps)
loss = -T.sum(t * T.log(y)) / y.shape[0].astype(theano.config.floatX)
return loss
def accuracy_loss(y, t, eps=1e-15):
y_ = T.cast(T.argmax(y, axis=1), 'int32')
t_ = T.cast(T.argmax(t, axis=1), 'int32')
# predictions = T.argmax(y, axis=1)
return -T.mean(T.switch(T.eq(y_, t_), 1, 0))
def quad_kappa_loss(y, t, y_pow=1, eps=1e-15):
num_scored_items = y.shape[0]
num_ratings = 5
tmp = T.tile(T.arange(0, num_ratings).reshape((num_ratings, 1)),
reps=(1, num_ratings)).astype(theano.config.floatX)
weights = (tmp - tmp.T) ** 2 / (num_ratings - 1) ** 2
y_ = y ** y_pow
y_norm = y_ / (eps + y_.sum(axis=1).reshape((num_scored_items, 1)))
hist_rater_a = y_norm.sum(axis=0)
hist_rater_b = t.sum(axis=0)
conf_mat = T.dot(y_norm.T, t)
nom = T.sum(weights * conf_mat)
denom = T.sum(weights * T.dot(hist_rater_a.reshape((num_ratings, 1)),
hist_rater_b.reshape((1, num_ratings))) /
num_scored_items.astype(theano.config.floatX))
return - (1 - nom / denom)
def quad_kappa_log_hybrid_loss(y, t, y_pow=1, log_scale=0.5, log_offset=0.50):
log_loss_res = log_loss(y, t)
kappa_loss_res = quad_kappa_loss(y, t, y_pow=y_pow)
return kappa_loss_res + log_scale * (log_loss_res - log_offset)
def quad_kappa_log_hybrid_loss_clipped(
y, t, y_pow=1, log_cutoff=0.9, log_scale=0.5):
log_loss_res = log_loss(y, t)
kappa_loss_res = quad_kappa_loss(y, t, y_pow=y_pow)
return kappa_loss_res + log_scale * \
T.clip(log_loss_res, log_cutoff, 10 ** 3)
def mse(y, t):
return T.mean((y - t) ** 2)