def weighted_objective(fn):
 def weighted(y_true, y_pred, weights, mask=None):
  score_array = fn(y_true, y_pred)
  if (mask is not None):
   mask = K.cast(mask, K.floatx())
   score_array *= mask
   score_array /= K.mean(mask)
  ndim = K.ndim(score_array)
  weight_ndim = K.ndim(weights)
  score_array = K.mean(score_array, axis=list(range(weight_ndim, ndim)))
  if (weights is not None):
   score_array *= weights
   score_array /= K.mean(K.cast(K.not_equal(weights, 0), K.floatx()))
  return K.mean(score_array)
 return weighted

def weighted_objective ( fn ) : def weighted ( y_true , y_pred , weights , mask = None ) : score_array = fn ( y_true , y_pred ) if ( mask is not None ) : mask = K . cast ( mask , K . floatx ( ) ) score_array *= mask score_array /= K . mean ( mask ) ndim = K . ndim ( score_array ) weight_ndim = K . ndim ( weights ) score_array = K . mean ( score_array , axis = list ( range ( weight_ndim , ndim ) ) ) if ( weights is not None ) : score_array *= weights score_array /= K . mean ( K . cast ( K . not_equal ( weights , 0 ) , K . floatx ( ) ) ) return K . mean ( score_array ) return weighted
transforms an objective function fn into a sample-weighted .