diff options
author | A. Unique TensorFlower <gardener@tensorflow.org> | 2018-09-19 05:06:48 -0700 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2018-09-19 05:10:30 -0700 |
commit | 7b936cb6c4ca47c8d3a63b42364998c86d87f2cf (patch) | |
tree | 8437d357bdfde1cc346c6ee75b5551f89e25da6e | |
parent | 227b819d55c3b24103026cdaf1897892422c5cd3 (diff) |
jacobian: manually setting the output shape in the output.
PiperOrigin-RevId: 213610324
-rw-r--r-- | tensorflow/python/ops/parallel_for/BUILD | 2 | ||||
-rw-r--r-- | tensorflow/python/ops/parallel_for/gradients.py | 2 | ||||
-rw-r--r-- | tensorflow/python/ops/parallel_for/gradients_test.py | 26 |
3 files changed, 30 insertions, 0 deletions
diff --git a/tensorflow/python/ops/parallel_for/BUILD b/tensorflow/python/ops/parallel_for/BUILD index 015181af47..07fc9433a2 100644 --- a/tensorflow/python/ops/parallel_for/BUILD +++ b/tensorflow/python/ops/parallel_for/BUILD @@ -123,6 +123,8 @@ cuda_py_test( "//third_party/py/numpy", "//tensorflow/python:layers", "//tensorflow/python:client_testlib", + "//tensorflow/python:control_flow_ops", + "//tensorflow/python:functional_ops", "//tensorflow/python:random_ops", "//tensorflow/python/ops/losses", ], diff --git a/tensorflow/python/ops/parallel_for/gradients.py b/tensorflow/python/ops/parallel_for/gradients.py index 460de0a97f..1f026b3660 100644 --- a/tensorflow/python/ops/parallel_for/gradients.py +++ b/tensorflow/python/ops/parallel_for/gradients.py @@ -42,6 +42,7 @@ def jacobian(output, inputs, use_pfor=True): [y_1, ..., y_n, x_1, ..., x_m]. """ flat_inputs = nest.flatten(inputs) + output_tensor_shape = output.shape output_shape = array_ops.shape(output) output = array_ops.reshape(output, [-1]) @@ -65,6 +66,7 @@ def jacobian(output, inputs, use_pfor=True): new_shape = array_ops.concat( [output_shape, array_ops.shape(out)[1:]], axis=0) out = array_ops.reshape(out, new_shape) + out.set_shape(output_tensor_shape.concatenate(flat_inputs[i].shape)) pfor_outputs[i] = out return nest.pack_sequence_as(inputs, pfor_outputs) diff --git a/tensorflow/python/ops/parallel_for/gradients_test.py b/tensorflow/python/ops/parallel_for/gradients_test.py index 628c6764cd..5467f55af6 100644 --- a/tensorflow/python/ops/parallel_for/gradients_test.py +++ b/tensorflow/python/ops/parallel_for/gradients_test.py @@ -32,6 +32,8 @@ from tensorflow.python.framework import ops from tensorflow.python.keras.engine import training as keras_training from tensorflow.python.layers import layers as tf_layers from tensorflow.python.ops import array_ops +from tensorflow.python.ops import control_flow_ops as tf_control_flow_ops +from tensorflow.python.ops import functional_ops from tensorflow.python.ops import gradients as gradient_ops from tensorflow.python.ops import math_ops from tensorflow.python.ops import nn @@ -355,6 +357,30 @@ class GradientsTest(test.TestCase): self.run_and_assert_equal(answer, jacobian_pfor) self.run_and_assert_equal(answer, jacobian_while) + def test_jacobian_scan_shape(self): + # Shape x: [3, 4] + x = random_ops.random_uniform([3, 4]) + elems = random_ops.random_uniform([6]) + # Shape y: [6, 3, 4] + y = functional_ops.scan(lambda a, e: a + e, elems, initializer=x) + jacobian = gradients.jacobian(y, x) + + expected_shape = [6, 3, 4, 3, 4] + self.assertAllEqual(expected_shape, jacobian.shape.as_list()) + + def test_jacobian_while_loop_shape(self): + # Shape x: [3, 4] + x = random_ops.random_uniform([3, 4]) + _, y = tf_control_flow_ops.while_loop(lambda i, a: i > 5., + lambda i, a: (i + 1, a + i), + (constant_op.constant(0.), x)) + # Shape y: [2, 3] + y = y[:2, :3] + jacobian = gradients.jacobian(y, x) + + expected_shape = [2, 3, 3, 4] + self.assertAllEqual(expected_shape, jacobian.shape.as_list()) + def test_jacobian_unknown_shape(self): with self.cached_session() as sess: x = array_ops.placeholder(dtypes.float32, shape=[None, None]) |