diff --git a/tensorflow/python/kernel_tests/resource_variable_ops_test.py b/tensorflow/python/kernel_tests/resource_variable_ops_test.py index edcd8d7a05e5eaf77b17f01e667b840ca4dfdbd0..0bc61fc6dac9d09599159c184a1d0a2fa84040ff 100644 --- a/tensorflow/python/kernel_tests/resource_variable_ops_test.py +++ b/tensorflow/python/kernel_tests/resource_variable_ops_test.py @@ -1194,6 +1194,8 @@ class ResourceVariableOpsTest(test_util.TensorFlowTestCase, v2 = resource_variable_ops.ResourceVariable( initial_value=callable_init, name="var7") self.assertEqual("var7:0", v2.name) + self.assertAllEqual(np.array(v2), v2.read_value().numpy()) + self.assertAllEqual(v2.numpy(), v2.read_value().numpy()) self.assertAllEqual(2 * init.numpy(), v2.read_value().numpy()) # Test assign_add. diff --git a/tensorflow/python/ops/resource_variable_ops.py b/tensorflow/python/ops/resource_variable_ops.py index 8f5d056807fc12989fecc59129f8b7b3b2cb50ce..5921d9ac0f61c6b2445b3173dde3bdd4d3bf0964 100644 --- a/tensorflow/python/ops/resource_variable_ops.py +++ b/tensorflow/python/ops/resource_variable_ops.py @@ -474,6 +474,17 @@ class BaseResourceVariable(variables.VariableV1, core.Tensor): else: yield + def __array__(self): + """Allows direct conversion to a numpy array. + + >>> np.array(tf.Variable([1.0])) + array([1.], dtype=float32) + + Returns: + The variable value as a numpy array. + """ + return self.numpy() + def __nonzero__(self): return self.__bool__()