| 81 | |
| 82 | |
| 83 | def crossentropy_metric(y_pred, y_true, void_labels, one_hot=False): |
| 84 | # Clip predictions |
| 85 | y_pred = T.clip(y_pred, _EPSILON, 1.0 - _EPSILON) |
| 86 | |
| 87 | if one_hot: |
| 88 | y_true = T.argmax(y_true, axis=1) |
| 89 | |
| 90 | # Create mask |
| 91 | mask = T.ones_like(y_true, dtype=_FLOATX) |
| 92 | for el in void_labels: |
| 93 | mask = T.set_subtensor(mask[T.eq(y_true, el).nonzero()], 0.) |
| 94 | |
| 95 | # Modify y_true temporarily |
| 96 | y_true_tmp = y_true * mask |
| 97 | y_true_tmp = y_true_tmp.astype('int32') |
| 98 | |
| 99 | # Compute cross-entropy |
| 100 | loss = T.nnet.categorical_crossentropy(y_pred, y_true_tmp) |
| 101 | |
| 102 | # Compute masked mean loss |
| 103 | loss *= mask |
| 104 | loss = T.sum(loss) / T.sum(mask) |
| 105 | |
| 106 | return loss |
| 107 | |
| 108 | |
| 109 | SAVEPATH = 'save_models/' |