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