CIFAR-10神经网络不接受批

时间:2017-08-14 17:35:34

标签: python python-2.7 debugging tensorflow

我正在使用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)

我尝试过的一些事情和结果:

  • batch = get_imgs(args)给出'TypeError:feed的值不能是tf.Tensor对象。可接受的Feed值包括Python标量,字符串,列表,numpy ndarrays或TensorHandles。'
  • batch = get_imgs(args).eval()给出'AttributeError:'tuple'对象没有属性'eval''
  • batch = sess.run(get_imgs(args))导致程序无限期运行而没有输出
  • 打印类型(批处理)表示批次是元组
  • 打印批次给出了张量的描述
  • 打印batch.eval()或type(batch.eval())给出'W tensorflow / core / kernels / queue_base.cc:302] _3_input_producer:在队列未关闭时跳过取消的队列取消'

我怀疑问题是批量转换,使用tf.train.Coordinator()排队还是占位符。任何帮助将不胜感激。

0 个答案:

没有答案
相关问题