def testL2Normalize(self):
x_shape = [20]
np.random.seed(1)
x_np = np.random.random_sample(x_shape).astype(np.float32)
for dim in range(len(x_shape)):
y_np = self._l2Normalize(x_np, dim)
with self.test_session():
x_tf = tf.constant(x_np, name="x")
y_tf = tf.nn.l2_normalize(x_tf, dim)
self.assertAllClose(y_np, y_tf.eval())
# def testL2NormalizeGradient(self):
# x_shape = [20, 7, 3]
# np.random.seed(1)
# x_np = np.random.random_sample(x_shape).astype(np.float64)
# for dim in range(len(x_shape)):
# with self.test_session():
# x_tf = tf.constant(x_np, name="x")
# y_tf = tf.nn.l2_normalize(x_tf, dim)
# err = tf.test.compute_gradient_error(x_tf, x_shape, y_tf, x_shape)
# print("L2Normalize gradient err = %g " % err)
# self.assertLess(err, 1e-4)
评论列表
文章目录