diff options
author | 2017-09-01 13:47:23 -0700 | |
---|---|---|
committer | 2017-09-01 13:55:12 -0700 | |
commit | 2e7fdf03319725d3e5c62c20238f49dccb474dc5 (patch) | |
tree | fa28de805a197e6bbe8b2da6af6e0b7668bb6654 /tensorflow | |
parent | b897ae084a9833f88ab5619bdf8940735b7d0de5 (diff) |
Fix convert_to_tensor problem for list of TensorNodes
Current code fail to error out in convert_to_tensor
when multiple TensorNodes are passed in.
PiperOrigin-RevId: 167319274
Diffstat (limited to 'tensorflow')
-rw-r--r-- | tensorflow/python/eager/backprop_test.py | 14 | ||||
-rw-r--r-- | tensorflow/python/framework/ops.py | 20 | ||||
-rw-r--r-- | tensorflow/python/kernel_tests/array_ops_test.py | 16 |
3 files changed, 36 insertions, 14 deletions
diff --git a/tensorflow/python/eager/backprop_test.py b/tensorflow/python/eager/backprop_test.py index 429eeabb42..b437905509 100644 --- a/tensorflow/python/eager/backprop_test.py +++ b/tensorflow/python/eager/backprop_test.py @@ -307,6 +307,20 @@ class BackpropTest(test.TestCase): [tensor_shape.TensorShape(s).as_proto() for s in shape_list], backprop.make_attr([pywrap_tensorflow.TF_ATTR_SHAPE], shape_list)) + def testMultiValueConvertToTensor(self): + x = resource_variable_ops.ResourceVariable( + initial_value=array_ops.constant([1.0]), name='x') + + def fn(): + tape.watch_variable(x) + a = math_ops.add(x.value(), 1.0) + # Make sure convert_to_tensor works correctly with list of TensorNodes. + b = array_ops.stack([a, a], axis=0) + return math_ops.reduce_mean(b) + + grad = backprop.implicit_grad(fn)()[0][1] + self.assertAllEqual([1.0], grad.numpy()) + if __name__ == '__main__': test.main() diff --git a/tensorflow/python/framework/ops.py b/tensorflow/python/framework/ops.py index 659bc394b9..b197e96886 100644 --- a/tensorflow/python/framework/ops.py +++ b/tensorflow/python/framework/ops.py @@ -49,6 +49,7 @@ from tensorflow.python.framework import versions from tensorflow.python.platform import tf_logging as logging from tensorflow.python.util import compat from tensorflow.python.util import decorator_utils +from tensorflow.python.util import nest from tensorflow.python.util import tf_contextlib # Temporary global switch determining if we should enable the work-in-progress @@ -1036,12 +1037,19 @@ def internal_convert_to_tensor(value, # tracing gradients, to ensure the same behavior happens with and without # tracing. unwrapped = ag_core.getval(value) - # Fast path for EagerTensors that don't need any conversion. - if isinstance(unwrapped, EagerTensor) and context.in_eager_mode(): - # Note that we don't check that value's dtype matches the dtype - # argument. We exepct that the C runtime will do that checking - # when we execute the kernel. - return value + + if context.in_eager_mode(): + # Fast path for EagerTensors that don't need any conversion. + if isinstance(unwrapped, EagerTensor): + # Note that we don't check that value's dtype matches the dtype + # argument. We exepct that the C runtime will do that checking + # when we execute the kernel. + return value + values = nest.flatten(value) + if (len(values) > 1 and + any(isinstance(ag_core.getval(v), EagerTensor) for v in values)): + raise TypeError("Cannot convert to a eager tensor.") + if dtype is not None: dtype = dtypes.as_dtype(dtype) unwrapped_type = type(unwrapped) diff --git a/tensorflow/python/kernel_tests/array_ops_test.py b/tensorflow/python/kernel_tests/array_ops_test.py index 392639fa17..77c5bb6d40 100644 --- a/tensorflow/python/kernel_tests/array_ops_test.py +++ b/tensorflow/python/kernel_tests/array_ops_test.py @@ -981,15 +981,15 @@ class SequenceMaskTest(test_util.TensorFlowTestCase): class ConcatSliceResourceTest(test_util.TensorFlowTestCase): + @test_util.run_in_graph_and_eager_modes() def testConcatSlice(self): - with self.test_session(): - r1 = test_ops.stub_resource_handle_op(container="a", shared_name="b") - r2 = test_ops.stub_resource_handle_op(container="a", shared_name="c") - c = array_ops.stack([r1, r2]) - s = array_ops.strided_slice(c, [1], [2]) - test_ops.resource_create_op(s).run() - with self.assertRaises(errors.AlreadyExistsError): - test_ops.resource_create_op(r2).run() + r1 = test_ops.stub_resource_handle_op(container="a", shared_name="b") + r2 = test_ops.stub_resource_handle_op(container="a", shared_name="c") + c = array_ops.stack([r1, r2]) + s = array_ops.strided_slice(c, [1], [2]) + self.evaluate(test_ops.resource_create_op(s)) + with self.assertRaises(errors.AlreadyExistsError): + self.evaluate(test_ops.resource_create_op(r2)) class IdentityTest(test_util.TensorFlowTestCase): |