基于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編程判斷一個(gè)正整數(shù)是否為素?cái)?shù)的方法
這篇文章主要介紹了Python編程判斷一個(gè)正整數(shù)是否為素?cái)?shù)的方法,涉及Python數(shù)學(xué)運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-04-04Python利用jmespath模塊進(jìn)行json數(shù)據(jù)處理
jmespath是python的第三方模塊,是需要額外安裝的。它在python原有的json數(shù)據(jù)處理上做出了很大的貢獻(xiàn)。本文將詳細(xì)介紹如何利用jmespath實(shí)現(xiàn)json數(shù)據(jù)處理,需要的可以參考一下2022-03-03Python調(diào)用ChatGPT制作基于Tkinter的桌面時(shí)鐘
這篇文章主要為大家詳細(xì)介紹了Python如何調(diào)用ChatGPT制作基于Tkinter的桌面時(shí)鐘,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2023-03-03用Python實(shí)現(xiàn)批量生成法務(wù)函代碼
大家好,本篇文章主要講的是用Python實(shí)現(xiàn)批量生成法務(wù)函代碼,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02