my_losses.py
Go to the documentation of this file.
1 import keras.backend as K
2 from keras import losses
3 
4 def masked_loss(y_true, y_pred):
5  mask_value = -1
6  mask = K.cast(K.not_equal(y_true, mask_value), K.floatx())
7 
8  return multitask_loss(y_true * mask, y_pred * mask)
9 
10 def masked_loss_binary(y_true, y_pred):
11  mask_value = -1
12  mask = K.cast(K.not_equal(y_true, mask_value), K.floatx())
13 
14  return loss_binary_crossentropy(y_true * mask, y_pred * mask)
15 
16 def masked_loss_categorical(y_true, y_pred):
17  mask_value = -1
18  mask = K.cast(K.not_equal(y_true, mask_value), K.floatx())
19 
20  return loss_categorical_crossentropy(y_true * mask, y_pred * mask)
21 
22 def loss_binary_crossentropy(y_true, y_pred):
23  # Avoid divide by 0
24  y_pred = K.clip(y_pred, K.epsilon(), 1 - K.epsilon())
25 
26  return losses.binary_crossentropy(y_true, y_pred)
27 
28 def loss_categorical_crossentropy(y_true, y_pred):
29  # Avoid divide by 0
30  y_pred = K.clip(y_pred, K.epsilon(), 1 - K.epsilon())
31 
32  return losses.categorical_crossentropy(y_true, y_pred)
33 
34 
35 def multitask_loss(y_true, y_pred):
36  # Avoid divide by 0
37  y_pred = K.clip(y_pred, K.epsilon(), 1 - K.epsilon())
38 
39  # Multi-task loss
40  return K.mean(K.sum(- y_true * K.log(y_pred) - (1 - y_true) * K.log(1 - y_pred), axis=1))
41 
42 def multitask_loss_weighted(y_true, y_pred):
43  # Avoid divide by 0
44  y_pred = K.clip(y_pred, K.epsilon(), 1 - K.epsilon())
45 
46  print "...WEIGHTS..."
47  #weights = K.cast([1.5, 1.5, 1.5, 1.5, 0.75, 0.75, 0.75, 0.75], K.floatx())
48  #weights = K.cast([1.75, 1.75, 1.75, 1.75, 0.5, 0.5, 0.5, 0.5], K.floatx())
49  weights = K.cast([5.0*0.67049114189459802, 5.0*0.6574572346004135, 5.0*2.4884334247030484, 5.0*1.7074017025820696,
50  0.5*1.1687034551437478, 0.5*0.8630824121263583, 0.5*0.6372003918709082, 0.5*2.4018367377204779], K.floatx())
51 
52  # Multi-task loss
53  return K.mean(K.sum((- y_true * K.log(y_pred) - (1 - y_true) * K.log(1 - y_pred)) * weights, axis=1))
54 
55 
def multitask_loss_weighted(y_true, y_pred)
Definition: my_losses.py:42
def masked_loss_categorical(y_true, y_pred)
Definition: my_losses.py:16
def multitask_loss(y_true, y_pred)
Definition: my_losses.py:35
def masked_loss(y_true, y_pred)
Definition: my_losses.py:4
def masked_loss_binary(y_true, y_pred)
Definition: my_losses.py:10
def loss_binary_crossentropy(y_true, y_pred)
Definition: my_losses.py:22
def loss_categorical_crossentropy(y_true, y_pred)
Definition: my_losses.py:28