tensorflow TypeError:unhashable类型:' numpy.ndarray'

时间:2018-06-15 17:41:07

标签: python numpy tensorflow scope

所以我试图使用我自己的数据制作张量流模型,但似乎numpy数组不是tf.Session.run()中的可输入类型?

我检查了网站,它应该是可输送的,如他们的一个示例所示:

x = tf.placeholder(tf.float32, shape=(1024, 1024))
y = tf.matmul(x, x)

with tf.Session() as sess:
  print(sess.run(y))  # ERROR: will fail because x was not fed.

  rand_array = np.random.rand(1024, 1024) #HERE IS THE NUMPY ARRAY

  print(sess.run(y, feed_dict={x: rand_array}))  # Will succeed.

在搜索了几次之后,似乎大多数情况下问题是在代码中重命名占位符变量时出现问题,但是在我的代码中并不是这样,至少我没有&#39我想是的。

以下是完整的工作代码和输入文件data

import tensorflow as tf
import numpy as np

data = np.load("test_data.npz")
trng_input = np.array(data['Input'], dtype=np.float64)
trng_output = np.array(data['Output'], dtype=np.float64)

nhl1 = 16
nhl2 = 8
n_classes = 4


x = tf.placeholder(dtype=tf.float64, shape=[len(trng_input),24])
y = tf.placeholder(dtype=tf.float64, shape=[len(trng_output),n_classes])

def NN(data):
    hl1 = {"weights":tf.Variable(tf.random_normal([24, nhl1], dtype=tf.float64)),
           "biases":tf.Variable(tf.random_normal([nhl1], dtype=tf.float64))}

    hl2 = {"weights":tf.Variable(tf.random_normal([nhl1, nhl2], dtype=tf.float64)),
           "biases":tf.Variable(tf.random_normal([nhl2], dtype=tf.float64))}

    output_layer = {"weights":tf.Variable(tf.random_normal([nhl2, n_classes], dtype=tf.float64)),
                    "biases":tf.Variable(tf.random_normal([n_classes], dtype=tf.float64))}

    l1 = tf.add(tf.matmul(data, hl1["weights"]), hl1["biases"])
    l1 = tf.nn.leaky_relu(l1, alpha=0.2)

    l2 = tf.add(tf.matmul(l1, hl2["weights"]), hl2["biases"])
    l2 = tf.nn.leaky_relu(l2, alpha=0.2)

    output = tf.add(tf.matmul(l2, output_layer["weights"]), output_layer["biases"])
##    output = tf.nn.leaky_relu(l1, alpha=0.2)

    return output

def train(x):
    prediction = NN(x)
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=prediction, labels=y))
    optimizer = tf.train.AdamOptimizer().minimize(cost)

    epochs = 100

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        for epoch in range(epochs):
            epoch_loss = 0
            _, c = sess.run([optimizer, cost], feed_dict={x: trng_input, y: trng_output})
            epoch_loss += c
            print(F"Epoch {epoch} completed out of {epochs}. \nloss:{epoch_loss}")

        correct = tf.equal(tf.argmax(prediction,1), tf.argmax(y, 1))

        accuracy = tf.reduce_mean(tf.cast(correct,"float"))
        Eval = accuracy.eval({x:trng_input, y:trng_output})
        print(F"Accuracy:{Eval}")

train(trng_input)

要求完整错误:

Traceback (most recent call last):
  File "C:\Python36\machine_learning\real_tf_folder\test.py", line 59, in <module>
    train(trng_input)
  File "C:\Python36\machine_learning\real_tf_folder\test.py", line 49, in train
    _, c = sess.run([optimizer, cost], feed_dict={x: trng_input, y: trng_output})
 TypeError: unhashable type: 'numpy.ndarray'

1 个答案:

答案 0 :(得分:3)

正如您所猜测的那样,您实际上正在重命名&#34;占位符x。函数x内的train(x)获取参数的值(例如trng_input),并且不指向外部范围占位符x

注意:如果trng_inputtrng_output代表您的完整数据集,您可能希望迭代小批量而不是整批数据。