MCPcopy Create free account
hub / github.com/lisa-lab/DeepLearningTutorials / crossentropy_metric

Function crossentropy_metric

code/fcn_2D_segm/train_fcn8.py:80–103  ·  view source on GitHub ↗
(y_pred, y_true, void_labels, one_hot=False)

Source from the content-addressed store, hash-verified

78
79
80def 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
106SAVEPATH = 'save_models/'

Callers 1

trainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected