Skip to content

Commit fe8ee1f

Browse files
beopstrmlarsen
authored andcommitted
Fix TypeError in gradient clipping ops (tensorflow#3707)
* Fix TypeError in gradient clipping ops * Fix whitespace issue
1 parent d03f254 commit fe8ee1f

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

‎tensorflow/python/kernel_tests/clip_ops_test.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,13 @@ def testClipByNormClipped(self):
5757
clip_norm = 4.0
5858
ans = tf.clip_by_norm(x, clip_norm)
5959
tf_ans = ans.eval()
60+
61+
clip_tensor = tf.constant(4.0)
62+
ans = tf.clip_by_norm(x, clip_norm)
63+
tf_ans_tensor = ans.eval()
6064

6165
self.assertAllClose(np_ans, tf_ans)
66+
self.assertAllClose(np_ans, tf_ans_tensor)
6267

6368
def testClipByNormNotClipped(self):
6469
# No norm clipping when clip_norm >= 5
@@ -148,6 +153,28 @@ def testClipByGlobalNormClipped(self):
148153
self.assertAllClose(np_ans_0, tf_ans_1)
149154
self.assertAllClose(np_ans_1, tf_ans_2)
150155

156+
def testClipByGlobalNormClippedTensor(self):
157+
# Norm clipping when clip_norm < 5
158+
with self.test_session():
159+
x0 = tf.constant([-2.0, 0.0, 0.0, 4.0, 0.0, 0.0], shape=[2, 3])
160+
x1 = tf.constant([1.0, -2.0])
161+
# Global norm of x0 and x1 = sqrt(1 + 4^2 + 2^2 + 2^2) = 5
162+
clip_norm = tf.constant(4.0)
163+
164+
# Answers are the original tensors scaled by 4.0/5.0
165+
np_ans_0 = [[-1.6, 0.0, 0.0],
166+
[3.2, 0.0, 0.0]]
167+
np_ans_1 = [0.8, -1.6]
168+
169+
ans, norm = tf.clip_by_global_norm((x0, x1), clip_norm)
170+
tf_ans_1 = ans[0].eval()
171+
tf_ans_2 = ans[1].eval()
172+
tf_norm = norm.eval()
173+
174+
self.assertAllClose(tf_norm, 5.0)
175+
self.assertAllClose(np_ans_0, tf_ans_1)
176+
self.assertAllClose(np_ans_1, tf_ans_2)
177+
151178
def testClipByGlobalNormSupportsNone(self):
152179
# Norm clipping when clip_norm < 5
153180
with self.test_session():
@@ -259,6 +286,19 @@ def testClipByAverageNormClipped(self):
259286

260287
self.assertAllClose(np_ans, tf_ans)
261288

289+
def testClipByAverageNormClippedTensor(self):
290+
# Norm clipping when average clip_norm < 0.83333333
291+
with self.test_session():
292+
x = tf.constant([-3.0, 0.0, 0.0, 4.0, 0.0, 0.0], shape=[2, 3])
293+
# Average norm of x = sqrt(3^2 + 4^2) / 6 = 0.83333333
294+
np_ans = [[-2.88, 0.0, 0.0],
295+
[3.84, 0.0, 0.0]]
296+
clip_norm = tf.constant(0.8)
297+
ans = tf.clip_by_average_norm(x, clip_norm)
298+
tf_ans = ans.eval()
299+
300+
self.assertAllClose(np_ans, tf_ans)
301+
262302
def testClipByAverageNormNotClipped(self):
263303
# No norm clipping when average clip_norm >= 0.83333333
264304
with self.test_session():

‎tensorflow/python/ops/clip_ops.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def clip_by_global_norm(t_list, clip_norm, use_norm=None, name=None):
206206
# Calculate L2-norm, clip elements by ratio of clip_norm to L2-norm
207207
scale = clip_norm * math_ops.minimum(
208208
1.0 / use_norm,
209-
constant_op.constant(1.0 / clip_norm, dtype=use_norm.dtype))
209+
constant_op.constant(1.0, dtype=use_norm.dtype) / clip_norm)
210210

211211
values = [
212212
ops.convert_to_tensor(
@@ -268,7 +268,7 @@ def clip_by_average_norm(t, clip_norm, name=None):
268268
math_ops.reduce_sum(t * t, math_ops.range(array_ops.rank(t))))
269269
tclip = array_ops.identity(
270270
t * clip_norm * math_ops.minimum(
271-
l2norm_inv * n_element, constant_op.constant(1.0 / clip_norm)),
271+
l2norm_inv * n_element, constant_op.constant(1.0) / clip_norm),
272272
name=name)
273273

274274
return tclip

0 commit comments

Comments
 (0)