Skip to content

Commit f137801

Browse files
committed
release absolute difference error for 2d, 3d or 4d tensor
1 parent 17c29e8 commit f137801

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

docs/modules/cost.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ to the cost function.
121121
binary_cross_entropy
122122
mean_squared_error
123123
normalized_mean_square_error
124+
absolute_difference_error
124125
dice_coe
125126
dice_hard_coe
126127
iou_coe
@@ -146,14 +147,18 @@ Binary cross entropy
146147
-------------------------
147148
.. autofunction:: binary_cross_entropy
148149

149-
Mean squared error
150+
Mean squared error (L2)
150151
-------------------------
151152
.. autofunction:: mean_squared_error
152153

153154
Normalized mean square error
154155
--------------------------------
155156
.. autofunction:: normalized_mean_square_error
156157

158+
Absolute difference error (L1)
159+
--------------------------------
160+
.. autofunction:: absolute_difference_error
161+
157162
Dice coefficient
158163
-------------------------
159164
.. autofunction:: dice_coe

tensorlayer/cost.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,19 +113,50 @@ def normalized_mean_square_error(output, target):
113113
114114
Parameters
115115
----------
116-
output : 2D or 4D tensor.
117-
target : 2D or 4D tensor.
116+
output : 2D, 3D or 4D tensor i.e. [batch_size, n_feature], [batch_size, w, h] or [batch_size, w, h, c].
117+
target : 2D, 3D or 4D tensor.
118118
"""
119119
with tf.name_scope("mean_squared_error_loss"):
120120
if output.get_shape().ndims == 2: # [batch_size, n_feature]
121121
nmse_a = tf.sqrt(tf.reduce_sum(tf.squared_difference(output, target), axis=1))
122122
nmse_b = tf.sqrt(tf.reduce_sum(tf.square(target), axis=1))
123+
elif output.get_shape().ndims == 3: # [batch_size, w, h]
124+
nmse_a = tf.sqrt(tf.reduce_sum(tf.squared_difference(output, target), axis=[1,2]))
125+
nmse_b = tf.sqrt(tf.reduce_sum(tf.square(target), axis=[1,2]))
123126
elif output.get_shape().ndims == 4: # [batch_size, w, h, c]
124127
nmse_a = tf.sqrt(tf.reduce_sum(tf.squared_difference(output, target), axis=[1,2,3]))
125128
nmse_b = tf.sqrt(tf.reduce_sum(tf.square(target), axis=[1,2,3]))
126129
nmse = tf.reduce_mean(nmse_a / nmse_b)
127130
return nmse
128131

132+
def absolute_difference_error(output, target, is_mean=False):
133+
""" Return the TensorFlow expression of absolute difference error (L1) of two batch of data.
134+
135+
Parameters
136+
----------
137+
output : 2D, 3D or 4D tensor i.e. [batch_size, n_feature], [batch_size, w, h] or [batch_size, w, h, c].
138+
target : 2D, 3D or 4D tensor.
139+
is_mean : boolean, if True, use ``tf.reduce_mean`` to compute the loss of one data, otherwise, use ``tf.reduce_sum`` (default).
140+
"""
141+
with tf.name_scope("mean_squared_error_loss"):
142+
if output.get_shape().ndims == 2: # [batch_size, n_feature]
143+
if is_mean:
144+
loss = tf.reduce_mean(tf.reduce_mean(tf.abs(output - target), 1))
145+
else:
146+
loss = tf.reduce_mean(tf.reduce_sum(tf.abs(output - target), 1))
147+
elif output.get_shape().ndims == 3: # [batch_size, w, h]
148+
if is_mean:
149+
loss = tf.reduce_mean(tf.reduce_mean(tf.abs(output - target), [1, 2]))
150+
else:
151+
loss = tf.reduce_mean(tf.reduce_sum(tf.abs(output - target), [1, 2]))
152+
elif output.get_shape().ndims == 4: # [batch_size, w, h, c]
153+
if is_mean:
154+
loss = tf.reduce_mean(tf.reduce_mean(tf.abs(output - target), [1, 2, 3]))
155+
else:
156+
loss = tf.reduce_mean(tf.reduce_sum(tf.abs(output - target), [1, 2, 3]))
157+
else:
158+
raise Exception("Unknow dimension")
159+
return loss
129160

130161

131162
def dice_coe(output, target, loss_type='jaccard', axis=[1,2,3], smooth=1e-5):

0 commit comments

Comments
 (0)