Python 如何确保 tensorflow 正在使用 GPU

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51114771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:43:35  来源:igfitidea点击:

How to ensure tensorflow is using the GPU

pythontensorflowkeras

提问by Mihir Deshpande

I installed CUDA v9.2 and corresponding cuDNN manually to install tensorflow gpu But I realized that tensorflow 1.8.0 requires CUDA 9.0 so I ran

我手动安装了 CUDA v9.2 和相应的 cuDNN 来安装 tensorflow gpu 但是我意识到 tensorflow 1.8.0 需要 CUDA 9.0 所以我跑了

pip install tensorflow-gpu

from the anaconda prompt (base environment) where it automatically installed CUDA 9.0 and corresponding cuDNN. I started Spyder from the same command prompt. So here is my code in Python 3.6 where I'm using keras and tensorflow to train using 8000 odd images -

从 anaconda 提示(基本环境)开始,它自动安装了 CUDA 9.0 和相应的 cuDNN。我从同一个命令提示符启动了 Spyder。所以这是我在 Python 3.6 中的代码,其中我使用 keras 和 tensorflow 来训练使用 8000 张奇数图像 -

# Convolutional Neural Networks
# Part 1 - Building the CNN
# Not important

# Part 2- Fitting the CNN to the images - 
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

training_set = train_datagen.flow_from_directory(
        'dataset/training_set',
        target_size=(64, 64),
        batch_size=32,
        class_mode='binary')

test_set = test_datagen.flow_from_directory(
        'dataset/test_set',
        target_size=(64, 64),
        batch_size=32,
        class_mode='binary')
with tf.device("/gpu:0"):   # Notice THIS
    classifier.fit_generator(
            training_set,
            steps_per_epoch=8000,
            epochs=25,
            validation_data=test_set,
            validation_steps=2000)

Notice that right before fitting the dataset at the end, I put it inside

请注意,在最后拟合数据集之前,我将其放入

with tf.device("/gpu:0"):

I think this should ensure that it uses the GPU for training? I'm not sure because changing " gpu:0 " to " cpu:0 " gives the exact same time (18-20 minutes per epoch) for training. How do I ensure that tensorflow in Spyder uses my GPU ?

我认为这应该确保它使用 GPU 进行训练?我不确定,因为将“ gpu:0 ”更改为“ cpu:0 ”可以提供完全相同的训练时间(每个时期 18-20 分钟)。如何确保 Spyder 中的 tensorflow 使用我的 GPU?

I have a NVIDIA GTX 970 so its CUDA compatible. Also I'm using python 3.6 , is that a problem ? Should I create a seperate Python 3.5 environment and install tensorflow-gpu in that similarly and try ?

我有一个 NVIDIA GTX 970,所以它兼容 CUDA。另外我使用的是 python 3.6,这是一个问题吗?我应该创建一个单独的 Python 3.5 环境并在其中安装 tensorflow-gpu 并尝试吗?

回答by dimension

Creates a graph.

创建图表。

 with tf.device('/device:GPU:0'):
    a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
    b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
    c = tf.matmul(a, b)
    # Creates a session with log_device_placement set to True.
    sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
    # Runs the op.
    r = sess.run(c)
    print(r)
    import numpy as np
    assert np.all(r == np.array([[22., 28.], [49., 64.]]))

or go tensorflow website (https://www.tensorflow.org/programmers_guide/using_gpu)

或者去 tensorflow 网站(https://www.tensorflow.org/programmers_guide/using_gpu

import tensorflow as tf
if tf.test.gpu_device_name():
   print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
else:
   print("Please install GPU version of TF")

or this :

或这个 :

from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())