Tensorflow GPU使用的一些记录

1.在有支持GPU的电脑上安装tensorflow只需要安装tensorflow GPU版本即可,
GPU版本会包含CPU。
可通过tf.test.is_gpu_available()函数看GPU能否使用:

# Hyperparams if GPU is available
if tf.test.is_gpu_available():
    print('GPU is OK')
    # GPU
    BATCH_SIZE = 16  # Number of images used in each iteration
    EPOCHS = 3  # Number of passes through entire dataset
    
# Hyperparams for CPU training
else:
    # CPU
    print('No GPU')
    BATCH_SIZE = 4
    EPOCHS = 1

2.在GPU版本使用CPU时可以通过如下code指定使用CPU

# 该段代码是在GPU版本的tensorflow中使用CPU
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"

3.运行GPU版本tensorflow 时,有时候会出现
Cudnn fail 的问题
(1)可能时tensorflow gpu版本过高,建议降低版本
(2)设置GPU资源按需增加:

# 该段代码是设置GPU资源按需增加
config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))
sess = tf.Session(config=config)

或者
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)
发布了29 篇原创文章 · 获赞 12 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/c2250645962/article/details/101115815