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

Other info / logs

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18 (7 by maintainers)

Most upvoted comments

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 -

class VocabLookup(tf.keras.layers.Layer):
    def __init__(self,vocab_path):
        super(VocabLookup, self).__init__(trainable=False,dtype=tf.int64)
        self.vocab_path = vocab_path
    def build(self,input_shape):
        table_init = tf.lookup.TextFileInitializer(self.vocab_path,tf.string,tf.lookup.TextFileIndex.WHOLE_LINE,
                              tf.int64,tf.lookup.TextFileIndex.LINE_NUMBER)
        self.table = tf.lookup.StaticHashTable(table_init,-1)
        self.built=True
        
    def call(self, input_text):
        splitted_text = tf.strings.split(input_text).to_tensor()
        word_ids = self.table.lookup(splitted_text)
        return word_ids
    
    def get_config(self):
        config = super(VocabLookup, self).get_config()
        config.update({'vocab_path': self.vocab_path})
        return config 
input_text = tf.keras.Input(shape=(),dtype=tf.string,name='input_text')
lookup_out = VocabLookup(vocab_path=vocab_path)(input_text)
model_lookup = tf.keras.Model(inputs={'input_text':input_text},outputs=lookup_out)

Hope this helps