Some records Tensorflow GPU used

1. Install tensorflow on a computer that supports GPU only tensorflow GPU version can be installed,
GPU version will contain the CPU.
By tf.test.is_gpu_available () function is used to see whether the 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. When CPU GPU version code can be specified by using the CPU

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

3. Run GPU version tensorflow, sometimes there will be
problems Cudnn fail in
(1) may be too high when tensorflow gpu version, we recommend decreasing version
(2) is set to increase demand GPU resources:

# 该段代码是设置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)
Published 29 original articles · won praise 12 · views 10000 +

Guess you like

Origin blog.csdn.net/c2250645962/article/details/101115815