def testOpClip(self):
x = tf.placeholder(tf.float32, shape=[2, 1])
y = snt.clip_gradient(x, 2, 3)
z = tf.reduce_sum(y * y)
dzdy = tf.gradients(z, y)[0]
dzdx = tf.gradients(z, x)[0]
x_np = np.array([[0.5], [2]])
with self.test_session() as sess:
y_np, dzdy_np, dzdx_np = sess.run([y, dzdy, dzdx], feed_dict={x: x_np})
self.assertAllEqual(y_np, x_np)
# We do not expect the gradients with respect to the output to be clipped.
self.assertAllEqual(dzdy_np, np.array([[1], [4]]))
# We expect the gradients with respect to the input to be clipped [2, 3].
self.assertAllEqual(dzdx_np, np.array([[2], [3]]))
评论列表
文章目录