首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >训练时的流错误: op 'shuffle_batch‘引起的

训练时的流错误: op 'shuffle_batch‘引起的
EN

Stack Overflow用户
提问于 2017-09-06 15:31:42
回答 1查看 662关注 0票数 0

我正在尝试从TFRecord文件中读取图像和标签,然后使用这些文件进行训练。我知道我的TFRecord文件存在,并检查它是否包含1000个图像和标签。我的问题似乎只有当我想把管道作为训练的投入时才出现。我对python和张量流很陌生,不知道如何解决这个问题。

我得到在tf.train.shuffle_batch发生的以下错误

..。

由images_batch、labels_batch =tf.train.shuffle_batch(图像、标签、batch_size=10、capacity=1000、min_after_dequeue=2)中定义为:文件“C:/AI/projects/DataGen/tra.py”第40行中的op 'shuffle_batch‘引起的

..。

下面是我的代码,从各种mnist示例中拼凑而成

代码语言:javascript
复制
import tensorflow as tf


def read_and_decode_single_example(filename):
    # first construct a queue containing a list of filenames.
    # this lets a user split up there dataset in multiple files to keep
    # size down
    filename_queue = tf.train.string_input_producer([filename],
                                                num_epochs=None)
    # Unlike the TFRecordWriter, the TFRecordReader is symbolic
    reader = tf.TFRecordReader()
    # One can read a single serialized example from a filename
    # serialized_example is a Tensor of type string.
    _, serialized_example = reader.read(filename_queue)
    # The serialized example is converted back to actual values.
    # One needs to describe the format of the objects to be returned

    feature = {'image': tf.FixedLenFeature([], tf.string),
           'label': tf.FixedLenFeature([], tf.int64)}

    features = tf.parse_single_example(serialized_example, features=feature)

    # now return the converted data
    label = tf.cast(features['label'], tf.float32)
    image = tf.decode_raw(features['image'], tf.float32)
    image = tf.reshape(image, [28, 28, 3])
    return label, image


with tf.Session() as sess:
    sess.run(tf.local_variables_initializer())
    sess.run(tf.global_variables_initializer())

    # get single examples
    label, image = read_and_decode_single_example("train.tfrecords")

    image = tf.cast(image, tf.float32) / 255.

    # groups examples into batches randomly
    images_batch, labels_batch = tf.train.shuffle_batch([image, label], batch_size=10, capacity=1000, min_after_dequeue=2)

    # The model is:
    #
    # Y = softmax( X * W + b)
    #              X: matrix for rgb images of 28x28 pixels, flattened (there are 100 images in a mini-batch)
    #              W: weight matrix with (28x28x3) lines and 10 columns
    #              b: bias vector with 10 dimensions
    #              +: add with broadcasting: adds the vector to each line of the matrix (numpy)
    #              softmax(matrix) applies softmax on each line
    #              softmax(line) applies an exp to each value then divides by the norm of the resulting line
    #              Y: output matrix with 100 lines and 10 columns

    # input X: 28x28x3 RGB images
    X = images_batch
    # correct answers will go here
    Y_ = labels_batch
    # weights W[28 * 28 * 3, 10]
    W = tf.Variable(tf.zeros([28 * 28 * 3, 10]))
    # biases b[10]
    b = tf.Variable(tf.zeros([10]))

    # flatten the images into a single line of pixels
    # -1 in the shape definition means "the only possible dimension that will preserve the number of elements"
    XX = tf.reshape(X, [-1, 28 * 28 * 3])

    # The model
    Y = tf.nn.softmax(tf.matmul(XX, W) + b)

    # loss function: cross-entropy = - sum( Y_i * log(Yi) )
    #                           Y: the computed output vector
    #                           Y_: the desired output vector

    # cross-entropy
    # log takes the log of each element, * multiplies the tensors element by element
    # reduce_mean will add all the components in the tensor
    # so here we end up with the total cross-entropy for all images in the batch
    cross_entropy = -tf.reduce_mean(Y_ * tf.log(Y)) * 100.0  # normalized for batches of 100 images,
    # *10 because  "mean" included an unwanted division by 10

    # accuracy of the trained model, between 0 (worst) and 1 (best)
    correct_prediction = tf.equal(tf.argmax(Y, 1), tf.argmax(Y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    # training, learning rate = 0.005
    train_step = tf.train.GradientDescentOptimizer(0.005).minimize(cross_entropy)

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)

    for i in range(100 + 1):
        print(i)
        sess.run(train_step)

    coord.request_stop()

    # Wait for threads to stop
    coord.join(threads)
    sess.close()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-07 07:25:51

我将初始化移到tf.train.start_queue_runners调用之前,这解决了问题,即在建立模型之后

代码语言:javascript
复制
sess.run(tf.local_variables_initializer())
sess.run(tf.global_variables_initializer())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46079186

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档