tensorflow 数据读取

本文详细介绍了TensorFlow中三种主要的数据读取方法:供给数据、从文件读取数据和预加载数据。涵盖文件队列、批处理、乱序处理及线程管理等关键步骤,适合初学者和开发者深入理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TensorFlow程序读取数据一共有3种方法:

  • 供给数据(Feeding): 在TensorFlow程序运行的每一步, 让Python代码来供给数据。
  • 从文件读取数据: 在TensorFlow图的起始, 让一个输入管线从文件中读取数据。
  • 预加载数据: 在TensorFlow图中定义常量或变量来保存所有数据(仅适用于数据量比较小的情况)。

目录

数据读取

 一文件读取流程

    1.构造文件名队列   

将文件名列表交给tf.train.string_input_producer 函数.string_input_producer来生成一个先入先出的队列, 文件阅读器会需要它来读取数据

tf.train.string_input_producer(string_tensor, num_epochs=None, shuffle=True, seed=None, capacity=32, name=None)

  string_tensor:含有文件名+路径的1阶张量

  num_epochs:过几遍数据,默认过无限编

Returns: 文件队列

    2.读取与解码

根据你的文件格式, 选择对应的文件阅读器, 然后将文件名队列提供给阅读器的read方法。阅读器的read方法会输出一个key来表征输入的文件和其中的纪录(对于调试非常有用),同时得到一个字符串标量, 这个字符串标量可以被一个或多个解析器,或者转换操作将其解码为张量并且构造成为样本。

     key, value  =阅读器.read(queue, name=None)

  • key: 文件名
  • value: 样本

           文本格式/CSV 文件

                   读取 tf.TextLineReader()

 

                   解码    tf.decode_csv()

           图片格式

                   读取 tf.WholeFileReader()

                   解码 不同的图片格式有不同的解码方式

                             tf.image.decode_png()

           tf.image.decode_jpeg()

           二进制

                   读取   tf.FixedLengthRecordReader()

                   解码  tf.decode_raw()

         TFRecords文件

                 读取   tf.TFRecordReader()

                  解码   tf.parse_single_example()

    3.批处理

      在数据输入管线的末端, 我们需要有另一个队列来执行输入样本的训练,评价和推理。因此我们使用f.train.batch、tf.train.shuffle_batch 函数来对队列中的样本进行乱序处理

tf.train.batch(tensor_list, batch_size, num_threads=1, capacity=32, enqueue_many=False, shapes=None, name=None)

功能:读取指定大小(个数)的张量

Args:

  • tensor_list: The list of tensors to enqueue.
  • batch_size: 从列表中读取的批处理器大小
  • num_threads: 进入队列的线程数
  • capacity: An integer. The maximum number of elements in the queue.
  • enqueue_many: Whether each tensor in tensor_list is a single example.
  • shapes: (Optional) The shapes for each example. Defaults to the inferred shapes for tensor_list.
  • name: (Optional) A name for the operations.

Returns:

    A list of tensors with the same number and types as tensor_list.

   4.手动开启线程

tf.train.start_queue_runners(sess=None, coord=None, daemon=True, start=True, collection='queue_runners')

收集图中所有队列线程,同时默认启动线程

Args:

  • sess: Session used to run the queue ops. Defaults to the default session.
  • coord: Optional Coordinator for coordinating the started threads.
  • daemon: Whether the threads should be marked as daemons, meaning they don't block program exit.
  • start: Set to False to only create the threads, not start them.
  • collection: A GraphKey specifying the graph collection to get the queue runners from. Defaults to GraphKeys.QUEUE_RUNNERS.

Returns:

A list of threads.

class tf.train.Coordinator()

线程协调员,对线程进行管理和协调

request_stop() 请求停止

should_stop()   询问是否结束

join(threads, stop_grace_period_secs=120)回收线程

Returns: 线程协调器实例

 

 

 

      http://www.tensorfly.cn/tfdoc/images/AnimatedFileQueues.gif 

                             

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值