zoukankan      html  css  js  c++  java
  • 吴裕雄 python 神经网络——TensorFlow 输入文件队列

    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)

  • 相关阅读:
    自定义 Repository 方法
    SpringData_JpaSpecificationExecutor接口
    SpringData_JpaRepository接口
    SpringData_PagingAndSortingRepository接口
    SpringData_CrudRepository接口
    SpringData修改和删除操作
    自定义查询语句SpringData
    SpringData关键字查询练习
    compile——生成ast
    vue-compile概述
  • 原文地址:https://www.cnblogs.com/tszr/p/10885390.html
Copyright © 2011-2022 走看看