kafaso
kafaso

Reputation: 67

Tensorflow: MutableHashTable InvalidArgumentError, supported datatypes

I want to create a MutableHashTable with int64 keys/values:

import tensorflow as tf
with tf.Session() as sess:
    keys = tf.range(10,dtype=tf.int64)
    vals = tf.range(10,dtype=tf.int64)
    table = tf.contrib.lookup.MutableHashTable(key_dtype=tf.int64, value_dtype=tf.int64, default_value=-1)
    table.insert(keys, vals)
    print(sess.run(table.lookup(tf.range(20,dtype=tf.int64))))

But when I execute it i get the following error message:

InvalidArgumentError (see above for traceback): No OpKernel was registered to support Op 'MutableHashTableV2' with these attrs.  Registered devices: [CPU,GPU], Registered kernels:
  device='CPU'; key_dtype in [DT_STRING]; value_dtype in [DT_FLOAT]
  device='CPU'; key_dtype in [DT_STRING]; value_dtype in [DT_INT64]
  device='CPU'; key_dtype in [DT_INT64]; value_dtype in [DT_STRING]
  device='CPU'; key_dtype in [DT_STRING]; value_dtype in [DT_BOOL]
  device='CPU'; key_dtype in [DT_INT64]; value_dtype in [DT_FLOAT]

     [[Node: MutableHashTable_16 = MutableHashTableV2[container="", key_dtype=DT_INT64, shared_name="", use_node_name_sharing=true, value_dtype=DT_INT64]()]]

If I do it with HashTable it works:

import tensorflow as tf
with tf.Session() as sess:
    keys = tf.range(10,dtype=tf.int64)
    vals = tf.range(10,dtype=tf.int64)
    table = tf.contrib.lookup.HashTable(tf.contrib.lookup.KeyValueTensorInitializer(keys,vals),-1)
    table.init.run()
    print(sess.run(table.lookup(tf.range(20,dtype=tf.int64))))

Upvotes: 1

Views: 802

Answers (1)

For mutable hash table in tensorflow, only the following key - value pair types are allowed:

key_type   -   value_type  
tf.string  -   tf.float  
tf.string  -   tf.int64  
tf.int64   -   tf.string  
tf.string  -   tf.bool  
tf.int64   -   tf.float  

This is also mentioned in the error message that you got.

One way to use in64 as keys and values is by using MutableDenseHashTable.

Following is a sample code to do this:

import tensorflow as tf

with tf.Session() as sess:
         # Initialize keys and values.
         keys = tf.constant([1, 2, 3], dtype=tf.int64)
         vals = tf.constant([1, 2, 3], dtype=tf.int64)

         # Initialize hash table.
         table = tf.contrib.lookup.MutableDenseHashTable(key_dtype=tf.int64, value_dtype=tf.int64, default_value=-1, empty_key=0)

         # Insert values to hash table and run the op.
         insert_op = table.insert(keys, vals)
         sess.run(insert_op)

         # Print hash table lookups.
         print(sess.run(table.lookup(keys)))

Upvotes: 2

Related Questions