tensorflow: tf.lookup.StaticHashTable: Cannot convert a Tensor of dtype resource to a NumPy array
System information
- Have I written custom code: Yes
- OS Platform and Distribution: macOS 10.14.6
- TensorFlow installed from (source or binary): binary
- TensorFlow version (use command below): v2.1.0-rc2-17-ge5bf8de410 2.1.0
- Python version: 3.7.4
Describe the current behavior I have some input that I need to translate,
"Dog" -> 0
"Cat" -> 1
...
and then embed.
I tried to use the tf.lookup.StaticHashTable
for this, but it works when the input is a tf.Variable
, not when using tf.keras.layers.Input
, see the following output:
In [6]:
inputs1 = tf.Variable([[3]], dtype=tf.int64)
translate_and_embed(inputs1)
Out[6]:
<tf.Tensor: shape=(1, 1, 10), dtype=float32, numpy=
array([[[ 0.01451602, 0.04537327, -0.00232627, 0.00584463,
-0.04128218, -0.03868868, 0.04147324, -0.02444596,
0.03310961, 0.01144157]]], dtype=float32)>
In [7]:
inputs2 = tf.keras.layers.Input(shape=(1,), dtype=tf.int64)
translate_and_embed(inputs2)
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-10-b2c4cfd055e5> in <module>()
1 inputs2 = tf.keras.layers.Input(shape=(1,), dtype=tf.int64)
----> 2 translate_and_embed(inputs2)
<ipython-input-8-340bf85dfb2f> in translate_and_embed(inputs)
21 output_dim=10,
22 )
---> 23 return embedder(translated)
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
718 # framework.
719 if build_graph and base_layer_utils.needs_keras_history(inputs):
--> 720 base_layer_utils.create_keras_history(inputs)
721
722 # Clear eager losses on top level model call.
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/base_layer_utils.py in create_keras_history(tensors)
185 keras_tensors: The Tensors found that came from a Keras Layer.
186 """
--> 187 _, created_layers = _create_keras_history_helper(tensors, set(), [])
188 return created_layers
189
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/base_layer_utils.py in _create_keras_history_helper(tensors, processed_ops, created_layers)
245 else:
246 with ops.init_scope():
--> 247 constants[i] = backend.function([], op_input)([])
248 processed_ops, created_layers = _create_keras_history_helper(
249 layer_inputs, processed_ops, created_layers)
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/backend.py in __call__(self, inputs)
3733 return nest.pack_sequence_as(
3734 self._outputs_structure,
-> 3735 [x._numpy() for x in outputs], # pylint: disable=protected-access
3736 expand_composites=True)
3737
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/backend.py in <listcomp>(.0)
3733 return nest.pack_sequence_as(
3734 self._outputs_structure,
-> 3735 [x._numpy() for x in outputs], # pylint: disable=protected-access
3736 expand_composites=True)
3737
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py in _numpy(self)
908 return self._numpy_internal()
909 except core._NotOkStatusException as e:
--> 910 six.raise_from(core._status_to_exception(e.code, e.message), None)
911
912 @property
/usr/local/lib/python3.6/dist-packages/six.py in raise_from(value, from_value)
InvalidArgumentError: Cannot convert a Tensor of dtype resource to a NumPy array.
Describe the expected behavior I expect the second function call to return a symbolic tensor.
Standalone code to reproduce the issue
- https://colab.research.google.com/gist/jeanmn/6d2821feaf4828f43524ada267db266f/tf-lookup-statichhashtable-cannot-convert-a-tensor-of-dtype-resource-to-a-numpy-array.ipynb
- https://gist.github.com/jeanmn/6d2821feaf4828f43524ada267db266f
Other info / logs
- Similar issue? https://github.com/tensorflow/tensorflow/issues/37441
- Related? #37844
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 18 (7 by maintainers)
The following sample code worked in tensorflow 2.1 & 2.2 -
Main point is to extend keras layer object & use lookup operation inside that. Here’s the sample code - https://gist.github.com/jithinjees/a99e57af3812be2c84bdc2ef84ad0de6 In the link above i have listed lookup using 2 different classes, 1 is file based lookup & other one is an in-memory list based lookup. For the file based lookup the input file used is a text file which contains one word per line
Here’s a gist of that code -
Hope this helps