简体   繁体   中英

How to run Keras on GPU?

I am trying to run my ML task on a remote server with GPU. I typed

nvidia-smi

and I was sure that the device has one GPU.

在此处输入图像描述

I am using Keras to write my ML task. And I intend to run my task on one GPU. But I just can't get the program to run on GPU. I've checked running processes and my task was not one of them.

I've tried to print out available devices using multiple methods in my code, but they did not show that I have a GPU.

from tensorflow.python.client import device_lib

print(device_lib.list_local_devices())

This gave me:

[name: "/device:CPU:0" device_type: "CPU" memory_limit: 268435456 locality { }....

but no "/device:GPU:0" is shown

And then I tried:

print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))

This gave me:

Num GPUs Available: 0

Also,

print("GPUs: ", tf.config.experimental.list_physical_devices('GPU'))

This gave me:

GPUs: []

And,

 with tf.device("gpu:0"): print('-------------------------------------------') print("tf.keras code in this scope will run on GPU") print('-------------------------------------------')

This gave me:

 ------------------------------------------- tf.keras code in this scope will run on GPU -------------------------------------------

Apparently, my code was not run on the GPU. Do not know why it said my Keras code run on GPU.

I've searched for a lot of information, but I still failed. I would like to know how to run my Keras code on GPU, and also what happened to the above situation. Thanks in advance!

After installing CUDA

nvcc -V

If you use the command above, it says to install cuda-toolkit

The problem is caused by not being able to find the route because it was not specified.

gedit ~/.bashrc

Add the top two sentences. However, please press different versions depending on the cuda version

export PATH=/usr/local/cuda-11.1/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-11.1/lib64:$LD_LIBRARY_PATH

Then enter the command below to recognize the CUDA in the installation path and print out the current version.

source ~/.bashrc

nvcc-V

If you finished euqal to this picture

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM