【发布时间】: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