def weighted(y_true, y_pred, weights, mask=None): # score_array has ndim >= 2 score_array = fn(y_true, y_pred) if mask is not None: # Cast the mask to floatX to avoid float64 upcasting in theano mask = K.cast(mask, K.floatX) # mask should have the same shape as score_array score_array *= mask # the loss per batch should be proportional # to the number of unmasked samples. score_array /= K.mean(mask) # reduce score_array to 1D ndim = K.ndim(score_array) for _ in range(ndim-1): score_array = K.mean(score_array, axis=-1) if weights is not None: score_array *= weights return K.mean(score_array)
def weighted(y_true, y_pred, weights, mask=None): # score_array has ndim >= 2 score_array = fn(y_true, y_pred) if mask is not None: # Cast the mask to floatX to avoid float64 upcasting in theano mask = K.cast(mask, K.floatX) # mask should have the same shape as score_array score_array *= mask # the loss per batch should be proportional # to the number of unmasked samples. score_array /= K.mean(mask) # reduce score_array to 1D ndim = K.ndim(score_array) for _ in range(ndim - 1): score_array = K.mean(score_array, axis=-1) if weights is not None: score_array *= weights return K.mean(score_array)
def __call__(self, p): p *= K.cast(p >= 0., K.floatX) return p
def __call__(self, p): p *= K.cast(p >= 0., K.floatX) return p