我最近在学习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)File" /usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", 第925行,在_run 提出TypeError('提要的值不能是tf.Tensor对象。' TypeError:提要的值不能是tf.Tensor对象。 可接受的Feed值包括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 我应该为我的情况输入什么?
非常感谢您的帮助
答案 0 :(得分:0)
image
和label
变量是张量image
变量必须是您在输入张量[None, 3000]
中定义的形状x = tf.placeholder(tf.float32, [None, 30000])
的numpy数组。label
变量也必须是您在输入张量[None, 8]
中定义的形状x = tf.placeholder(tf.float32, [None, 8])
的numpy数组image
变量从tensor变为numpy数组,请尝试以下操作:batch_xs = sess.run(image)
- 你也想把你的label
变量从张量变成一个numpy数组,所以试试这个:batch_ys = sess.run(label)