Keras: Installation with both CPU/GPU tensorflow modules

Created on 15 Jun 2017  路  12Comments  路  Source: keras-team/keras

It is said in the document that keras will automatically run on GPU with tensorflow backend.

However, I found Keras depends on the installation order of tensorflow modules. It seems that keras always use the module installed last. For example,

pip install tensorflow-gpu tensorflow

Keras will use CPU under this circumenstance.

tensorflow

Most helpful comment

Is there any way to get the above solution to work from a tensorflow-gpu container? TF fails to import using this container on a non-gpu machine. It'd be nice to be able to use the same docker image for both machine types.

All 12 comments

I have experimented with this a bit on my machine as well and it seems to me that the default behaviour working directly with tensorflow==1.1.0 is following:

If I perform the installation in the following order:

sudo pip install tensorflow
sudo pip install tensorflow-gpu

then GPU version is being used
But if I install it in the opposite order:

sudo pip install tensorflow-gpu
sudo pip install tensorflow

then CPU version is being used.

I have performed the experiment not using keras, just importing tensorflow directly (the same way keras does it in the tensorflow backend module). And I have installed it directly to the root python 2.7. on Ubuntu 16.04.2.

I have performed the experiment not using keras, just importing tensorflow directly (the same way keras does it in the tensorflow backend module).

Yep. It seems the bug is caused by the new tensorflow. Maybe the document only tested on tensorflow 0.x.

It seems using below code does not force Keras 2.0 to use CPU any more. Any comment?
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152
os.environ["CUDA_VISIBLE_DEVICES"] = ""

As a new user of Keras, I was curious about how could I know which tensorflow is uesd in Keras?

@lxbcd94
If you use the default tensorflow and CUDA settings, the program should print some lines about GPU info when using tensorflow-gpu. Although it is clumsy, it works in all cases for me.

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.

Was there a fix for this?

This question has been answered multiple times before. You can force tensorflow-gpu ins keras to use cpu with

disable GPU

import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152
os.environ['CUDA_VISIBLE_DEVICES'] = '-1' # -1 !!!!

This question has been answered multiple times before. You can force tensorflow-gpu ins keras to use cpu with

disable GPU

import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152
os.environ['CUDA_VISIBLE_DEVICES'] = '-1' # -1 !!!!

It works. One can easily switch CPU or GPU by setting the above env settings when running a programme.

Closing this issue since its solved. Feel free to reopen if it still persists.

Is there any way to get the above solution to work from a tensorflow-gpu container? TF fails to import using this container on a non-gpu machine. It'd be nice to be able to use the same docker image for both machine types.

useful, mark

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kylemcdonald picture kylemcdonald  路  3Comments

anjishnu picture anjishnu  路  3Comments

LuCeHe picture LuCeHe  路  3Comments

fredtcaroli picture fredtcaroli  路  3Comments

braingineer picture braingineer  路  3Comments