1. 程式人生 > >TensorFlow中指定GPU及GPU視訊記憶體設定

TensorFlow中指定GPU及GPU視訊記憶體設定

檢視機器上GPU情況

命令: nvidia-smi

功能:顯示機器上gpu的情況

命令: nvidia-smi -l

功能:定時更新顯示機器上gpu的情況

其中左上側有0、1、2、3的編號,表示GPU的編號,在後面指定GPU時需要使用這個編號。

在終端執行程式時指定GPU

CUDA_VISIBLE_DEVICES=1   python  your_file.py

這樣在跑你的網路之前,告訴程式只能看到1號GPU,其他的GPU它不可見

可用的形式如下:

CUDA_VISIBLE_DEVICES=1           Only device 1 will be seen
CUDA_VISIBLE_DEVICES=0,1         Devices 0 and 1 will be visible
CUDA_VISIBLE_DEVICES=”0,1”       Same as above, quotation marks are optional
CUDA_VISIBLE_DEVICES=0,2,3       Devices 0, 2, 3 will be visible; device 1 is masked

CUDA_VISIBLE_DEVICES=”“          No GPU will be visible

在Python程式碼中指定GPU

import os
os.environ[“CUDA_VISIBLE_DEVICES”] = “0”

設定定量的GPU使用量

config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.9 # 佔用GPU90%的視訊記憶體
session = tf.Session(config=config)

設定最小的GPU使用量

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