Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make keras in R use the tensorflow installed by Python

I have already performed the tensor flow installation with the following command:

pip install --ignore-installed https://github.com/mind/wheels/releases/download/tf1.5-gpu-cuda91-nomkl/tensorflow-1.5.0-cp27-cp27mu-linux_x86_64.whl

This is the latest tensorflow wheel catered for CUDA 9.1. (3x faster than CUDA 8.0)

And I can call it successfully in my python code.

How can I make the keras in R to call the tensorflow installed by python above? The reason I asked that because I the default installation method

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

It failed to recognize the cuda-9.1 library.

> conv_base <- keras::application_vgg16(
+   weights = "imagenet",
+   include_top = FALSE,
+   input_shape = c(150, 150, 3)
+ )
/home/ubuntu/anaconda2/envs/r-tensorflow/lib/python2.7/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.
Error: ImportError: Traceback (most recent call last):
  File "/home/ubuntu/anaconda2/envs/r-tensorflow/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "/home/ubuntu/anaconda2/envs/r-tensorflow/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "/home/ubuntu/anaconda2/envs/r-tensorflow/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory

This is because R installation method calling for tensorflow version 1.5.0 that is still not catered for CUDA 9.1.

like image 358
neversaint Avatar asked Feb 01 '18 04:02

neversaint


People also ask

How do I run keras and TensorFlow in Rstudio?

Run pip install tensorflow and pip install keras to install both of these libraries in python. You can test the TensorFlow installation by running import tensorflow as tf from python.

Can I use TensorFlow in R?

TensorFlow is a state-of-the-art machine learning framework that specializes in the ability to develop deep learning neural networks. And now, it's available in R!

Is keras automatically installed with TensorFlow?

When you install TensorFlow 2.0+, Keras will be automatically installed, as well. The simplest way to install TensorFlow is to install the binary version using one of the official releases on the Python Package Index (PyPI).


1 Answers

Try to put this in your .bashrc:

export KERAS_BACKEND='tensorflow'

Or based on this instruction, you can do:

tensorflow::install_tensorflow(version = "https://github.com/mind/wheels/releases/download/tf1.5-gpu-cuda91-nomkl/tensorflow-1.5.0-cp27-cp27mu-linux_x86_64.whl")

Then keras will automatically identify the correct tensorflow

like image 184
pdubois Avatar answered Sep 19 '22 15:09

pdubois