• 吴裕雄 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)

  • 相关阅读:
    brewhome
    WIN7下安装SQL server 2008 R2“出现性能计数器注册表配置单元一致性”失败的解决办法(新)
    话说ELK使用安装,结合.NET Core、ABP框架Nlog日志
    支付系统架构
    高可用Redis服务架构分析与搭建
    基于STS和JWT的微服务身份认证
    全面解读NoSQL数据库Redis的核心技术与应用实践
    Redis应用及安装
    微服务实践分享与探讨
    Docker的核心概念,镜像操作
  • 原文地址:https://www.cnblogs.com/tszr/p/10885390.html
Copyright © 2020-2023  润新知