Tensorflow in R - Make it simple
Deep learning and all kind of machine learning techniques have received a lot of attraction recently. However, most of the commonly used tool for data science and machine learning are implemented in python. As the most favorable programming language of statisticians is R, some might hesitate to stay out of their comfort zone.
TensorFlow first releases in 11/2015 as a machine learning framework which is usability and flexibility. In the github folder of TensorFlow, there are several examples on NPL, computer vision … Off course, Tensorflow is natively supported in python. Recently, Rstudio team develops a library for R which is quite interesting.
In this post, I will explain my understanding on how to implement a simple model and have some fun.
Deep learning
In deep learning, we use multi-layer neural networks. In each nodes, there are functions/operations that transform the input (Tensor) to the output (Tensor). As a graphical model, we might have some interest in the gradient of each nodes and Tensorflow will help you with this stage effortlessly.
In the first step, we have to propose a graph model. It might sound complicated but imagine that you have an equation with constants and variables
\(y = A x + b\)
Then we could describe constants and variables as tensor and operators as nodes. Using Iris data, I assume that x
is iris$Petal.Length
and y
is iris$Petal.Width
.
So, we have created a Coefficient and Intercept as a variable. As all computation in Tensorflow needs to be called in Session
. We obtain the equation using sess$run()
as,
Well, now we have more or less everything we need for a simple linear regression. Remember that in linear regression, we want to minimized the mean square error.
\[MSE = \frac{1}{n} \sum_{i = 1}^n (y_i - \hat{y}_i)^2\]We need an optimizer method and put the MSE as the objective function. Different from the close form linear regression lm
, in the optimization method, we have to loop over the parameter set until we reach the optimal point.
We visualize the final result as,
Conclusion
With this ‘not so deep example’, we have experienced that Tensorflow helps to build graphical model and estimate the parameter with a user defined loss function. In this example, we need to tweak the
GradientDescentOptimizer(learning_rate)
. A large value of learning_rate
might not guarantee convergence while a smaller one might slow down the convergence of the objective function. More advanced examples could be found at Rstudio website