Check whether Tensorflow is running on GPU or CPU

every blog every motto: You never know how strong you really are until being strong is the only choice you have。

0. Preface

Check whether Tensorflow2 uses GPU or CPU summary

1. Text

Note: As long as the GPU driver and tools such as cuda and cudnn are configured, the program execution will use the GPU first by default, and then use the CPU if it cannot be executed.

1.1 tensorflow 1.x

View log information, if it contains GPU information, it means using GPU

import tensorflow as tf
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))

1.2 tensorflow 2.x

View log information, if it contains GPU information, it means using GPU

import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(log_device_placement=True))

Insert picture description here

references

[1] https://blog.csdn.net/weixin_42702666/article/details/88067012
[2] https://zhuanlan.zhihu.com/p/110151684
[3] https://blog.csdn.net/qi_yue_yu/article/details/103549345?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase

Guess you like

Origin blog.csdn.net/weixin_39190382/article/details/107286225
Recommended