完美解决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.

作者:肆不肆傻
链接:https://www.jianshu.com/p/0816c3a5fa5c

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版本

如果同时调用加速运算的话这样

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海宝7号

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值