def testConstructGradientWithLargeVolumes(self, use_tape): with test_util.AbstractGradientTape(use_tape=use_tape) as tape: batch_size = 4 planes = 8 height = 32 width = 32 ksize = 5 shape = (batch_size, planes, height, width, 1) volumes = variables.Variable( np.random.uniform(size=np.prod(shape)).reshape(shape), name='inputs') tape.watch(volumes) patches = array_ops.extract_volume_patches( volumes, ksizes=[1, ksize, ksize, ksize, 1], strides=[1, 1, 1, 1, 1], padding='SAME') # Github issue: #20146 # tf.extract_volume_patches() gradient very slow at graph construction # time. gradients = tape.gradient(patches, volumes) # Won't time out. self.assertIsNotNone(gradients)
def testGradientsRank7SliceUpdate(self, use_tape): for dtype in GRADIENT_TESTS_DTYPES: with test_util.AbstractGradientTape(use_tape=use_tape) as tape: indices = constant_op.constant( [[[[[[[0, 0, 0, 0, 0, 1], [0, 0, 1, 0, 0, 0]]]], [[[[0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 1]]]]]]], dtype=dtypes.int32) updates = constant_op.constant( [[[[[[[5, 6], [2, 4]]]], [[[[1, 3], [6, 8]]]]]]], dtype=dtype) tape.watch(updates) shape = constant_op.constant([1, 1, 2, 1, 1, 2, 2], dtype=dtypes.int32) input_ = array_ops.zeros(shape, dtype=dtype) tape.watch(input_) outputs = self.scatter_nd(indices, updates, shape, input_) grad_vals = constant_op.constant( [[[[[[[1, 2], [3, 4]]]], [[[[5, 6], [7, 8]]]]]]], dtype=dtype) updates_grad, input_grad = tape.gradient([outputs], [updates, input_], [grad_vals]) expected_updates_grad = np.array( [[[[[[[3, 4], [5, 6]]]], [[[[1, 2], [7, 8]]]]]]], dtype=dtype.as_numpy_dtype()) expected_input_grad = np.array( [[[[[[[1, 2], [3, 4]]]], [[[[5, 6], [7, 8]]]]]]], dtype=dtype.as_numpy_dtype()) self.assertAllEqual(expected_updates_grad, self.evaluate(updates_grad)) if self.non_aliasing_add_test: self.assertAllEqual(expected_input_grad, self.evaluate(input_grad))
def _testShapesParameterized(self, use_tape): TEST_CASES = [[1, 1], [2, 3], [5, 4]] # pylint: disable=invalid-name for batch_size, channel_count in TEST_CASES: smaller_shape = [batch_size, 2, 3, channel_count] larger_shape = [batch_size, 4, 6, channel_count] for in_shape, out_shape, _, _ in self._itGen( smaller_shape, larger_shape): with test_util.AbstractGradientTape(use_tape=use_tape) as tape: # Input values should not influence shapes x = np.arange(np.prod(in_shape)).reshape(in_shape).astype( np.float32) input_tensor = constant_op.constant(x, shape=in_shape) tape.watch(input_tensor) resized_tensor = image_ops.resize_bilinear( input_tensor, out_shape[1:3]) self.assertEqual(out_shape, list(resized_tensor.get_shape())) grad_tensor = tape.gradient(resized_tensor, input_tensor) self.assertEqual(in_shape, list(grad_tensor.get_shape())) with self.cached_session(): resized_values = self.evaluate(resized_tensor) self.assertEqual(out_shape, list(resized_values.shape)) grad_values = self.evaluate(grad_tensor) self.assertEqual(in_shape, list(grad_values.shape))
def testNoIntegerGradient3(self, use_tape): with test_util.AbstractGradientTape(use_tape=use_tape) as tape: k = constant_op.constant([3, 4]) tape.watch(k) m = k * k dm_dk = tape.gradient(m, k) self.assertIsNone(dm_dk)
def testNoIntegerGradient1(self, use_tape): with test_util.AbstractGradientTape(use_tape=use_tape) as tape: x = constant_op.constant([3.9, 4.1]) tape.watch(x) k = math_ops.cast(math_ops.cast(x, dtypes.int32), dtypes.float32) y = k * k dy_dx = tape.gradient(y, x) self.assertIsNone(dy_dx)
def testNoIntegerGradient2(self, use_tape): with test_util.AbstractGradientTape(use_tape=use_tape) as tape: k = constant_op.constant([3, 4]) x = math_ops.cast(k, dtypes.float32) tape.watch([k, x]) y = x * x dy_dk = tape.gradient(y, k) self.assertIsNone(dy_dk)
def testGradientWithIntegerPath(self, use_tape): with test_util.AbstractGradientTape(use_tape=use_tape) as tape: x = constant_op.constant([3.9, 4.1]) tape.watch(x) k = math_ops.cast(math_ops.cast(x, dtypes.int32), dtypes.float32) y = x * k dy_dx = tape.gradient(y, x) self.assertAllClose([3., 4.], self.evaluate(dy_dx))
def testMultipleOutputChainedGradients(self, use_tape): with test_util.AbstractGradientTape(use_tape=use_tape) as tape: x = constant_op.constant(1.0, dtype=dtypes.float32) tape.watch(x) yexp = math_ops.exp(x) yexplog = math_ops.log(yexp) grads = tape.gradient([yexp, yexplog], [x]) grad_vals = self.evaluate(grads) exp1_plus_one = (1.0 + np.exp(1.0)).astype(np.float32) # [dexp(x)/dx + d(log(exp(x)))/dx] @ x=1 == exp(1) + 1 self.assertAllClose(grad_vals[0], exp1_plus_one)
def testGradOnUnsupportedType(self, use_tape): in_shape = [1, 4, 6, 1] out_shape = [1, 2, 3, 1] with test_util.AbstractGradientTape(use_tape=use_tape) as tape: x = np.arange(0, 24).reshape(in_shape).astype(np.uint8) input_tensor = constant_op.constant(x, shape=in_shape) tape.watch(input_tensor) resize_out = image_ops.resize_bilinear(input_tensor, out_shape[1:3]) with self.cached_session(): grad = tape.gradient(resize_out, [input_tensor]) self.assertEqual([None], grad)
def testNoIntegerGradient6(self, use_tape): with test_util.AbstractGradientTape(use_tape=use_tape, persistent=True) as tape: k = constant_op.constant(3) tape.watch(k) x = math_ops.cast(k, dtypes.float32) grad_1 = tape.gradient(k * k, k) grad_2 = tape.gradient(x * x, k) grad_3 = tape.gradient(math_ops.square(k), k) grad_4 = tape.gradient(math_ops.square(x), k) self.assertIsNone(grad_1) self.assertIsNone(grad_2) self.assertIsNone(grad_3) self.assertIsNone(grad_4)
def testConstructGradientWithLargeImages(self, use_tape): with test_util.AbstractGradientTape(use_tape=use_tape) as tape: batch_size = 4 # Prevent OOM by setting reasonably large image size (b/171808681). height = 512 width = 512 ksize = 5 shape = (batch_size, height, width, 1) images = variables.Variable( np.random.uniform(size=np.prod(shape)).reshape(shape), name='inputs') tape.watch(images) patches = array_ops.extract_image_patches( images, ksizes=[1, ksize, ksize, 1], strides=[1, 1, 1, 1], rates=[1, 1, 1, 1], padding='SAME') # Github issue: #20146 # tf.image.extract_image_patches() gradient very slow at graph # construction time. gradients = tape.gradient(patches, images) # Won't time out. self.assertIsNotNone(gradients)
def testIntegerIdentityGradient(self, use_tape): x = constant_op.constant(3) with test_util.AbstractGradientTape(use_tape=use_tape) as tape: tape.watch(x) dx_dx = tape.gradient(x, x) self.assertAllClose(1, self.evaluate(dx_dx))