Site icon R-bloggers

Loading Python’s Keras into R, Part 1

[This article was first published on R – FinEx.Co, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Late last year, Matt Dancho had a post on deep learning celebrating the arrival of the Python keras package for R. It is a very good tutorial on using artificial neural networks (ANN) to solve complicated business problems, well worth checking out.

Took More Doing Than I Thought

I started working with neural networks over a decade ago with Palisade Decision Tree software, which includes NeuralTools, a neural network add-in for Excel. It’s a quality program that works well, but it is subject to constraints imposed by Excel. So I looked forward to playing around with keras and getting a sense of how R works with neural nets.

What I didn’t know is that in order to use keras in R it is necessary to have the keras Python library loaded and ready to go. This took more doing than I thought it would.

Of course, R has native neural network and deep learning packages, such as nnet and RSNNS, among others. But the idea of R joining forces with Python to implement a keras package is a welcome addition and one I wanted to try. I went through the R-Studio cheat sheet on keras and decided to make a go.

Straight to GTS Mode

Things went smoothly until I got to actually building and running the keras model. I was immediately faced with a long list of warnings followed by the failure of the model to run. I ran the code a couple more times to see if I could figure out what was going on. Each time, the same warnings popped up.

In looking closely at the warnings I finally noticed, buried among them towards the bottom, this error message:

ModuleNotFoundError: No module named 'keras'

I checked to make sure the keras library was loaded in my environment and running. It was. A lesson from a long ago data science class came to mind and I went straight to GTS mode. All I could find were references to keras in Python. There was nothing about this error message in R.

GitHub was the most help. There I found a thread on “No module named keras: #4889”. But it was short and was closed down due to lack of use in late 2017.

That thread contained a few snippets of Python code that helped me figure out the problem. For keras to run in R you need to have keras loaded in Python. Which means you need to have Anaconda Prompt or JupyterLab loaded in your system, as well as R.

Lesson Learned

This was news to me. It’s not mentioned in the keras cheat sheet or in Matt’s blog post.

In fact, the keras cheat sheet mentions in the “Installation” section that “the keras R package uses the Python keras library. You can install all the prerequisites directly from R.”

That wasn’t the case for me. There’s a note that says “See ?keras_install for GPU instructions,” but when I run the command I get “No results found.”

I guess it is common knowledge, but somehow I did not get the memo. Many others are probably unaware. Hence, this post.

The lesson here is read the documentation. Keras in R is the interface to Python’s keras. No Python, no keras in R.

More on this is the next post.

To leave a comment for the author, please follow the link and comment on their blog: R – FinEx.Co.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.