kotakotakota
kotakotakota

Reputation: 782

How to use other GPUs in Keras with a TensorFlow backend?

This is related to How to enable Keras with Theano to utilize multiple GPUs but instead of using multiple GPUs, I'm interested in specifying which GPU the specific model trains or runs on.

My nvidia-smi output looks as follows:

+------------------------------------------------------+                       
| NVIDIA-SMI 361.42     Driver Version: 361.42         |                       
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Tesla K80           Off  | 0000:03:00.0     Off |                    0 |
| N/A   38C    P0    60W / 149W |  11354MiB / 11519MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   1  Tesla K80           Off  | 0000:04:00.0     Off |                    0 |
| N/A   37C    P0    71W / 149W |    224MiB / 11519MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   2  GeForce GTX 750 Ti  Off  | 0000:06:00.0      On |                  N/A |
| 40%   29C    P8     1W /  38W |    120MiB /  2047MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

This output is of course when nothing is running. The issue is I'm not sure in Keras how to specify which GPU to run on. Of course, with TensorFlow we can just do the with tf.device('/cpu:1'): paradigm, but I am not sure how that would integrate with Keras.

Thanks!

Upvotes: 4

Views: 7626

Answers (2)

Thomas Pinetz
Thomas Pinetz

Reputation: 7148

Additionally to specifying tensorflow in your keras.json file as backend, you can limit the number of GPUs used and / or use a specific GPU using the environment variable CUDA_VISIBLE_DEVICES (http://acceleware.com/blog/cudavisibledevices-masking-gpus). Here you can specify which GPU to use.

Upvotes: 4

Pramod Patil
Pramod Patil

Reputation: 827

Basically two steps, you have to follow:

  1. Install tensorflow version which is GPU enable. See this https://www.tensorflow.org/versions/r0.7/get_started/os_setup.html

  2. Keras uses Theano as a backend by default. You need to change it to tensorflow

vi ~/.keras/keras.json

file content: {"epsilon": 1e-07, "floatx": "float32", "backend": "theano"}

change "theano" to "tensorflow"

The thing is that you just need to install gpu enabled tensorflow version. It will automatically use your configured gpu.

See this link for installation procedure of cuda and tensorflowhttp://www.nvidia.com/object/gpu-accelerated-applications-tensorflow-installation.html

Upvotes: 0

Related Questions