完美解决tensorflow 使用CPU与GPU相关问题

查看 tensorflow 版本
conda list
例如发现 tensorflow 1.10.0
tensorflow-gpu 1.10.0
当两个版本相同时,默认会使用 cpu 版本
如果同时存在 cpu 和 gpu 版本的 tensorflow,系统默认使用版本高的 tensorflow
如果想要使用 gpu 版本的 tensorflow,只需要将 gpu 版本升级比 cpu 高,即可解决

在运行之前先查看GPU的使用情况:
指令:nvidia-smi 备注:查看GPU此时的使用情况
或者
指令:watch nvidia-smi 备注:实时返回GPU使用情况

两种限定GPU占用量的方法:
方法一、设置定量的GPU显存使用量:
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.4 # 占用GPU40%的显存
session = tf.Session(config=config)
方法二、设置最小的GPU显存使用量,动态申请显存:(建议)
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config=config)
参考来源:https://www.cnblogs.com/wandaoyi/p/11544109.html

指定GPU训练:
方法一、在python程序中设置:
代码:os.environ[‘CUDA_VISIBLE_DEVICES’] = ‘0’ 备注:使用 GPU 0
代码:os.environ[‘CUDA_VISIBLE_DEVICES’] = ‘0,1’ 备注:使用 GPU 0,1
方法二、在执行python程序时候:
指令:CUDA_VISIBLE_DEVICES=2 python yourcode.py
指令:CUDA_VISIBLE_DEVICES=0,1 python yourcode.py
备注:’=’的左右不允许有空格

实际使用案例

'''版本问题123
import tensorflow as tf
import os
os.environ["CUDA_VISIBLE_DEVICES"]="0"
tf.compat.v1.disable_eager_execution()
hello=tf.constant('Hello,TensorFlow')
config=tf.compat.v1.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.6
占用GPU显存0.6,,建议使用GPU最小显存使用量
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config=config)

sess=tf.compat.v1.Session(config=config)
print(sess.run(hello))
'''

要执行的Python文件首加上下面的代码即可,

import os
os.environ[“CUDA_VISIBLE_DEVICES”]=”-1″

来自 Acceleware 的说明:
If you are writing GPU enabled code, you would typically use a device query to select the desired GPUs. However, a quick and easy solution for testing is to use the environment variable CUDA_VISIBLE_DEVICES to restrict the devices that your CUDA application sees. This can be useful if you are attempting to share resources on a node or you want your GPU enabled executable to target a specific GPU.

Environment Variable Syntax Results
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 will enumerate the visible devices starting at zero. In the last case, devices 0, 2, 3 will appear as devices 0, 1, 2. If you change the order of the string to “2,3,0”, devices 2,3,0 will be enumerated as 0,1,2 respectively. If CUDA_VISIBLE_DEVICES is set to a device that does not exist, all devices will be masked. You can specify a mix of valid and invalid device numbers. All devices before the invalid value will be enumerated, while all devices after the invalid value will be masked.

export CUDA_VISIBLE_DEVICES=0,1,2,3
注意bash脚本中不要随意使用空格,特别是符号之后不要加空格
source ~/anaconda3/bin/activate torch
使用source命令读取并且执行activate,激活anaconda虚拟环境
python -c 'import torch; print(torch.__version__); print(torch.cuda.device_count())'
用python command模式显示pytorch版本

如果同时调用加速操作,则

[En]

If the accelerated operation is called at the same time, so

import torch
import os
os.environ['CUDA_VISIBLE_DEVICES']='0,1,2'
print(torch.cuda.device_count())

Original: https://blog.csdn.net/dongbao520/article/details/120114280
Author: 海宝7号
Title: 完美解决tensorflow 使用CPU与GPU相关问题

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/509624/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球