吴忠躺衫网络科技有限公司

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

關(guān)于TensorFlow在GPU中的使用規(guī)則

Tensorflowers ? 來源:cg ? 2018-12-04 09:27 ? 次閱讀

支持的設(shè)備

在一套標(biāo)準(zhǔn)系統(tǒng)中通常有多臺(tái)計(jì)算設(shè)備。TensorFlow 支持CPUGPU這兩種設(shè)備。它們均用strings表示。

例如:

"/cpu:0":機(jī)器的 CPU

"/device:GPU:0":機(jī)器的 GPU(如果有一個(gè))

"/device:GPU:1":機(jī)器的第二個(gè) GPU(以此類推)

如果 TensorFlow 指令中兼有 CPU 和 GPU 實(shí)現(xiàn),當(dāng)該指令分配到設(shè)備時(shí),GPU 設(shè)備有優(yōu)先權(quán)。例如,如果matmul同時(shí)存在 CPU 和 GPU 核函數(shù),在同時(shí)有cpu:0和gpu:0設(shè)備的系統(tǒng)中,gpu:0會(huì)被選來運(yùn)行matmul。

記錄設(shè)備分配方式

要找出您的指令和張量被分配到哪個(gè)設(shè)備,請(qǐng)創(chuàng)建會(huì)話并將log_device_placement配置選項(xiàng)設(shè)為True。

# Creates a graph.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))

您應(yīng)該會(huì)看到以下輸出內(nèi)容:

Device mapping:/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Tesla K40c, pci busid: 0000:05:00.0b: /job:localhost/replica:0/task:0/device:GPU:0a: /job:localhost/replica:0/task:0/device:GPU:0MatMul: /job:localhost/replica:0/task:0/device:GPU:0[[ 22. 28.][ 49. 64.]]

手動(dòng)分配設(shè)備

如果您希望特定指令在您選擇的設(shè)備(而非系統(tǒng)自動(dòng)為您選擇的設(shè)備)上運(yùn)行,您可以使用with tf.device創(chuàng)建設(shè)備上下文,這個(gè)上下文中的所有指令都將被分配在同一個(gè)設(shè)備上運(yùn)行。

# Creates a graph.with tf.device('/cpu:0'): 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))

您會(huì)看到現(xiàn)在a和b被分配到cpu:0。由于未明確指定運(yùn)行MatMul指令的設(shè)備,因此 TensorFlow 運(yùn)行時(shí)將根據(jù)指令和可用設(shè)備(此示例中的gpu:0)選擇一個(gè)設(shè)備,并會(huì)根據(jù)要求自動(dòng)復(fù)制設(shè)備間的張量。

Device mapping:/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Tesla K40c, pci busid: 0000:05:00.0b: /job:localhost/replica:0/task:0/cpu:0a: /job:localhost/replica:0/task:0/cpu:0MatMul: /job:localhost/replica:0/task:0/device:GPU:0[[ 22. 28.][ 49. 64.]]

允許增加 GPU 內(nèi)存

默認(rèn)情況下,TensorFlow 會(huì)映射進(jìn)程可見的所有 GPU 的幾乎所有 GPU 內(nèi)存(取決于CUDA_VISIBLE_DEVICES)。通過減少內(nèi)存碎片,可以更有效地使用設(shè)備上相對(duì)寶貴的 GPU 內(nèi)存資源。

在某些情況下,最理想的是進(jìn)程只分配可用內(nèi)存的一個(gè)子集,或者僅根據(jù)進(jìn)程需要增加內(nèi)存使用量。TensorFlow 在 Session 上提供兩個(gè) Config 選項(xiàng)來進(jìn)行控制。

第一個(gè)是allow_growth選項(xiàng),它試圖根據(jù)運(yùn)行時(shí)的需要來分配 GPU 內(nèi)存:它剛開始分配很少的內(nèi)存,隨著 Session 開始運(yùn)行并需要更多 GPU 內(nèi)存,我們會(huì)擴(kuò)展 TensorFlow 進(jìn)程所需的 GPU 內(nèi)存區(qū)域。請(qǐng)注意,我們不會(huì)釋放內(nèi)存,因?yàn)檫@可能導(dǎo)致出現(xiàn)更嚴(yán)重的內(nèi)存碎片情況。要開啟此選項(xiàng),請(qǐng)通過以下方式在 ConfigProto 中設(shè)置選項(xiàng):

config = tf.ConfigProto()config.gpu_options.allow_growth = Truesession = tf.Session(config=config, ...)

如要真正限制 TensorFlow 進(jìn)程可使用的 GPU 內(nèi)存量,這非常實(shí)用。

在多 GPU 系統(tǒng)中使用單一 GPU

如果您的系統(tǒng)中有多個(gè) GPU,則默認(rèn)情況下將選擇 ID 最小的 GPU。如果您希望在其他 GPU 上運(yùn)行,則需要顯式指定偏好設(shè)置:

# Creates a graph.with tf.device('/device:GPU:2'): 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))

如果您指定的設(shè)備不存在,您會(huì)看到InvalidArgumentError:

InvalidArgumentError: Invalid argument: Cannot assign a device to node 'b':Could not satisfy explicit device specification '/device:GPU:2' [[Node: b = Const[dtype=DT_FLOAT, value=Tensor, _device="/device:GPU:2"]()]]

當(dāng)指定設(shè)備不存在時(shí),如果您希望 TensorFlow 自動(dòng)選擇現(xiàn)有的受支持設(shè)備來運(yùn)行指令,則可以在創(chuàng)建會(huì)話時(shí)將配置選項(xiàng)中的allow_soft_placement設(shè)為True。

# Creates a graph.with tf.device('/device:GPU:2'): 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 allow_soft_placement and log_device_placement set# to True.sess = tf.Session(config=tf.ConfigProto( allow_soft_placement=True, log_device_placement=True))# Runs the op.print(sess.run(c))

使用多個(gè) GPU

如果您想要在多個(gè) GPU 上運(yùn)行 TensorFlow,則可以采用多塔式方式構(gòu)建模型,其中每個(gè)塔都會(huì)分配給不同 GPU。例如:

# Creates a graph.c = []for d in ['/device:GPU:2', '/device:GPU:3']: 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))

您會(huì)看到以下輸出內(nèi)容:

Device mapping:/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Tesla K20m, pci busid: 0000:02:00.0/job:localhost/replica:0/task:0/device:GPU:1 -> device: 1, name: Tesla K20m, pci busid: 0000:03:00.0/job:localhost/replica:0/task:0/device:GPU:2 -> device: 2, name: Tesla K20m, pci busid: 0000:83:00.0/job:localhost/replica:0/task:0/device:GPU:3 -> device: 3, name: Tesla K20m, pci busid: 0000:84:00.0Const_3: /job:localhost/replica:0/task:0/device:GPU:3Const_2: /job:localhost/replica:0/task:0/device:GPU:3MatMul_1: /job:localhost/replica:0/task:0/device:GPU:3Const_1: /job:localhost/replica:0/task:0/device:GPU:2Const: /job:localhost/replica:0/task:0/device:GPU:2MatMul: /job:localhost/replica:0/task:0/device:GPU:2AddN: /job:localhost/replica:0/task:0/cpu:0[[ 44. 56.][ 98. 128.]]

cifar10 教程就是個(gè)很好的例子(https://tensorflow.google.cn/tutorials/images/deep_cnn?hl=zh-CN),演示了如何使用多個(gè) GPU 進(jìn)行訓(xùn)練。

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • gpu
    gpu
    +關(guān)注

    關(guān)注

    28

    文章

    4777

    瀏覽量

    129360
  • tensorflow
    +關(guān)注

    關(guān)注

    13

    文章

    329

    瀏覽量

    60631

原文標(biāo)題:TensorFlow 指南:GPU 的使用

文章出處:【微信號(hào):tensorflowers,微信公眾號(hào):Tensorflowers】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。

收藏 人收藏

    評(píng)論

    相關(guān)推薦

    關(guān)于 TensorFlow

    節(jié)點(diǎn)間相互聯(lián)系的多維數(shù)據(jù)數(shù)組,即張量(tensor)。它靈活的架構(gòu)讓你可以多種平臺(tái)上展開計(jì)算,例如臺(tái)式計(jì)算機(jī)的一個(gè)或多個(gè)CPU(或GPU),服務(wù)器,移動(dòng)設(shè)備等等。
    發(fā)表于 03-30 19:57

    使用 TensorFlow, 你必須明白 TensorFlow

    :1": 機(jī)器的第二個(gè) GPU, 以此類推.閱讀使用GPU章節(jié), 了解 TensorFlow GPU 使用的更多信息.交互式使用文檔的 P
    發(fā)表于 03-30 20:03

    阿里云Kubernetes容器服務(wù)上打造TensorFlow實(shí)驗(yàn)室

    GPU的使用,同時(shí)支持最新的TensorFLow版本, 對(duì)于數(shù)據(jù)科學(xué)家來說既是復(fù)雜的,同時(shí)也是浪費(fèi)精力的。阿里云的Kubernetes集群上,您可以通過簡(jiǎn)單的按鈕提交創(chuàng)建一套完整的Tenso
    發(fā)表于 05-10 10:24

    可以vGPU配置文件上運(yùn)行TensorFlow嗎?

    大家好,我有一些問題,你們可能知道答案。我閱讀了文檔,但我還不確定。開發(fā)團(tuán)隊(duì)中有一些人正在為TensorFlow(AI項(xiàng)目)尋找GPU。我們對(duì)工作站和Dockers上運(yùn)行的Quadro GP
    發(fā)表于 09-18 16:35

    深度學(xué)習(xí)框架TensorFlow&TensorFlow-GPU詳解

    TensorFlow&TensorFlow-GPU:深度學(xué)習(xí)框架TensorFlow&TensorFlow-GPU的簡(jiǎn)介、安裝、使用方法詳細(xì)攻略
    發(fā)表于 12-25 17:21

    tensorflow-gpu安裝報(bào)錯(cuò)的修改

    tensorflow-gpu安裝遇到的一些問題解決
    發(fā)表于 05-20 10:25

    TensorFlow是什么

    更長(zhǎng)。TensorFlow 使這一切變得更加簡(jiǎn)單快捷,從而縮短了想法到部署之間的實(shí)現(xiàn)時(shí)間。本教程,你將學(xué)習(xí)如何利用 TensorFlow 的功能來實(shí)現(xiàn)深度神經(jīng)網(wǎng)絡(luò)。
    發(fā)表于 07-22 10:14

    TensorFlow安裝和下載(超詳細(xì))

    TensorFlow安裝具體做法命令行中使用以下命令創(chuàng)建 conda 環(huán)境(如果使用 Windows,最好在命令行以管理員身份執(zhí)行):conda create -n tensorflow
    發(fā)表于 07-22 10:25

    TensorFlow教程|常見問題

    (name): context 創(chuàng)建操作(operation),這樣可以指定的設(shè)備上運(yùn)行操作(operation)。 關(guān)于 TensorFlow 怎樣將操作(operations)
    發(fā)表于 07-27 18:33

    TensorFlow XLA加速線性代數(shù)編譯器

    編譯:會(huì)話級(jí)別打開JIT編譯: 這是手動(dòng)打開 JIT 編譯: 還可以通過將操作指定在特定的 XLA 設(shè)備(XLA_CPU 或 XLA_GPU)上,通過 XLA 來運(yùn)行計(jì)算: AoT編譯:獨(dú)立使用 tfcompile 將
    發(fā)表于 07-28 14:31

    TensorFlow指定CPU和GPU設(shè)備操作詳解

    TensorFlow 支持 CPU 和 GPU。它也支持分布式計(jì)算。可以一個(gè)或多個(gè)計(jì)算機(jī)系統(tǒng)的多個(gè)設(shè)備上使用 TensorFlowTensorF
    發(fā)表于 07-28 14:33

    Mali GPU支持tensorflow或者caffe等深度學(xué)習(xí)模型嗎

    Mali GPU 支持tensorflow或者caffe等深度學(xué)習(xí)模型嗎? 好像caffe2go和tensorflow lit可以部署到ARM,但不知道是否支持
    發(fā)表于 09-16 14:13

    如何在AMD的GPU上運(yùn)行TensorFlow

    ROCm 即 Radeon 開放生態(tài)系統(tǒng) (Radeon Open Ecosystem),是我們 Linux 上進(jìn)行 GPU 計(jì)算的開源軟件基礎(chǔ)。而 TensorFlow 實(shí)現(xiàn)則使用了 MIOpen,這是一個(gè)適用于深度學(xué)習(xí)的高
    的頭像 發(fā)表于 10-04 08:59 ?2.4w次閱讀

    GPU上利用TensorFlow Serving 部署ResNet

    本文中,我們將展示以同樣的方式運(yùn)行經(jīng) TF-TRT 轉(zhuǎn)換的模型有多簡(jiǎn)單。此 docker run 命令會(huì)啟動(dòng) TensorFlow Serving 服務(wù)器,以提供 /tmp/resnet 已下載
    的頭像 發(fā)表于 03-05 17:51 ?7700次閱讀
    <b class='flag-5'>在</b><b class='flag-5'>GPU</b>上利用<b class='flag-5'>TensorFlow</b> Serving 部署ResNet

    TensorFlow-DirectML TensorFlowGPU范圍擴(kuò)展

    ./oschina_soft/tensorflow-directml.zip
    發(fā)表于 06-17 09:18 ?1次下載
    <b class='flag-5'>TensorFlow</b>-DirectML <b class='flag-5'>TensorFlow</b>的<b class='flag-5'>GPU</b>范圍擴(kuò)展
    大发888线上娱乐城加盟合作| 百家乐官网分享| 百家乐娱乐城彩金| 大发888备用网址大全| 百家乐官网的奥秘| 百家乐赔率计算| 百家乐官网游戏如何玩| 百家乐官网平台注册| 大发888官方网站下载| 赌场百家乐官网台| 威尼斯人娱乐城首存优惠| 百家乐官网珠盘路| 大发888真人真钱游戏| 百家乐官网的看路技巧| 战神娱乐| 百家乐官网博彩免费体验金3| 大发888开户注册会员| 菲律宾百家乐试玩| 陆河县| 百家乐平台网| 朝阳县| 百家乐透明出千牌靴| 电子百家乐官网技巧| 富川| 百家乐生活馆拖鞋| 澳门赌百家乐官网打法| 水果机破解| 百家乐官网开户| 百家乐官网技巧大全| 喜达百家乐的玩法技巧和规则| 百家乐官网赌场游戏平台| 在线赌博网站| 真人百家乐官网赢钱| 香港六合彩开| 百家乐EA平台| 百家乐官网直揽经验| 百家乐官网取胜秘笈| 视频百家乐平台| 鑫鑫百家乐官网的玩法技巧和规则| 百家乐扑克玩法| 曲松县|