Keras: Can keras model run on specific device?

Created on 5 Dec 2016  路  6Comments  路  Source: keras-team/keras

Problem configuration

  • using tensorflow as backed.
  • computer with 1GPU card and 12 CPUs
  • not distributed learning over cluster
  • with only one session, use GPU or use CPUs. Not using both of them at any time.

Way to force keras calling tensorflow in GPU or CPUs

run keras in CPU

with tf.device(/cpu:0)

run keras in GPU

I don't write anything, let everything be as default. So since I install tensorflow in GPU version. It should assume default using GPU.
with tf.device(/gpu:0)

This undocumented trick works for me so far. However, since keras is a blackbox to me, while tensorflow is more structured and clear, I feel there should be an improvement for keras to better control the CPU/GPU device with in keras. I know we could just use keras as simplified tensorflow layer constructor. Thus it is possible to run everything under framework of tensorflow rather than living in the world of keras.

Comment:

If you have alternative ways to force keras be used in CPU or GPU, please comment below and let everyone else know.

Best,
Shaowu

stale

Most helpful comment

Put

import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"   # see issue #152
os.environ["CUDA_VISIBLE_DEVICES"] = ""

before tensorflow is imported.

See also: http://stackoverflow.com/a/42750563/562769

All 6 comments

You can also set the environment variable: CUDA_VISIBLE_DEVICES to limit the number of gpus used. http://www.acceleware.com/blog/cudavisibledevices-masking-gpus

We use a task scheduler for our CUDA Servers and in the schell script we use this variable like this:
export CUDA_VISIBLE_DEVICES=$(getFreeGPU)

Put

import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"   # see issue #152
os.environ["CUDA_VISIBLE_DEVICES"] = ""

before tensorflow is imported.

See also: http://stackoverflow.com/a/42750563/562769

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.

import os
os.environ["CUDA_VISIBLE_DEVICES"] = '-1' works on windows 10 to force it to use CPU

work perfect for me (windows 10 and keras 2.2.4)
Thank !

work perfect for me (windows 10 and keras 2.2.4)
Thank !

when I did this in keras (with the backend of tensorflow in window 10), the program will occupy all the memories of GPUs, but it only runs on one GPU. Anyone has meet this problem before?

Was this page helpful?
0 / 5 - 0 ratings