Tensorflow 多线程设置方式

作者:jingxian 时间:2021-09-29 21:53:50 

一. 通过 ConfigProto 设置多线程

(具体参数功能及描述见 tensorflow/core/protobuf/config.proto)

在进行 tf.ConfigProto() 初始化时,可以通过设置相应的参数,来控制每个操作符 op 并行计算的线程个数或 session 线程池的线程数。主要涉及的参数有以下三个:

1. intra_op_parallelism_threads 控制运算符op内部的并行
当运算符 op 为单一运算符,并且内部可以实现并行时,如矩阵乘法,reduce_sum 之类的操作,可以通过设置 intra_op_parallelism_threads 参数来并行。

2. inter_op_parallelism_threads 控制多个运算符op之间的并行计算
当有多个运算符 op,并且他们之间比较独立,运算符和运算符之间没有直接的路径 Path 相连。Tensorflow会尝试并行地计算他们,使用由 inter_op_parallelism_threads 参数来控制数量的一个线程池。
在第一次创建会话将设置将来所有会话的线程数,除非是配置了 session_inter_op_thread_pool 选项。

3. session_inter_op_thread_pool 配置会话线程池。
如果会话线程池的 num_threads 为 0,使用 inter_op_parallelism_threads 选项。

二. 通过队列进行数据读取时设置多线程

(具体函数功能及描述见 tensorflow/python/training/input.py)

1. 通过以下函数进行样本批处理时,可以通过设置 num_threads 来设置单个 Reader 多线程读取

1) batch(tensors, batch_size, num_threads=1, capacity=32,
    enqueue_many=False, shapes=None, dynamic_pad=False,
    allow_smaller_final_batch=False, shared_name=None, name=None)

2) maybe_batch(tensors, keep_input, batch_size, num_threads=1, capacity=32,
       enqueue_many=False, shapes=None, dynamic_pad=False,
       allow_smaller_final_batch=False, shared_name=None, name=None)

3) shuffle_batch(tensors, batch_size, capacity, min_after_dequeue,
        num_threads=1, seed=None, enqueue_many=False, shapes=None,
        allow_smaller_final_batch=False, shared_name=None, name=None)

4) maybe_shuffle_batch(tensors, batch_size, capacity, min_after_dequeue,
           keep_input, num_threads=1, seed=None,
           enqueue_many=False, shapes=None,
           allow_smaller_final_batch=False, shared_name=None,
           name=None)

例:


import tensorflow as tf

filenames = ['A.csv', 'B.csv', 'C.csv']
# 生成一个先入先出队列和一个 QueueRunner,生成文件名队列
filename_queue = tf.train.string_input_producer(filenames, shuffle=False)

# 定义 Reader 和 Decoder
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
example, label = tf.decode_csv(value, record_defaults=[['null'], ['null']])

# 使用tf.train.batch() 会为 graph 添加一个样本队列和一个 QueueRunner。
# 经过 Reader 读取文件和 Decoder 解码后数据会进入这个队列,再批量出队。
# tf.train.batch() 这里只有一个 Reader,可以设置多线程

example_batch, label_batch = tf.train.batch([example, label], batch_size=5)

with tf.Session() as sess:
 coord = tf.train.Coordinator()
 threads = tf.train.start_queue_runners(coord=coord)
 for i in range(10):
   e_val,l_val = sess.run([example_batch,label_batch])
   print e_val,l_val
 coord.request_stop()
 coord.join(threads)

2. 通过以下函数进行样本批处理时,可以通过设置 Decoder 和 Reader 的个数来设置多 Reader 读取,其中每个 Reader 使用一个线程

1) batch_join(tensors_list, batch_size, capacity=32, enqueue_many=False,
       shapes=None, dynamic_pad=False, allow_smaller_final_batch=False,
       shared_name=None, name=None):

2) maybe_batch_join(tensors_list, keep_input, batch_size, capacity=32,
          enqueue_many=False, shapes=None, dynamic_pad=False,
          allow_smaller_final_batch=False, shared_name=None,
          name=None)

3) shuffle_batch_join(tensors_list, batch_size, capacity,
           min_after_dequeue, seed=None, enqueue_many=False,
           shapes=None, allow_smaller_final_batch=False,
           shared_name=None, name=None)

4) maybe_shuffle_batch_join(tensors_list, batch_size, capacity,
              min_after_dequeue, keep_input, seed=None,
              enqueue_many=False, shapes=None,
              allow_smaller_final_batch=False, shared_name=None,
              name=None)

例:


import tensorflow as tf

filenames = ['A.csv', 'B.csv', 'C.csv']
# 生成一个先入先出队列和一个 QueueRunner,生成文件名队列
filename_queue = tf.train.string_input_producer(filenames, shuffle=False)

# 定义 Reader
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)

#定义了多个 Decoder, 每个 Decoder 跟一个 Reader 相连, 即有多个 Reader
example_list = [tf.decode_csv(value, record_defaults=[['null'], ['null']])
        for _ in range(2)] # Decoder 和 Reader 为 2

# 使用tf.train.batch_join() 会为 graph 添加一个样本队列和一个 QueueRunner。
# 经过多个 Reader 读取文件和 Decoder 解码后数据会进入这个队列,再批量出队。  
# 使用 tf.train.batch_join(), 可以使用多个 Reader 并行读取数据。每个 Reader 使用一个线程
example_batch, label_batch = tf.train.batch_join(example_list, batch_size=5)
with tf.Session() as sess:
 coord = tf.train.Coordinator()
 threads = tf.train.start_queue_runners(coord=coord)
 for i in range(10):
   e_val,l_val = sess.run([example_batch,label_batch])
   print e_val,l_val
 coord.request_stop()
 coord.join(threads)

来源:https://blog.csdn.net/s_sunnyy/article/details/72924317

标签:Tensorflow,多线程
0
投稿

猜你喜欢

  • JavaScript变量类型以及变量作用域详解

    2023-08-12 08:50:17
  • Asp用正则表达式获取文章中的所有图片地址

    2010-07-17 13:11:00
  • php比较多维数组中值的大小排序实现代码

    2023-11-24 13:22:32
  • 通过事务日志解决SQL Server常见四大故障(二)

    2009-03-25 13:51:00
  • python使用tqdm模块处理文件阅读进度条显示

    2022-09-08 11:29:17
  • 基于SQL Server的C/S数据库应用系统

    2009-01-21 14:44:00
  • Javascript 中 String.replace( ) 的妙用

    2008-08-05 18:08:00
  • Python面向对象的三大特性封装、继承、多态

    2023-12-11 05:15:52
  • Mysql的服务无法启动的1067错误解决

    2012-01-05 19:31:56
  • python 实现目录复制的三种小结

    2023-09-01 12:17:20
  • SQLSERVER查询所有数据库名,表名,和字段名的语句

    2012-01-29 18:07:44
  • Python模块的制作方法实例分析

    2021-09-06 05:57:26
  • Python WebSocket长连接心跳与短连接的示例

    2022-08-20 04:05:15
  • Python编程调用百度API实现地理位置经纬度坐标转换示例

    2023-12-30 11:44:38
  • jQuery性能优化指南[译]

    2009-05-12 11:54:00
  • CSS关于Border你可能会不注意的东西

    2007-10-20 13:50:00
  • 详解django中Template语言

    2022-12-01 21:20:59
  • 5个有趣的浏览器地址栏Javascript代码

    2008-07-21 13:04:00
  • 怎样设置密码保护问题

    2009-02-16 13:12:00
  • 基于google图表API的jquery组件 I

    2010-03-01 10:20:00
  • asp之家 网络编程 m.aspxhome.com