Tensorflow 多線程設定_TensorFlow

來源:互聯網
上載者:User
Tensorflow 多線程設定
一. 通過 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 和 Decoderreader = 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) # 定義 Readerreader = tf.TextLineReader()  key, value = reader.read(filename_queue)  #定義了多個 Decoder, 每個 Decoder 跟一個 Reader 相連, 即有多個 Readerexample_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) 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.