我最近學習了TensorFlow,想將我的圖片導入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 (most recent call last): File "/home/hadoop/PycharmProjects/TensorFlow/Test.py", line 43, in sess.run(train_step, feed_dict={x: batch_xs, y_actual: batch_ys}) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 767, in run run_metadata_ptr) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 925, in _run raise TypeError('The value of a feed cannot be a tf.Tensor object. ' TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, or 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'直接,因爲這些都是張量的值。 –
您能告訴我如何編輯我的代碼以使其運行? 我不知道如何編輯代碼,因爲我只是按照文檔做 – iamcodylee