基于tensorflow指定GPU運(yùn)行及GPU資源分配的幾種方式小結(jié)
1. 在終端執(zhí)行時(shí)設(shè)置使用哪些GPU(兩種方式)
(1) 如下(export 語(yǔ)句執(zhí)行一次就行了,以后再運(yùn)行代碼不用執(zhí)行)

(2) 如下

2. 代碼中指定(兩種方式)
(1)
import os os.environ["CUDA_VISIBLE_DEVICES"] = "1"
(2)
# Creates a graph.
with tf.device('/gpu:1'):
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
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)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print sess.run(c)
若想使用多個(gè)GPU,如下
c = []
for d in ['/gpu:0', '/gpu:1']:
with tf.device(d):
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3])
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2])
c.append(tf.matmul(a, b))
with tf.device('/cpu:0'):
sum = tf.add_n(c)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print sess.run(sum)
3.GPU資源分配
(1) 設(shè)置允許GPU增長(zhǎng)
config = tf.ConfigProto() config.gpu_options.allow_growth = True session = tf.Session(config=config, ...)
(2) 設(shè)置每個(gè)GPU內(nèi)存使用多少
config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 0.4 session = tf.Session(config=config, ...)
以上這篇基于tensorflow指定GPU運(yùn)行及GPU資源分配的幾種方式小結(jié)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python中數(shù)組nums[:]和nums的區(qū)別
本文主要介紹了python中數(shù)組nums[:]和nums的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
基于Tensorflow使用CPU而不用GPU問(wèn)題的解決
今天小編就為大家分享一篇基于Tensorflow使用CPU而不用GPU問(wèn)題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
python3?flask使用連接池連接數(shù)據(jù)庫(kù)實(shí)例
這篇文章主要為大家介紹了python3?flask使用連接池連接數(shù)據(jù)庫(kù)實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Python虛擬環(huán)境virtualenv的安裝與使用詳解
virtualenv可以用來(lái)管理互不干擾的獨(dú)立python虛擬環(huán)境,在有些場(chǎng)景下非常有用,下面這篇文章主要給大家介紹了Python虛擬環(huán)境virtualenv安裝與使用的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-05-05

