日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Tensorflow中tf.ConfigProto()详解

發(fā)布時間:2023/11/28 生活经验 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Tensorflow中tf.ConfigProto()详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

參考Tensorflow Machine Leanrning Cookbook

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))

?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

?

具體解釋

log_device_placement=True

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


inter_op_parallelism_threads=0

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


intra_op_parallelism_threads=0

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


allow_soft_placement=True

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

?


其他選項

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

session_config.gpu_options.allow_growth = True
1


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

session_config.gpu_options.per_process_gpu_memory_fraction = 0.4
1


是否能夠使用GPU進行運算

tf.test.is_built_with_cuda()
1

?


另外的處理方法

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))
---------------------
作者:泥石流中的一股清流
來源:CSDN
原文:https://blog.csdn.net/qq_31261509/article/details/79746114
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請附上博文鏈接!

總結(jié)

以上是生活随笔為你收集整理的Tensorflow中tf.ConfigProto()详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。