【发布时间】:2017-08-21 04:19:26
【问题描述】:
我最近在学习 TensorFlow,想将我的图片导入 TensorFlow 进行训练,但我遇到了一个问题。 下面是我的代码
import tensorflow as tf
tf.device(0)
def read_and_decode(filename):
filename_queue = tf.train.string_input_producer([filename])
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
features={
'label': tf.FixedLenFeature([], tf.int64),
'img_raw': tf.FixedLenFeature([], tf.string),
})
img = tf.decode_raw(features['img_raw'], tf.uint8)
img = tf.reshape(img, [100, 100, 3])
img = tf.cast(img, tf.float32) * (1. / 255) - 0.5
lbl = tf.cast(features['label'], tf.int32)
return img, lbl
image, label = read_and_decode('/Users/Cody/PycharmProjects/TensorFlowStartUp/train.tfrecords')
img_batch, label_batch = tf.train.shuffle_batch([image, label],
batch_size=5, capacity=5,
min_after_dequeue=2)
x = tf.placeholder(tf.float32, [None, 30000])
y_actual = tf.placeholder(tf.float32, shape=[None, 8])
W = tf.Variable(tf.zeros([30000,8]))
b = tf.Variable(tf.zeros([8]))
y_predict = tf.nn.softmax(tf.matmul(x,W) + b)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_actual*tf.log(y_predict),reduction_indices=1))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_predict,1), tf.argmax(y_actual,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for i in range(100):
batch_xs = image
batch_ys = label
sess.run(train_step, feed_dict={x: batch_xs, y_actual: batch_ys})
if(i%10==0):
print "accuracy:",sess.run(accuracy, feed_dict={x: image, y_actual: label})
当我运行代码时,我得到如下错误消息:
Traceback(最近一次调用最后一次):文件 “/home/hadoop/PycharmProjects/TensorFlow/Test.py”,第 43 行,在 sess.run(train_step, feed_dict={x: batch_xs, y_actual: batch_ys}) 文件 "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", 第 767 行,运行中 run_metadata_ptr)文件“/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py”, 第 925 行,在 _run raise TypeError('feed 的值不能是 tf.Tensor 对象。' TypeError: feed 的值不能是 tf.Tensor 对象。 可接受的提要值包括 Python 标量、字符串、列表或 numpy ndarrays。
我不知道如何正确编写代码。
x = tf.placeholder(tf.float32, [None, 30000])
y_actual = tf.placeholder(tf.float32, shape=[None, 8])
W = tf.Variable(tf.zeros([30000,8]))
b = tf.Variable(tf.zeros([8]))
对于 x, y_actual, W, b 我应该针对我的情况输入什么?
非常感谢您的帮助
【问题讨论】:
-
您应该直接使用
label和image,而不是创建占位符,因为它们是张量值。 -
您能告诉我如何编辑我的代码以使其运行吗?我不知道如何编辑代码,因为我只是按照文档去做
标签: python tensorflow