def _StridedSliceGrad(op, grad): """Gradient for StridedSlice op.""" begin = op.inputs[1] end = op.inputs[2] strides = op.inputs[3] # StridedSliceGrad requires `x`, `begin`, `end` and `strides` to be of the # same dtype so we build a shape of the same type as other args. # Note that the choice of `begin` for specifying `out_type` is arbitrary. # We could choose any of {begin|end|strides}.dtype since they are required to # be the same. x = array_ops.shape(op.inputs[0], out_type=begin.dtype) x_static = tensor_util.constant_value(x) x = x_static if x_static is not None else x begin_static = tensor_util.constant_value(begin) begin = begin_static if begin_static is not None else begin end_static = tensor_util.constant_value(end) end = end_static if end_static is not None else end strides_static = tensor_util.constant_value(strides) strides = strides_static if strides_static is not None else strides return array_ops.strided_slice_grad( x, begin, end, strides, grad, begin_mask=op.get_attr("begin_mask"), end_mask=op.get_attr("end_mask"), ellipsis_mask=op.get_attr("ellipsis_mask"), new_axis_mask=op.get_attr("new_axis_mask"), shrink_axis_mask=op.get_attr("shrink_axis_mask")), None, None, None
def testInt64Shape(self): with self.test_session(use_gpu=True) as sess: original_dy = tf.reshape(tf.cast(tf.range(1, 5, 1), tf.float32), shape=(4, 1, 1)) original_shape = tf.constant([6, 4, 4], dtype=tf.int64) sess.run(tf.global_variables_initializer()) begin = tf.constant([0, 0, 0], dtype=tf.int64) end = tf.constant([4, 1, 1], dtype=tf.int64) strides = tf.constant([1, 1, 1], dtype=tf.int64) dx = array_ops.strided_slice_grad(original_shape, begin, end, strides, original_dy) sess.run(dx)
def testHostVsDevice(self): with self.test_session(use_gpu=True) as sess: var2 = tf.Variable(tf.reshape(tf.cast(tf.range(1, 5, 1), tf.float32), shape=(4, 1, 1))) varshape = tf.Variable([6, 4, 4], dtype=tf.int32) sess.run(tf.global_variables_initializer()) begin = tf.constant([0, 0, 0]) end = tf.constant([4, 1, 1]) strides = tf.constant([1, 1, 1]) foo = array_ops.strided_slice_grad(varshape, begin, end, strides, var2) sess.run(foo)
def testInt64Shape(self): with self.test_session(use_gpu=True) as sess: original_dy = tf.reshape(tf.cast(tf.range(1, 5, 1), tf.float32), shape=(4, 1, 1)) original_shape = tf.constant([6, 4, 4], dtype=tf.int64) sess.run(tf.initialize_all_variables()) begin = tf.constant([0, 0, 0], dtype=tf.int64) end = tf.constant([4, 1, 1], dtype=tf.int64) strides = tf.constant([1, 1, 1], dtype=tf.int64) dx = array_ops.strided_slice_grad(original_shape, begin, end, strides, original_dy) sess.run(dx)
def testHostVsDevice(self): with self.test_session(use_gpu=True) as sess: var2 = tf.Variable( tf.reshape( tf.cast(tf.range(1, 5, 1), tf.float32), shape=(4, 1, 1))) varshape = tf.Variable([6, 4, 4], dtype=tf.int32) sess.run(tf.initialize_all_variables()) begin = tf.constant([0, 0, 0]) end = tf.constant([4, 1, 1]) strides = tf.constant([1, 1, 1]) foo = array_ops.strided_slice_grad(varshape, begin, end, strides, var2) sess.run(foo)
def testInt64Shape(self): with self.test_session(use_gpu=True) as sess: original_dy = array_ops.reshape( math_ops.cast(math_ops.range(1, 5, 1), dtypes.float32), shape=(4, 1, 1)) original_shape = constant_op.constant([6, 4, 4], dtype=dtypes.int64) sess.run(variables.global_variables_initializer()) begin = constant_op.constant([0, 0, 0], dtype=dtypes.int64) end = constant_op.constant([4, 1, 1], dtype=dtypes.int64) strides = constant_op.constant([1, 1, 1], dtype=dtypes.int64) dx = array_ops.strided_slice_grad(original_shape, begin, end, strides, original_dy) sess.run(dx)
def testHostVsDevice(self): with self.test_session(use_gpu=True) as sess: var2 = variables.Variable( array_ops.reshape( math_ops.cast(math_ops.range(1, 5, 1), dtypes.float32), shape=(4, 1, 1))) varshape = variables.Variable([6, 4, 4], dtype=dtypes.int32) sess.run(variables.global_variables_initializer()) begin = constant_op.constant([0, 0, 0]) end = constant_op.constant([4, 1, 1]) strides = constant_op.constant([1, 1, 1]) foo = array_ops.strided_slice_grad(varshape, begin, end, strides, var2) sess.run(foo)
def testMixedIndexTypes(self): with self.test_session(use_gpu=True) as sess: original_dy = tf.reshape( tf.cast(tf.range(1, 5, 1), tf.float32), shape=(4, 1, 1)) original_shape = tf.constant([6, 4, 4], dtype=tf.int64) sess.run(tf.initialize_all_variables()) begin = tf.constant([0, 0, 0], dtype=tf.int32) end = tf.constant([4, 1, 1], dtype=tf.int64) strides = tf.constant([1, 1, 1], dtype=tf.int64) with self.assertRaisesRegexp( TypeError, "Input 'begin' of 'StridedSliceGrad' Op has type int32" " that does not match type int64 of argument 'shape'"): dx = array_ops.strided_slice_grad(original_shape, begin, end, strides, original_dy) sess.run(dx)
def testStridedSliceGradWithNonConstAxis(self): if test.is_gpu_available(cuda_only=True): random_seed.set_random_seed(0) x = random_ops.truncated_normal([1, 784], seed=0) conv = _two_layer_model(x) end = array_ops.placeholder(dtype='int32') shape = array_ops.shape(conv) end_val = [1, 2, 3, 4] s = array_ops.strided_slice( conv, [0, 0, 0, 0], end_val, strides=[1, 2, 3, 1]) s_grad = array_ops.strided_slice_grad(shape, [0, 0, 0, 0], end, [1, 2, 3, 1], s) output = array_ops.identity(s_grad) with session.Session() as sess: output_val_ref = sess.run(output, feed_dict={end: end_val}) with session.Session(config=_get_config()) as sess: metadata = config_pb2.RunMetadata() output_val = sess.run( output, run_metadata=metadata, feed_dict={ end: end_val }) nodes = [] num_transposes = 0 for node in metadata.cost_graph.node: if node.name.startswith('LayoutOptimizerTranspose'): num_transposes += 1 nodes.append(node.name) # Four transposes were initially added in the Expand phase of # LayoutOptimizer; two of them are cancelled out in the Collapse phase. expected_num_transposes = 2 self.assertEqual(expected_num_transposes, num_transposes) self.assertIn('LayoutOptimizerTransposeNHWCToNCHW-Conv2D-0', nodes) self.assertIn('LayoutOptimizerTransposeNCHWToNHWC-StridedSliceGrad-0-0', nodes) self.assertIn('LayoutOptimizerVecPermuteNHWCToNCHW_StridedSliceGrad_2', nodes) self.assertIn('LayoutOptimizer-StridedSlice-StridedSliceGrad/begin', nodes) self.assertIn('LayoutOptimizer-StridedSlice-StridedSliceGrad/strides', nodes) self.assertAllClose(output_val_ref, output_val, atol=1e-3)
def _StridedSliceGrad(op, grad): """Gradient for StridedSlice op.""" x = array_ops.shape(op.inputs[0]) begin = op.inputs[1] end = op.inputs[2] strides = op.inputs[3] return array_ops.strided_slice_grad( x, begin, end, strides, grad, begin_mask=op.get_attr("begin_mask"), end_mask=op.get_attr("end_mask"), ellipsis_mask=op.get_attr("ellipsis_mask"), new_axis_mask=op.get_attr("new_axis_mask"), shrink_axis_mask=op.get_attr("shrink_axis_mask")), None, None, None
def _StridedSliceGrad(op, grad): """Gradient for StridedSlice op.""" begin = op.inputs[1] end = op.inputs[2] strides = op.inputs[3] # StridedSliceGrad requires `x`, `begin`, `end` and `strides` to be of the # same dtype so we build a shape of the same type as other args. # Note that the choice of `begin` for specifying `out_type` is arbitrary. # We could choose any of {begin|end|strides}.dtype since they are required to # be the same. x = array_ops.shape(op.inputs[0], out_type=begin.dtype) return array_ops.strided_slice_grad( x, begin, end, strides, grad, begin_mask=op.get_attr("begin_mask"), end_mask=op.get_attr("end_mask"), ellipsis_mask=op.get_attr("ellipsis_mask"), new_axis_mask=op.get_attr("new_axis_mask"), shrink_axis_mask=op.get_attr("shrink_axis_mask")), None, None, None
def testStridedSliceGrad(self): # Tests cases where input shape is empty. self._testNAry(lambda x: array_ops.strided_slice_grad(*x), [ np.array([], dtype=np.int32), np.array([], dtype=np.int32), np.array([], dtype=np.int32), np.array([], dtype=np.int32), np.float32(0.5) ], expected=np.array(np.float32(0.5), dtype=np.float32)) # Tests case where input shape is non-empty, but gradients are empty. self._testNAry(lambda x: array_ops.strided_slice_grad(*x), [ np.array([3], dtype=np.int32), np.array([0], dtype=np.int32), np.array([0], dtype=np.int32), np.array([1], dtype=np.int32), np.array([], dtype=np.float32) ], expected=np.array([0, 0, 0], dtype=np.float32)) self._testNAry(lambda x: array_ops.strided_slice_grad(*x), [ np.array([3, 0], dtype=np.int32), np.array([1, 0], dtype=np.int32), np.array([3, 0], dtype=np.int32), np.array([1, 1], dtype=np.int32), np.array([[], []], dtype=np.float32) ], expected=np.array([[], [], []], dtype=np.float32)) self._testNAry(lambda x: array_ops.strided_slice_grad(*x), [ np.array([3, 3], dtype=np.int32), np.array([1, 1], dtype=np.int32), np.array([3, 3], dtype=np.int32), np.array([1, 1], dtype=np.int32), np.array([[5, 6], [8, 9]], dtype=np.float32) ], expected=np.array([[0, 0, 0], [0, 5, 6], [0, 8, 9]], dtype=np.float32)) def ssg_test(x): return array_ops.strided_slice_grad(*x, shrink_axis_mask=0x4, new_axis_mask=0x1) self._testNAry(ssg_test, [ np.array([3, 1, 3], dtype=np.int32), np.array([0, 0, 0, 2], dtype=np.int32), np.array([0, 3, 1, -4], dtype=np.int32), np.array([1, 2, 1, -3], dtype=np.int32), np.array([[[1], [2]]], dtype=np.float32) ], expected=np.array( [[[0, 0, 1]], [[0, 0, 0]], [[0, 0, 2]]], dtype=np.float32)) ssg_test2 = lambda x: array_ops.strided_slice_grad(*x, new_axis_mask=0x15) self._testNAry(ssg_test2, [ np.array([4, 4], dtype=np.int32), np.array([0, 0, 0, 1, 0], dtype=np.int32), np.array([0, 3, 0, 4, 0], dtype=np.int32), np.array([1, 2, 1, 2, 1], dtype=np.int32), np.array([[[[[1], [2]]], [[[3], [4]]]]], dtype=np.float32) ], expected=np.array([[0, 1, 0, 2], [0, 0, 0, 0], [0, 3, 0, 4], [0, 0, 0, 0]], dtype=np.float32)) self._testNAry(lambda x: array_ops.strided_slice_grad(*x), [ np.array([3, 3], dtype=np.int32), np.array([0, 2], dtype=np.int32), np.array([2, 0], dtype=np.int32), np.array([1, -1], dtype=np.int32), np.array([[1, 2], [3, 4]], dtype=np.float32) ], expected=np.array([[0, 2, 1], [0, 4, 3], [0, 0, 0]], dtype=np.float32)) self._testNAry(lambda x: array_ops.strided_slice_grad(*x), [ np.array([3, 3], dtype=np.int32), np.array([2, 2], dtype=np.int32), np.array([0, 1], dtype=np.int32), np.array([-1, -2], dtype=np.int32), np.array([[1], [2]], dtype=np.float32) ], expected=np.array([[0, 0, 0], [0, 0, 2], [0, 0, 1]], dtype=np.float32))
def ssg_test(x): return array_ops.strided_slice_grad(*x, shrink_axis_mask=0x4, new_axis_mask=0x1)
def testStridedSliceGrad(self): # Tests cases where input shape is empty. self._testNAry(lambda x: array_ops.strided_slice_grad(*x), [np.array([], dtype=np.int32), np.array([], dtype=np.int32), np.array([], dtype=np.int32), np.array([], dtype=np.int32), np.float32(0.5)], expected=np.array(np.float32(0.5), dtype=np.float32)) # Tests case where input shape is non-empty, but gradients are empty. self._testNAry(lambda x: array_ops.strided_slice_grad(*x), [np.array([3], dtype=np.int32), np.array([0], dtype=np.int32), np.array([0], dtype=np.int32), np.array([1], dtype=np.int32), np.array([], dtype=np.float32)], expected=np.array([0, 0, 0], dtype=np.float32)) self._testNAry(lambda x: array_ops.strided_slice_grad(*x), [np.array([3, 0], dtype=np.int32), np.array([1, 0], dtype=np.int32), np.array([3, 0], dtype=np.int32), np.array([1, 1], dtype=np.int32), np.array([[], []], dtype=np.float32)], expected=np.array([[], [], []], dtype=np.float32)) self._testNAry(lambda x: array_ops.strided_slice_grad(*x), [np.array([3, 3], dtype=np.int32), np.array([1, 1], dtype=np.int32), np.array([3, 3], dtype=np.int32), np.array([1, 1], dtype=np.int32), np.array([[5, 6], [8, 9]], dtype=np.float32)], expected=np.array([[0, 0, 0], [0, 5, 6], [0, 8, 9]], dtype=np.float32)) def ssg_test(x): return array_ops.strided_slice_grad(*x, shrink_axis_mask=0x4, new_axis_mask=0x1) self._testNAry(ssg_test, [np.array([3, 1, 3], dtype=np.int32), np.array([0, 0, 0, 2], dtype=np.int32), np.array([0, 3, 1, -4], dtype=np.int32), np.array([1, 2, 1, -3], dtype=np.int32), np.array([[[1], [2]]], dtype=np.float32)], expected=np.array([[[0, 0, 1]], [[0, 0, 0]], [[0, 0, 2]]], dtype=np.float32)) ssg_test2 = lambda x: array_ops.strided_slice_grad(*x, new_axis_mask=0x15) self._testNAry(ssg_test2, [np.array([4, 4], dtype=np.int32), np.array([0, 0, 0, 1, 0], dtype=np.int32), np.array([0, 3, 0, 4, 0], dtype=np.int32), np.array([1, 2, 1, 2, 1], dtype=np.int32), np.array([[[[[1], [2]]], [[[3], [4]]]]], dtype=np.float32)], expected=np.array([[0, 1, 0, 2], [0, 0, 0, 0], [0, 3, 0, 4], [0, 0, 0, 0]], dtype=np.float32)) self._testNAry(lambda x: array_ops.strided_slice_grad(*x), [np.array([3, 3], dtype=np.int32), np.array([0, 2], dtype=np.int32), np.array([2, 0], dtype=np.int32), np.array([1, -1], dtype=np.int32), np.array([[1, 2], [3, 4]], dtype=np.float32)], expected=np.array([[0, 2, 1], [0, 4, 3], [0, 0, 0]], dtype=np.float32)) self._testNAry(lambda x: array_ops.strided_slice_grad(*x), [np.array([3, 3], dtype=np.int32), np.array([2, 2], dtype=np.int32), np.array([0, 1], dtype=np.int32), np.array([-1, -2], dtype=np.int32), np.array([[1], [2]], dtype=np.float32)], expected=np.array([[0, 0, 0], [0, 0, 2], [0, 0, 1]], dtype=np.float32))