Keras: FAQ on GPU

Created on 3 Dec 2017  路  5Comments  路  Source: rstudio/keras

Dear all,

I have just installed keras and tensorflow with GPU support in Windows 2012R2 server, following instructions at: https://keras.rstudio.com/reference/install_keras.html
No error during installation, everything is fine after:

install_keras(method = "conda", tensorflow = "gpu")

The issue is that I do not see any performance improvement from R.
From python, if I run a script from command line I can see it uses the GPU device and I can see much better performance.
How can I display what device is keras using from R?
Please add this to the FAQ.

Thanks.
Best regards.

Most helpful comment

GPU configuration on Windows is notoriously finicky :-(

You could try this code to see whether TensorFlow can see your GPU:

library(tensorflow)

with(tf$device("/gpu:0"), {
  const <- tf$constant(42)
})

sess <- tf$Session()
sess$run(const)

All 5 comments

GPU configuration on Windows is notoriously finicky :-(

You could try this code to see whether TensorFlow can see your GPU:

library(tensorflow)

with(tf$device("/gpu:0"), {
  const <- tf$constant(42)
})

sess <- tf$Session()
sess$run(const)

Thnaks.
This basically works.
It took me some hours before I got rid of all the issues.
What I am trying to do is a proof of concept of GPU before buying it.
Unfortunatelly it is embarassing.
On my laptop, an old I3 an LSTM on a tensor of dimension ~8000x8x6 takes 80 seconds.
In AWS EC2 Windows 2012 R2 server with K520 GRID GPU a tensor of dimension ~6500x8x6 it takes 270 seconds.
I do not know what the problem is.
Apparently keras and tensorflow are correctly configured.

Interesting, not at all sure what the problem might be.

Another option is to purchase a cloud desktop instance with a GPU and pay by the hour/second. You can do this on Paperspace (https://www.paperspace.com/)

Ok thanks.
I will try.
I have seen that in internet there is a lot of people unsatisfied of AWS GPU performance.
I didn't know that.

I have just installed an Air machine.
I dont understand why your small script fails.
It seems installing the gpu version:

install_keras(method = "conda", tensorflow = "gpu")
Using r-tensorflow conda environment for TensorFlow installation
Determining latest release of TensorFlow...done
Installing TensorFlow...
Collecting tensorflow-gpu==1.4.0 from https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-1.4.0-cp36-cp36m-win_amd64.whl
Using cached https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-1.4.0-cp36-cp36m-win_amd64.whl
Collecting six>=1.10.0 (from tensorflow-gpu==1.4.0)
Using cached six-1.11.0-py2.py3-none-any.whl
Collecting tensorflow-tensorboard<0.5.0,>=0.4.0rc1 (from tensorflow-gpu==1.4.0)
Using cached tensorflow_tensorboard-0.4.0rc3-py3-none-any.whl
Collecting wheel>=0.26 (from tensorflow-gpu==1.4.0)
Using cached wheel-0.30.0-py2.py3-none-any.whl
Collecting protobuf>=3.3.0 (from tensorflow-gpu==1.4.0)
Using cached protobuf-3.5.0.post1-py2.py3-none-any.whl
Collecting numpy>=1.12.1 (from tensorflow-gpu==1.4.0)
Using cached numpy-1.13.3-cp36-none-win_amd64.whl
Collecting enum34>=1.1.6 (from tensorflow-gpu==1.4.0)
Using cached enum34-1.1.6-py3-none-any.whl
Collecting bleach==1.5.0 (from tensorflow-tensorboard<0.5.0,>=0.4.0rc1->tensorflow-gpu==1.4.0)
Using cached bleach-1.5.0-py2.py3-none-any.whl
Collecting markdown>=2.6.8 (from tensorflow-tensorboard<0.5.0,>=0.4.0rc1->tensorflow-gpu==1.4.0)
Collecting werkzeug>=0.11.10 (from tensorflow-tensorboard<0.5.0,>=0.4.0rc1->tensorflow-gpu==1.4.0)
Using cached Werkzeug-0.12.2-py2.py3-none-any.whl
Collecting html5lib==0.9999999 (from tensorflow-tensorboard<0.5.0,>=0.4.0rc1->tensorflow-gpu==1.4.0)
Collecting setuptools (from protobuf>=3.3.0->tensorflow-gpu==1.4.0)
Using cached setuptools-38.2.4-py2.py3-none-any.whl
Installing collected packages: six, html5lib, bleach, wheel, markdown, setuptools, protobuf, numpy, werkzeug, tensorflow-tensorboard, enum34, tensorflow-gpu
Successfully installed bleach-1.5.0 enum34-1.1.6 html5lib-0.9999999 markdown-2.6.9 numpy-1.13.3 protobuf-3.5.0.post1 setuptools-38.2.4 six-1.11.0 tensorflow-gpu-1.4.0 tensorflow-tensorboard-0.4.0rc3 werkzeug-0.12.2 wheel-0.30.0
Fetching package metadata .............
Solving package specifications: .

All requested packages already installed.

packages in environment at C:\Users\PAPERS~1\ANACON~1\envs\r-tensorflow:

#
h5py 2.7.1 py36h20531f2_0
pillow 4.3.0 py36ha40f157_1
pyyaml 3.12 py36h1d1928f_1
requests 2.18.4 py36h4371aae_1
scipy 1.0.0 py36h1260518_0
Requirement already up-to-date: keras in c:\users\papers~1\anacon~1\envs\r-tensorflow\lib\site-packages
Requirement already up-to-date: six>=1.9.0 in c:\users\papers~1\anacon~1\envs\r-tensorflow\lib\site-packages (from keras)
Requirement already up-to-date: pyyaml in c:\users\papers~1\anacon~1\envs\r-tensorflow\lib\site-packages (from keras)
Requirement already up-to-date: scipy>=0.14 in c:\users\papers~1\anacon~1\envs\r-tensorflow\lib\site-packages (from keras)
Requirement already up-to-date: numpy>=1.9.1 in c:\users\papers~1\anacon~1\envs\r-tensorflow\lib\site-packages (from keras)

Installation complete.

reticulate::use_condaenv("r-tensorflow", required = TRUE)
library(keras)

library(tensorflow)

with(tf$device("/gpu:0"), {

  • const <- tf$constant(42)
  • })

sess <- tf$Session()
sess$run(const)
Error in py_call_impl(callable, dots$args, dots$keywords) :
InvalidArgumentError: Cannot assign a device for operation 'Const': Operation was explicitly assigned to /device:GPU:0 but available devices are [ /job:localhost/replica:0/task:0/device:CPU:0 ]. Make sure the device specification refers to a valid device.
[[Node: Const = Const[dtype=DT_FLOAT, value=Tensor, _device="/device:GPU:0"]()]]

Caused by op 'Const', defined at:
File "C:\Users\PAPERS~1\ANACON~1\envs\R-TENS~1\lib\site-packages\tensorflow\python\framework\constant_op.py", line 214, in constant
name=name).outputs[0]
File "C:\Users\PAPERS~1\ANACON~1\envs\R-TENS~1\lib\site-packages\tensorflow\python\framework\ops.py", line 2956, in create_op
op_def=op_def)
File "C:\Users\PAPERS~1\ANACON~1\envs\R-TENS~1\lib\site-packages\tensorflow\python\framework\ops.py", line 1470, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access

InvalidArgumentError (see above for traceback): Cannot assig

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MaximilianPi picture MaximilianPi  路  5Comments

mattwarkentin picture mattwarkentin  路  3Comments

saanasum picture saanasum  路  4Comments

aliarsalankazmi picture aliarsalankazmi  路  4Comments

vincenhe picture vincenhe  路  7Comments