Home > Software engineering >  How to use GPU with TensorFlow?
How to use GPU with TensorFlow?

Time:12-18

My PC

enter image description here

Microsoft Windows [Version 10.0.22621.963]
(c) Microsoft Corporation. All rights reserved.

C:\Users\donhu>nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Tue_May__3_19:00:59_Pacific_Daylight_Time_2022
Cuda compilation tools, release 11.7, V11.7.64
Build cuda_11.7.r11.7/compiler.31294372_0

C:\Users\donhu>nvidia-smi
Sat Dec 17 23:40:44 2022
 ----------------------------------------------------------------------------- 
| NVIDIA-SMI 512.77       Driver Version: 512.77       CUDA Version: 11.6     |
|------------------------------- ---------------------- ---------------------- 
| GPU  Name            TCC/WDDM | Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|=============================== ====================== ======================|
|   0  NVIDIA GeForce ... WDDM  | 00000000:01:00.0  On |                  N/A |
| 34%   31C    P8    16W / 125W |   1377MiB /  6144MiB |      4%      Default |
|                               |                      |                  N/A |
 ------------------------------- ---------------------- ---------------------- 

 ----------------------------------------------------------------------------- 
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A      3392    C G   C:\Windows\explorer.exe         N/A      |
|    0   N/A  N/A      4484    C G   ...artMenuExperienceHost.exe    N/A      |
|    0   N/A  N/A      6424    C G   ...n1h2txyewy\SearchHost.exe    N/A      |
|    0   N/A  N/A      6796    C G   ...lPanel\SystemSettings.exe    N/A      |
|    0   N/A  N/A      7612    C G   ...8bbwe\WindowsTerminal.exe    N/A      |
|    0   N/A  N/A      9700    C G   ...8bbwe\WindowsTerminal.exe    N/A      |
|    0   N/A  N/A     10624    C G   ...perience\NVIDIA Share.exe    N/A      |
|    0   N/A  N/A     10728    C G   ...er Java\jre\bin\javaw.exe    N/A      |
|    0   N/A  N/A     13064    C G   ...8bbwe\WindowsTerminal.exe    N/A      |
|    0   N/A  N/A     14496    C G   ...462.46\msedgewebview2.exe    N/A      |
|    0   N/A  N/A     17124    C G   ...ooting 2\BugShooting2.exe    N/A      |
|    0   N/A  N/A     19064    C G   ...8bbwe\Notepad\Notepad.exe    N/A      |
|    0   N/A  N/A     19352    C G   ...8bbwe\WindowsTerminal.exe    N/A      |
|    0   N/A  N/A     20920    C G   ...y\ShellExperienceHost.exe    N/A      |
|    0   N/A  N/A     21320    C G   ...e\PhoneExperienceHost.exe    N/A      |
|    0   N/A  N/A     21368    C G   ...me\Application\chrome.exe    N/A      |
 ----------------------------------------------------------------------------- 

C:\Users\donhu>

I train model

from tensorflow import keras
from tensorflow.keras import layers

def get_model():
    model = keras.Sequential([
        layers.Dense(512, activation="relu"),
        layers.Dense(10, activation="softmax")
    ])
    model.compile(optimizer="rmsprop",
                  loss="sparse_categorical_crossentropy",
                  metrics=["accuracy"])
    return model

model = get_model()
history_noise = model.fit(
    train_images_with_noise_channels, train_labels,
    epochs=10,
    batch_size=128,
    validation_split=0.2)

model = get_model()
history_zeros = model.fit(
    train_images_with_zeros_channels, train_labels,
    epochs=10,
    batch_size=128,
    validation_split=0.2)

source code enter image description here

How to use GPU with TensorFlow?

CodePudding user response:

As screenshot, you are using Anaconda. Need install

cudatoolkit
cudnn

then

tf.debugging.set_log_device_placement(True)

CodePudding user response:

Using GPU should be automatical for the Tensorflow, it seems that you are missing some of the required components (citing the Tensorflow web page):

The following NVIDIA® software are only required for GPU support.

NVIDIA® GPU drivers version 450.80.02 or higher.
CUDA® Toolkit 11.2.
cuDNN SDK 8.1.0.
(Optional) TensorRT to improve latency and throughput for inference.

See their complete list here: https://www.tensorflow.org/install/pip#software_requirements

After installing all of these, the Tensorflow should work fine and display that it found capable GPU device. Also note, when downloading the packages, you need mutually matching versions of CUDA and cuDNN.

  • Related