首页 > 编程语言 > 详细

吴裕雄 python 神经网络——TensorFlow 输入文件队列

时间:2019-05-18 13:30:48      阅读:104      评论:0      收藏:0      [点我收藏+]
import tensorflow as tf

def _int64_feature(value):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))

num_shards = 2
instances_per_shard = 2
for i in range(num_shards):
    filename = (E:\\temp\\data.tfrecords-%.5d-of-%.5d % (i, num_shards)) 
    # 将Example结构写入TFRecord文件。
    writer = tf.python_io.TFRecordWriter(filename)
    for j in range(instances_per_shard):
    # Example结构仅包含当前样例属于第几个文件以及是当前文件的第几个样本。
        example = tf.train.Example(features=tf.train.Features(feature={
            i: _int64_feature(i),
            j: _int64_feature(j)}))
        writer.write(example.SerializeToString())
    writer.close()  
    
files = tf.train.match_filenames_once("E:\\temp\\data.tfrecords-*")
filename_queue = tf.train.string_input_producer(files, shuffle=False) 
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
      serialized_example,
      features={
          i: tf.FixedLenFeature([], tf.int64),
          j: tf.FixedLenFeature([], tf.int64),
      })
with tf.Session() as sess:
    sess.run([tf.global_variables_initializer(), tf.local_variables_initializer()])
    print(sess.run(files))
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    for i in range(6):
        print(sess.run([features[i], features[j]]))
    coord.request_stop()
    coord.join(threads)

技术分享图片

example, label = features[i], features[j]
batch_size = 2
capacity = 1000 + 3 * batch_size
example_batch, label_batch = tf.train.batch([example, label], batch_size=batch_size, capacity=capacity)
with tf.Session() as sess:
    tf.global_variables_initializer().run()
    tf.local_variables_initializer().run()
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    for i in range(3):
        cur_example_batch, cur_label_batch = sess.run([example_batch, label_batch])
        print(cur_example_batch, cur_label_batch)
    coord.request_stop()
    coord.join(threads)

技术分享图片

 

 

吴裕雄 python 神经网络——TensorFlow 输入文件队列

原文:https://www.cnblogs.com/tszr/p/10885390.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!