我是tensorflow的新手,我正在尝试使用我自己的.tfrecords文件。现在我的代码出错了,我不知道发生了什么。有人告诉我怎么办?解决这个问题
from color_1 import read_and_decode, get_batch, get_test_batch
import cv2
import os
import time
import numpy as np
import tensorflow as tf
batch_size=50
n_input=56*56*3
n_classes=10
def weight_variable(shape):
initial = tf.truncated_normal(shape=shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
x = tf.placeholder(tf.float32, [None,56,56,3])
y = tf.placeholder(tf.float32, [None,n_classes])
x_image = tf.reshape(x, [-1, 56, 56, 3])
W_conv1 = weight_variable([5, 5, 3, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1)+b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
W_fc1 = weight_variable([14*14*64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 14*14*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
keep_prob = tf.placeholder("float")
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
cross_entropy = -tf.reduce_sum(y * tf.log(y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
def run():
image, label = read_and_decode('train.tfrecords')
batch_image, batch_label = get_batch(image, label, batch_size, crop_size=56)
test_image, test_label = read_and_decode('val.tfrecords')
test_images, test_labels = get_test_batch(test_image, test_label, batch_size, crop_size=56)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for i in range(20000):
image_batch, label_batch = sess.run([batch_image, batch_label])
if i%100 == 0:
train_accuracy = accuracy.eval(feed_dict={x: image_batch, y:label_batch,keep_prob:1.0})
print("step %d, training accuracy %g" % (i, train_accuracy))
train_step.run(feed_dict={x:image_batch, y:label_batch,keep_prob:0.5})
print("test accuracy %g" % accuracy.eval(feed_dict={
x:test_images, y:test_labels, keep_prob:1.0}))
coord.request_stop()
coord.join(threads)
if __name__=='__main__':
run()
问题就是这样:
Traceback (most recent call last):
File "/home/vrview/tensorflow/example/char/tfrecords/LeNet.py", line 130, in <module>
run()
File "/home/vrview/tensorflow/example/char/tfrecords/LeNet.py", line 120, in run
train_accuracy = accuracy.eval(feed_dict={x: image_batch, y:label_batch,keep_prob:1.0})
File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 581, in eval
return _eval_using_default_session(self, feed_dict, self.graph, session)
File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3797, in _eval_using_default_session
return session.run(tensors, feed_dict)
File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 767, in run
run_metadata_ptr)
File "/home/vrview/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 944, in _run
% (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (50,) for Tensor u'Placeholder_1:0', which has shape '(?, 10)'
train_accuracy = accuracy.eval(feed_dict = {x:image_batch,y:label_batch,keep_prob:1.0})#bug在这里
我不知道该怎么办。如果你知道,请告诉我。非常感谢你!
答案 0 :(得分:1)
第120行的 label_batch 看起来是标签的值,而不是1-hot编码。例如,它可能看起来像这样的一维数组[1,3,4,0,6 ...],而它需要是像这样的1-hot编码的二维数组[[0,1, 0,0,0,0,0,0,0,0],[0,0,0,1,0,0,0,0,0,0] ....]。
您可以使用tf.one_hot function将 label_batch 转换为所需的表单。为此,请更改
y = tf.placeholder(tf.float32, [None,n_classes])
到
y = tf.placeholder(tf.float32, [None])
y_one_hot = tf.one_hot( y , 10 )
并且您必须更改对 y 的引用,而不是使用 y_one_hot
cross_entropy = -tf.reduce_sum(y_one_hot * tf.log(y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_one_hot, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
干杯!