How to do it...

  1. Load the tensorflow library and import the numpy package:
library(tensorflow)
np <- import("numpy")
  1. Reset or remove any existing default_graph:
tf$reset_default_graph()
  1. Start an InteractiveSession:
sess <- tf$InteractiveSession()
  1. Initialize the global_variables:
sess$run(tf$global_variables_initializer())
  1. 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 = train_data$images[spls,], y_true = train_data$labels[spls,], keep_prob = 1.0))
cat(sprintf("step %d, training accuracy %g ", i, train_accuracy))
}
optimizer$run(feed_dict = dict(
x = train_data$images[spls,], y_true = train_data$labels[spls,], keep_prob = 0.5))
}
  1. Evaluate the performance of the trained model on test data:
# Test the model
test_accuracy <- accuracy$eval(feed_dict = dict(
x = test_data$images, y_true = test_data$labels, keep_prob = 1.0))
cat(sprintf("test accuracy %g", test_accuracy))
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset