溫馨提示×

溫馨提示×

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

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

基于tensorflow指定GPU運行及GPU資源分配的幾種方式小結(jié)

發(fā)布時間:2020-10-25 21:47:33 來源:腳本之家 閱讀:250 作者:青松愉快 欄目:開發(fā)技術(shù)

1. 在終端執(zhí)行時設(shè)置使用哪些GPU(兩種方式)

(1) 如下(export 語句執(zhí)行一次就行了,以后再運行代碼不用執(zhí)行)

基于tensorflow指定GPU運行及GPU資源分配的幾種方式小結(jié)

(2) 如下

基于tensorflow指定GPU運行及GPU資源分配的幾種方式小結(jié)

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)

若想使用多個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增長

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config=config, ...)

(2) 設(shè)置每個GPU內(nèi)存使用多少

config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.4
session = tf.Session(config=config, ...)

以上這篇基于tensorflow指定GPU運行及GPU資源分配的幾種方式小結(jié)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細節(jié)

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

AI