Search code examples
pythonrrstudiokerasreticulate

R reticulate package does not see keras library in winpython


I am trying to use keras library installed in Winpython version 3.4.4.amd64 in RStudio (via reticulate and kerasR libraries). I work on windows 7 64bit machine. I also tried the same with Winpython version 3.6.1.amd64

What I have tried in R:

# diagnostics
library(reticulate) 
# reticulate::use_python("PATH_TO_PYTHON") 
path_to_python <- "C:\\dev\\WinPython3.4\\python-3.4.4.amd64"

use_python(path_to_python) 

py_module_available("keras")
[1] FALSE               # it responds with TRUE for eg. matplotlib

py_config()
# python:         C:\dev\WinPython3.4\python-3.4.4.amd64/python.exe
# libpython:      C:/dev/WinPython3.4/python-3.4.4.amd64/python34.dll
# pythonhome:     C:\dev\WINPYT~1.4\PYTHON~1.AMD
# version:        3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC # v.1600 64 bit (AMD64)]
# Architecture:   64bit
# numpy:          C:\dev\WINPYT~1.4\PYTHON~1.AMD\lib\site-packages\numpy
# numpy_version:  1.11.3

When I use Spyder IDE and load keras, it does not throw errors at me. Earlier I took care to modify .keras json file to use theano and not tensorflow.

When I try other libs, eg py_module_available("theano") I get TRUE.

I have no idea what I miss here. I would like to use keras with library(kerasR). I am at work and cannot modify registry and environment paths. Would it be the reason?

When I try

reticulate::import("keras")

I get:

Error in py_module_import(module, convert = convert) : 
  AttributeError: 'NoneType' object has no attribute 'write'

Detailed traceback: 
  File "C:\dev\WINPYT~1.4\PYTHON~1.AMD\lib\site-packages\keras\__init__.py", line 2, in <module>
    from . import backend
  File "C:\dev\WINPYT~1.4\PYTHON~1.AMD\lib\site-packages\keras\backend\__init__.py", line 66, in <module>
    sys.stderr.write('Using TensorFlow backend.\n')

I tried the same for 3.6.1.amd64 with the same result. I am out of ideas, tkanks very much for all suggestions.


Solution

  • Regarding installation of Keras/theano plz check this thread in SO

    How do I install Keras and Theano in Anaconda Python on Windows?

    Once done restart your comp....then lets try to connect kerasR from R studio as given below (sequentially)...........

    ########## Deep learning installation & initiation
      #### First activate  environment where keras/theano were installed via anaconda prompt  :  
    

    (C:\Users\ADMIN\Anaconda3) C:\Users\ADMIN>activate mydeeplearning # mydeeplearning is the my environment name where i installed python keras theano etc.....

    Then From R studio

    try installing the latest github package versions rather than from cran.....

    devtools::install_github("statsmaths/kerasR")
    devtools::install_github("rstudio/reticulate")# install the latest reticulate package as it solves some errors/bugs
    
    library(kerasR)
    library(reticulate)
    
    py_available(initialize = FALSE)  # should give TRUE
    
    use_python("C:/Users/ADMIN/Anaconda3/envs/mydeeplearning/python.exe") # plz change it to your Path
    keras_init()  # will give `successfully loaded keras`
    
    # further checks if everything is working
    keras_available()   # should give `[1] TRUE`
    reticulate::py_config()  # should show u]the correct python path which we tried changing initially  using `use_python("C:/Users/ADMIN/Anaconda3/envs/mydeeplearning/python.exe") `
    
    python:         C:/Users/ADMIN/Anaconda3/envs/mydeeplearning/python.exe
    libpython:      C:/Users/ADMIN/Anaconda3/envs/mydeeplearning/python35.dll
    pythonhome:     C:\Users\ADMIN\ANACON~1\envs\MYDEEP~1
    version:        3.5.3 |Continuum Analytics, Inc.| (default, May 15 2017, 10:43:23) [MSC v.1900 64 bit (AMD64)]
    Architecture:   64bit
    numpy:          C:\Users\ADMIN\ANACON~1\envs\MYDEEP~1\lib\site-packages\numpy
    numpy_version:  1.13.1
    
    python versions found: 
     C:/Users/ADMIN/Anaconda3/envs/mydeeplearning/python.exe
     C:\PROGRA~1\Python35\python.exe
     C:\Users\ADMIN\ANACON~1\python.exe
     C:\PROGRA~1\Python35\\python.exe
     C:\Users\ADMIN\ANACON~1/envs/mydeeplearning/python.exe    
    
    reticulate::py_module_available("keras") #should give `[1] TRUE`   
    reticulate::py_available()#should give `[1] TRUE`   
    reticulate::import("keras.models")# should give `Module(keras.models)`
    

    That should do it....Took me almost entire day to install and figure out the errors....But now feeling elated.....