国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Tensorflow中tf.ConfigProto()的用法詳解

2020-02-15 21:21:12
字體:
供稿:網(wǎng)友

參考Tensorflow Machine Leanrning Cookbook

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

具體代碼如下:

import tensorflow as tfsession_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

設置線程一個操作內(nèi)部并行運算的線程數(shù),比如矩陣乘法,如果設置為0,則表示以最優(yōu)的線程數(shù)處理

intra_op_parallelism_threads=0

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

allow_soft_placement=True

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

其他選項

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

session_config.gpu_options.allow_growth = True

當使用GPU時,設置GPU內(nèi)存使用最大比例

session_config.gpu_options.per_process_gpu_memory_fraction = 0.4

是否能夠使用GPU進行運算

tf.test.is_built_with_cuda()

另外的處理方法

import tensorflow as tfsess = 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))

以上這篇Tensorflow中tf.ConfigProto()的用法詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持武林站長站。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 湘阴县| 左云县| 蕲春县| 定远县| 会同县| 逊克县| 大方县| 湖州市| 饶平县| 班玛县| 南昌市| 翁源县| 通渭县| 梁山县| 都昌县| 雅江县| 河北区| 桐城市| 绥芬河市| 道孚县| 玉环县| 石景山区| 墨脱县| 靖江市| 遵义市| 宁安市| 龙山县| 郁南县| 长顺县| 彩票| 涡阳县| 泗洪县| 安徽省| 兰溪市| 中西区| 犍为县| 集贤县| 济阳县| 秦安县| 北川| 保定市|