17.4 TensorBoard and tfruns in R

TensorBoard in R

# example from https://keras.rstudio.com/articles/examples/mnist_mlp.html

library(keras)
# Define hyperparameters -----------

batch_size <- 128
num_classes <- 10
epochs <- 30
# Data Preparation -----------

# The data, shuffled and split between train and test sets
c(c(x_train, y_train), c(x_test, y_test)) %<-% dataset_mnist()

x_train <- array_reshape(x_train, c(nrow(x_train), 784))
x_test <- array_reshape(x_test, c(nrow(x_test), 784))

# Transform RGB values into [0,1] range
x_train <- x_train / 255
x_test <- x_test / 255

cat(nrow(x_train), 'train samples\n')
cat(nrow(x_test), 'test samples\n')

# Convert class vectors to binary class matrices
y_train <- to_categorical(y_train, num_classes)
y_test <- to_categorical(y_test, num_classes)
60000 train samples
10000 test samples
# Define Model ---------------

model <- keras_model_sequential()
model %>% 
  layer_dense(units = 256, activation = 'relu', input_shape = c(784)) %>% 
  layer_dropout(rate = 0.4) %>% 
  layer_dense(units = 128, activation = 'relu') %>%
  layer_dropout(rate = 0.3) %>%
  layer_dense(units = 10, activation = 'softmax')

summary(model)

model %>% compile(
  loss = 'categorical_crossentropy',
  optimizer = optimizer_rmsprop(),
  metrics = c('accuracy')
)
________________________________________________________________________________
Layer (type)                        Output Shape                    Param #     
================================================================================
dense_1 (Dense)                     (None, 256)                     200960      
________________________________________________________________________________
dropout_1 (Dropout)                 (None, 256)                     0           
________________________________________________________________________________
dense_2 (Dense)                     (None, 128)                     32896       
________________________________________________________________________________
dropout_2 (Dropout)                 (None, 128)                     0           
________________________________________________________________________________
dense_3 (Dense)                     (None, 10)                      1290        
================================================================================
Total params: 235,146
Trainable params: 235,146
Non-trainable params: 0
________________________________________________________________________________
# Training the model --------
tensorboard("logs")

history <- model %>% fit(
  x_train, y_train,
  batch_size = batch_size,
  epochs = epochs,
  verbose = 1,
  validation_split = 0.2,
  callbacks = callback_tensorboard("logs")
)
Started TensorBoard at http://127.0.0.1:4826 

tfruns in R

# run these in the R Studio for seeing the visualization window
library(tfruns)
training_run("ch-17_mnist_mlp.R")
Using run directory runs/2017-12-23T04-59-04Z



> library(keras)

> FLAGS <- flags(flag_numeric("dropout1", 0.4), flag_numeric("dropout2", 
+     0.3))

> mnist <- dataset_mnist()

> x_train <- mnist$train$x

> y_train <- mnist$train$y

> x_test <- mnist$test$x

> y_test <- mnist$test$y

> dim(x_train) <- c(nrow(x_train), 784)

> dim(x_test) <- c(nrow(x_test), 784)

> x_train <- x_train/255

> x_test <- x_test/255

> y_train <- to_categorical(y_train, 10)

> y_test <- to_categorical(y_test, 10)

> model <- keras_model_sequential()

> model %>% layer_dense(units = 256, activation = "relu", 
+     input_shape = c(784)) %>% layer_dropout(rate = FLAGS$dropout1) %>% 
+     layer_dense .... [TRUNCATED] 

> model %>% compile(loss = "categorical_crossentropy", 
+     optimizer = optimizer_rmsprop(lr = 0.001), metrics = c("accuracy"))

> history <- model %>% fit(x_train, y_train, batch_size = 128, 
+     epochs = 20, verbose = 1, validation_split = 0.2)

> plot(history)

> score <- model %>% evaluate(x_test, y_test, verbose = 0)

> cat("Test loss:", score$loss, "\n")
Test loss: 0.09684165 

> cat("Test accuracy:", score$acc, "\n")
Test accuracy: 0.9808 



Run completed: runs/2017-12-23T04-59-04Z

results matching ""

    No results matching ""