我正在使用tensorflow库在python中创建一个神经网络来对CIFAR-10数据集进行分类。问题是我找不到转换批次的方法,以便列车步骤接受提要。这是我的代码,我已经验证的部分可以用描述它们的注释替换:
# import statements
# declare some global variables
# also declare filepaths
def read_cifar10(filename_queue):
# parse cifar10 file, return as label with uint8image (Cifar10Record)
def generate_batch(image, label):
# generate shuffled batch, given images and labels
def get_imgs(test = False, fmin = 1, files = 1):
# generate the filename(s) and the filename_queue
# read the input using read_cifar10
# cast it to uint8image and float32
# apply distortions
# set the shape of the image and label
# return the batch, made using generate_batch
# build placeholders for input and output
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
# create a weight variable with a given shape
# slight amount of variation
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev = 0.1)
return tf.Variable(initial)
# same but for bias variables
def bias_variable(shape):
initial = tf.constant(0.1, shape = shape)
return tf.Variable(initial)
# convolve it, do some more layers, apply relu, make dropout and readout layers, etc
# do softmax regression
# define the loss function, training step, accuracy function
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y_, logits = y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
guess = tf.argmax(y_conv, 1)
answer = tf.argmax(y_, 1)
correct_prediction = tf.equal(guess, answer)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
def train_nn():
for i in range(TRAIN_STEPS):
# this is where the error occurs
batch = get_imgs(files = 5).eval()
# every 100 steps output training accuracy
if i % 100 == 0:
train_accuracy = accuracy.eval(feed_dict = {x: batch[0], y_: batch[1], keep_prob: 0.5})
print('step %d, training accuracy %g' % (i, train_accuracy))
train_step.run(feed_dict = {x: batch[0], y_: batch[1], keep_prob: 0.5})
def test_nn():
# test it
batch = get_imgs(test = True).eval()
print('test accuracy %g' % accuracy.eval(feed_dict = {x: batch[0], y_: batch[1], keep_prob: 1.0}))
# create session
with tf.Session() as sess:
# initialize global variables
sess.run(tf.global_variables_initializer())
# make queue runners
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess = sess, coord = coord)
# train it, test it
sess.run(train_nn())
sess.run(test_nn())
coord.request_stop()
coord.join(threads)
我尝试过的一些事情和结果:
我怀疑问题是批量转换,使用tf.train.Coordinator()排队还是占位符。任何帮助将不胜感激。