1

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.

Jacek Kotowski
  • 724
  • 12
  • 40
  • I know nothing about these packages, but is *keras* properly installed in your python 3.4. Check in PowerShell: `"C:\path\to\python.exe"` followed by `import keras`. Or do same in IDLE or script. Do you get an error? – Parfait May 19 '17 at 15:47
  • 1
    Last time I checked, Tensorflow was not working on Windows for python-3.4. – stonebig May 19 '17 at 18:23
  • In Spyder ide available with winpython I could import keras and I changed the .python.json to use theano instead of tensorfliw. Maybe when used from r python reads python.json from different location? Is there a way to check it? – Jacek Kotowski May 19 '17 at 19:26

2 Answers2

1

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.....

Nishant
  • 845
  • 8
  • 26
1

Similarily, I had issues when trying to install R "keras" library in Anaconda3 5.0.0 under WIN7.

https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.3.0-cp36-cp36m-win_amd64.whl kept failing to install in Python 3.6 (transport/wheel issues), was looking like problems with python 3.6 compatibility, I did check 64bit architecture though:

>>>import platform
>>>platform.architecture()

( '64bit', 'WindowsPE')

Downgraded PYTHON from 3.6 to 3.5 in conda environment r-tensorflow and in R used reticulate to point that environment: use_condaenv("r-tensorflow")

https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.3.0-cp35-cp35m-win_amd64.whl version wouldnt install properly neither, had to use tensorflow-1.2.1-cp35-cp35m-win_amd64.whl in Python 3.5, and that worked

Problem is, install_keras() seems to invoke install_tensorflow(method = "conda") which actually overrides and uses pip to reinstall tensorflow:

activate r-tensorflow && pip install --upgrade --ignore-installed "https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.3.0-cp35-cp35m-win_amd64.whl"

My guess is it is better t use "kerasR" instead of "keras" library then trying to enforce tensorflow-1.2.1-py35. If you find a workaround though, please post.

headless
  • 11
  • 1