Returns a hashable reference object to this Tensor. Warning: Experimental API that could be changed or removed. The primary usecase for this API is to put tensors in a set/dictionary. We can't put tensors in a set/dictionary as `tensor.__hash__()` is no longer available starting Te
(self)
| 798 | return _eval_using_default_session(self, feed_dict, self.graph, session) |
| 799 | |
| 800 | def experimental_ref(self): |
| 801 | # tf.Variable also has the same experimental_ref() API. If you update the |
| 802 | # documenation here, please update tf.Variable.experimental_ref() as well. |
| 803 | """Returns a hashable reference object to this Tensor. |
| 804 | |
| 805 | Warning: Experimental API that could be changed or removed. |
| 806 | |
| 807 | The primary usecase for this API is to put tensors in a set/dictionary. |
| 808 | We can't put tensors in a set/dictionary as `tensor.__hash__()` is no longer |
| 809 | available starting Tensorflow 2.0. |
| 810 | |
| 811 | ```python |
| 812 | import tensorflow as tf |
| 813 | |
| 814 | x = tf.constant(5) |
| 815 | y = tf.constant(10) |
| 816 | z = tf.constant(10) |
| 817 | |
| 818 | # The followings will raise an exception starting 2.0 |
| 819 | # TypeError: Tensor is unhashable if Tensor equality is enabled. |
| 820 | tensor_set = {x, y, z} |
| 821 | tensor_dict = {x: 'five', y: 'ten', z: 'ten'} |
| 822 | ``` |
| 823 | |
| 824 | Instead, we can use `tensor.experimental_ref()`. |
| 825 | |
| 826 | ```python |
| 827 | tensor_set = {x.experimental_ref(), |
| 828 | y.experimental_ref(), |
| 829 | z.experimental_ref()} |
| 830 | |
| 831 | print(x.experimental_ref() in tensor_set) |
| 832 | ==> True |
| 833 | |
| 834 | tensor_dict = {x.experimental_ref(): 'five', |
| 835 | y.experimental_ref(): 'ten', |
| 836 | z.experimental_ref(): 'ten'} |
| 837 | |
| 838 | print(tensor_dict[y.experimental_ref()]) |
| 839 | ==> ten |
| 840 | ``` |
| 841 | |
| 842 | Also, the reference object provides `.deref()` function that returns the |
| 843 | original Tensor. |
| 844 | |
| 845 | ```python |
| 846 | x = tf.constant(5) |
| 847 | print(x.experimental_ref().deref()) |
| 848 | ==> tf.Tensor(5, shape=(), dtype=int32) |
| 849 | ``` |
| 850 | """ |
| 851 | return object_identity.Reference(self) |
| 852 | |
| 853 | |
| 854 | # TODO(agarwal): consider getting rid of this. |