rdrr1990 / kerasformula

A high-level interface to keras for R that takes advantage of formulas
51 stars 5 forks source link

creating virtualenv for kerasformula #9

Open rdrr1990 opened 6 years ago

rdrr1990 commented 6 years ago

kerasformula is currently experiencing some compatibility issues with Python 3.x (and/or certain versions of keras). kerasformula has the best track record with Python 2.7 however getting R to use that is not always straightforward if newer versions are installed, e.g. via Conda. I recommend bypassing install_keras, found in the R library keras, in this instance and instead creating a virtualenv where R will look for it. In Terminal:

virtualenv .virtualenvs/r-tensorflow        
# hidden folder where library(keras) will look for keras. 
# Do not use keras::install_keras()

source .virtualenvs/r-tensorflow/bin/activate
pip install utils np_utils   # don’t think others are needed…
pip install tensorflow
pip install keras

Now open R, load the following libraries (installing if need be...)...

library(tensorflow)
library(reticulate)
library(keras)
is_keras_available()        

If that statement's TRUE, you're done. You should be able to use kerasformula as you please. If not,

system(“which python”)
# /opt/rh/python27/root/usr/bin/python

Sys.setenv(TENSORFLOW_PYTHON=”/opt/rh/python27/root/usr/bin/python”)
use_python(“/opt/rh/python27/root/usr/bin/python”)          # do both

py_discover_config("tensorflow")    # should display appropriate paths for TF,  numpy, etc.
py_discover_config("keras")

is_keras_available()  

If TRUE, you're done. if FALSE, the easiest way to see what’s failing seems to be:

reticulate:::import(“keras”)

Lingering issues may of course persist and sometimes installing a slightly older version of keras, keras 2.1.3 in thatvirtualenv can help.