【问题标题】:tensorflow TypeError: unhashable type: 'numpy.ndarray'tensorflow TypeError:不可散列的类型:'numpy.ndarray'
【发布时间】:2018-06-15 17:41:06
【问题描述】:

所以我试图用我自己的数据创建一个 tensorflow 模型,但似乎 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.

在搜索了几次之后,似乎大部分时间的问题是在代码中重命名占位符变量会弄乱东西,但在我的代码中并非如此,至少我不这么认为.

这是完整的工作代码和输入文件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'

【问题讨论】:

  • 请发布整个错误及其堆栈跟踪。

标签: python numpy tensorflow scope


【解决方案1】:

正如您猜对的那样,您实际上是在“重命名”占位符x。函数train(x) 中的x 采用参数的值(例如trng_input),并且不指向外部作用域占位符x


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

【讨论】:

  • 天哪,小 x 就是问题所在,我的数据只有 3000 个示例,所以我没有看到批处理的意义,标签不是 onehot 数组,问题不存在
  • 对,我对 onehot 的东西不好。我编辑了我的答案。很高兴它有所帮助。
猜你喜欢
  • 2012-02-19
  • 2018-04-24
  • 2018-07-29
  • 2020-05-22
  • 1970-01-01
  • 2019-03-22
  • 2022-01-08
  • 1970-01-01
相关资源
最近更新 更多