The aim of this tutorial is to show the use of TensorFlow with KERAS for classification and prediction in Time Series Analysis. The latter just implement a Long Short Term Memory (LSTM) model (an instance of a Recurrent Neural Network which avoids the vanishing gradient problem). Introduction The code below has the aim to quick introduce Deep Learning analysis with TensorFlow using the Keras back-end in R environment. Keras is a high-level neural networks API, developed with a focus on enabling fast experimentation and not for final products. Keras and in particular the keras R package allows to perform computations using also the GPU if the installation environment allows for it. Installing KERAS and TensorFlow in Windows … otherwise it will be more simple Install Anaconda: https://www.anaconda.com/download/ Install Rtools(34): https://cran.r-project.org/bin/windows/Rtools/ GPU-TensorFlow To use the option GPU-TensorFlow, you need CUDA Toolkit that matches the version of your GCC compiler: https://developer.nvidia.com/cuda-toolkit If you have Python (i.e. Anaconda) just install.packages("keras") library(keras) install_keras() and this will install the Google Tensorflow module in Python. If you want it working on GPU and you have a suitable CUDA version, you can install it with tensorflow = "gpu" option install_keras(tensorflow = "gpu") Simple check library(keras) to_categorical(0:3) ## [,1] [,2] [,3] [,4] ## [1,] 1 0 0 0 ## [2,] 0 1 0 0 ## [3,] 0 0 1 0 ## [4,] 0 0 0 1 Background on Neural Networks Example old faithful IRIS data Consider the well-known IRIS data set rm(list=ls()) data(iris) plot(iris$Petal.Length, iris$Petal.Width, col = iris$Species) head(iris) ## Sepal.Length Sepal.Width Petal.Length Petal.Width Species ## 1 5.1 3.5 1.4 0.2 setosa ## 2 4.9 3.0 1.4 0.2 setosa ## 3 4.7 3.2 1.3 0.2 setosa ## 4 4.6 3.1 1.5 0.2 setosa ## 5 5.0 3.6 1.4 0.2 setosa ## 6 5.4 3.9 1.7 0.4 setosa We want to build an iris specie classifier based on the observed four iris dimensions. This is the usual classification (prediction) problem so we have to consider a training sample and evaluate the classifier on a test sample. Data in TensorFlow Data are Matrices ```matrix´´´ of doubles. Categorical variables need to be codified in dummies: one hot encoding. onehot.species = to_categorical(as.numeric(iris$Species) - 1) iris = as.matrix(iris[, 1:4]) iris = cbind(iris, onehot.species) Training and Test Data Sets Define training and test set.seed(17) ind