Advanced Machine Learning with R
上QQ阅读APP看书,第一时间看更新

Keras and TensorFlow background

I mentioned earlier that Keras is an API, a frontend if you will, for several deep learning backends. It was originally available only for Python but has been available in R since, mid-2017. It is important to spend some time reviewing its capabilities at its documentation source: https://keras.io/why-use-keras/.

I must confess my colleagues brought me into Keras and using TensorFlow kicking and screaming. If I can get this to work, I would say that you certainly can. I must thank them, as it is very powerful, even though I have a slight bias toward MXNet. As the old saying goes, it is tough to teach old dogs new tricks!

The backend of choice, of course, is TensorFlow. We must now take a few sentences to put in plain English what a tensor is, and what TensorFlow is. A tensor is an n-dimensional array. Thus, a vector is a one-dimensional tensor, a matrix is a two-dimensional tensor and so forth. Let's say you have a multivariate time series, which would consist of a three-dimensional tensor: one dimension is the observations or length of your data, another dimension is the features, and another the timesteps or, more concretely, the lagged values. TensorFlow as a backend is an open source platform, created by Google, that uses tensors, of course, for high-performance and scalable computation. The base frontend for TensorFlow is Python. Therefore, to use Keras and R as the frontend, you must install Python, in particular the Python platform Anaconda: https://www.anaconda.com/.

So, if installing Anaconda becomes an issue, then the following exercise will require you to use a different backend, which is outside the scope of this discussion. Let's look at how to get this up and running. Keep in mind that I'm using a Windows-based computer:

As a caveat, it is important to inform you that I in no way guarantee that the following code will work for you as I exactly lay it out. I've installed this on several computers, and each time I encountered different problems that required me to search the internet for a specific solution. The one common factor is that you have installed Anaconda on your computer and that it is fully functional.
# Install reticulate package as it allows R to call python
> install.packages("reticulate")

> install.packages("keras")

> keras::install_keras() # loads the necessary python packages and may take some time to complete

> library(keras)

> library(reticulate)

That was the code that worked for me on this laptop. Again, your results may vary. Also, note that I've run into a number of issues that required me to run install_keras() again to get it working properly.

Assuming all is well, let's get our data loaded.