@@ -102,6 +102,23 @@ def mean_squared_error(output, target, is_mean=False):
102102 mse = tf .reduce_mean (tf .reduce_sum (tf .squared_difference (output , target ), [1 , 2 , 3 ]))
103103 return mse
104104
105+ def normalize_mean_squared_error (output , target ):
106+ """Return the TensorFlow expression of normalized mean-squre-error of two distributions.
107+
108+ Parameters
109+ ----------
110+ output : 2D or 4D tensor.
111+ target : 2D or 4D tensor.
112+ """
113+ with tf .name_scope ("mean_squared_error_loss" ):
114+ if output .get_shape ().ndims == 2 : # [batch_size, n_feature]
115+ nmse_a = tf .sqrt (tf .reduce_sum (tf .squared_difference (output , target ), axis = 1 ))
116+ nmse_b = tf .sqrt (tf .reduce_sum (tf .square (target ), axis = 1 ))
117+ elif output .get_shape ().ndims == 4 : # [batch_size, w, h, c]
118+ nmse_a = tf .sqrt (tf .reduce_sum (tf .squared_difference (output , target ), axis = [1 ,2 ,3 ]))
119+ nmse_b = tf .sqrt (tf .reduce_sum (tf .square (target ), axis = [1 ,2 ,3 ]))
120+ nmse = tf .reduce_mean (nmse_a / nmse_b )
121+ return nmse
105122
106123
107124def dice_coe (output , target , epsilon = 1e-10 ):
0 commit comments