溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

tf.ConfigProto()如何在Tensorflow中使用

發布時間:2021-01-11 15:49:16 來源:億速云 閱讀:307 作者:Leah 欄目:開發技術

tf.ConfigProto()如何在Tensorflow中使用?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。

tf.ConfigProto()主要的作用是配置tf.Session的運算方式,比如gpu運算或者cpu運算

具體代碼如下:

import tensorflow as tf

session_config = tf.ConfigProto(
   log_device_placement=True,
   inter_op_parallelism_threads=0,
   intra_op_parallelism_threads=0,
   allow_soft_placement=True)

sess = tf.Session(config=session_config)

a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2,3], name='b')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3,2], name='b')

c = tf.matmul(a,b)
print(sess.run(c))

具體解釋

log_device_placement=True

設置為True時,會打印出TensorFlow使用了那種操作

inter_op_parallelism_threads=0

設置線程一個操作內部并行運算的線程數,比如矩陣乘法,如果設置為0,則表示以最優的線程數處理

intra_op_parallelism_threads=0

設置多個操作并行運算的線程數,比如 c = a + b,d = e + f . 可以并行運算

allow_soft_placement=True

有時候,不同的設備,它的cpu和gpu是不同的,如果將這個選項設置成True,那么當運行設備不滿足要求時,會自動分配GPU或者CPU。

其他選項

當使用GPU時候,Tensorflow運行自動慢慢達到最大GPU的內存

session_config.gpu_options.allow_growth = True

當使用GPU時,設置GPU內存使用最大比例

session_config.gpu_options.per_process_gpu_memory_fraction = 0.4

是否能夠使用GPU進行運算

tf.test.is_built_with_cuda()

另外的處理方法

import tensorflow as tf

sess = tf.Session()

with tf.device('/cpu:0'):
  a = tf.constant([1.0, 3.0, 5.0], shape=[1, 3])
  b = tf.constant([2.0, 4.0, 6.0], shape=[3, 1])

  with tf.device('/gpu:0'):
    c = tf.matmul(a, b)
    c = tf.reshape(c, [-1])

  with tf.device('/gpu:0'):
    d = tf.matmul(b, a)
    flat_d = tf.reshape(d, [-1])

  combined = tf.multiply(c, flat_d)
  print(sess.run(combined))

看完上述內容,你們掌握tf.ConfigProto()如何在Tensorflow中使用的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女