Until now, we have only created tensor objects and added them to a TensorFlow graph for later execution. In this recipe, we will learn how to create a TensorFlow session that can be used to execute (or run) the TensorFlow graph.
Before we run the graph, we should have TensorFlow installed and loaded in R. The installation details can be found in Chapter 1, Getting Started.
- Load the
tensorflow
library and import thenumpy
package:
library(tensorflow) np <- import("numpy")
- Reset or remove any existing
default_graph
:
tf$reset_default_graph()
- Start an
InteractiveSession
:
sess <- tf$InteractiveSession()
- Initialize the
global_variables
:
sess$run(tf$global_variables_initializer())
- Run iterations to perform optimization (
training
):
# Train the model train_batch_size = 128L for (i in 1:100) { spls <- sample(1:dim(train_data$images)[1],train_batch_size) if (i %% 10 == 0) { train_accuracy <- accuracy$eval(feed_dict = dict( x ...