TF core API in R
We learned about the TensorFlow core API in Chapter 1. In R, this API is implemented with the tensorflow
R package.
As an example, we provide a walkthrough of the MLP Model for classifying handwritten digits from the MNIST dataset at the following link: https://tensorflow.rstudio.com/tensorflow/articles/examples/mnist_softmax.html.
Note
You can follow along with the code in the Jupyter R notebook ch-17a_TFCore_in_R
.
- First, load the library:
library(tensorflow)
- Define the hyper-parameters:
batch_size <- 128 num_classes <- 10 steps <- 1000
- Prepare the data:
datasets <- tf$contrib$learn$datasets mnist <- datasets$mnist$read_data_sets("MNIST-data", one_hot = TRUE)
The data is loaded from the TensorFlow dataset library and is already normalized to fall into the [0,1] range.
- Define the model:
# Create the model x <- tf$placeholder(tf$float32, shape(NULL, 784L)) W <- tf$Variable(tf$zeros(shape(784L, num_classes))) b <- tf$Variable(tf$zeros(shape(num_classes))) y <...