Fix TensorFlow TypeError: unhashable type: ‘numpy.ndarray’ Error – TensorFlow Tutorial

By | August 12, 2021

In this tutorial, we will introduce you how to fix tensorflow TypeError: unhashable type: ‘numpy.ndarray’ error, which is very useful for tensorflow beginners.

Look at example below:

import tensorflow as tf
import numpy as np

x = tf.placeholder(tf.float32, [None,2], name="input_x")  #
y = tf.placeholder(tf.float32, [None, 2], name="input_y")  #
d = x +y

init = tf.global_variables_initializer()
init_local = tf.local_variables_initializer()
with tf.Session() as sess:
    sess.run([init, init_local])
    np.set_printoptions(precision=4, suppress=True)
    x = np.random.random([3, 2])
    feed_dict = {
        x: x,
        y: np.random.random([3, 2])
    }
    x = sess.run(d, feed_dict)
    print(x)

Run this code, you will get this error:

TypeError: unhashable type: ‘numpy.ndarray’

Fix TensorFlow TypeError: unhashable type: 'numpy.ndarray' Error - TensorFlow Tutorial

How to fix this unhashable error?

Look at this code in our example:

x = np.random.random([3, 2]

Here x is converted to numpy.ndarray. However, in code:

feed_dict = {
        x: x,
        y: np.random.random([3, 2])
    }

We need x be a <class ‘tensorflow.python.framework.ops.Tensor’>

In order to fix this error, we shoud not change the data type of x.

We can do as follows:

    z = np.random.random([3, 2])
    feed_dict = {
        x: z,
        y: np.random.random([3, 2])
    }

Then this unhashable error is fixed.

Leave a Reply

Your email address will not be published. Required fields are marked *