'无法将feed_dict键解释为Tensor:使用probabilities.eval时无法将int转换为Tensor'?

时间:2017-09-24 19:57:06

标签: python tensorflow mnist tensor

我想使用tensor-flow打印我自己图像的所有概率。以下代码适用于我从在线获得的任何mnist图像,但不适用于我自己的图像。这是整个代码。

from PIL import Image
import random
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import numpy as np
import scipy.ndimage
from PIL import Image


np.set_printoptions(threshold='nan')

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)


x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, [None, 10])

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

init = tf.initialize_all_variables()

sess = tf.Session()
sess.run(init)

for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(1000)
    sess.run(train_step, feed_dict= {x: batch_xs, y_: batch_ys})

print ("done with training")
print ("\n\n\n")

probabilities  = y

img = Image.open('mnist_7_367.jpg').convert('L')  # convert image to 8-bit grayscale
data = list(img.getdata()) # convert image data to a list of integers
newList = [float(x) / 255.0 for x in data]
newList1 = np.asarray(newList)
batch_xs = np.reshape(newList1, (1, 784))
batch_xs[batch_xs < 0.1] = 0
#print isinstance(newList1, list)
print batch_xs


counter = 0
for x in probabilities.eval(feed_dict={x: batch_xs}, session = sess):
    print ('[')
    for y in x:
        print (counter, '{:f}'.format(float(y)))
        counter += 1
    print (']')

hello = np.reshape(batch_xs, (28, 28))  


Matrix = np.array([])

for x in hello:
    for y in x:
        Matrix = np.append(Matrix, int(255 * y))    


imageReal = np.reshape(Matrix, (28, 28))

img = Image.fromarray(imageReal)
img.show()

sess.close()

我遇到的问题是

for x in probabilities.eval(feed_dict={x: batch_xs}, session = sess): 

产生以下错误:

Cannot interpret feed_dict key as Tensor: Can not convert a int into a Tensor.

我觉得问题出在session = sess部分行。

0 个答案:

没有答案