-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnist_example.R
72 lines (56 loc) · 1.94 KB
/
mnist_example.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#FILE: mnist_example.R
#AUTHOR: Karsten Suhre
#DATE: 3 Jan 2019
#PURPOSE: evaluate usage of R with tensorflow and keras to do AI
#MODIF:
# inspired by https://tensorflow.rstudio.com
library(keras)
rm(list=ls())
print ( tensorflow::tf_gpu_configured() )
# test example from https://tensorflow.rstudio.com/keras/
mnist <- dataset_mnist()
x_train <- mnist$train$x
y_train <- mnist$train$y
x_test <- mnist$test$x
y_test <- mnist$test$y
# reshape
x_train <- array_reshape(x_train, c(nrow(x_train), 784))
x_test <- array_reshape(x_test, c(nrow(x_test), 784))
# rescale
x_train <- x_train / 255
x_test <- x_test / 255
y_train <- to_categorical(y_train, 10)
y_test <- to_categorical(y_test, 10)
# Defining the Model
model <- keras_model_sequential()
# We begin by creating a sequential model and then adding layers using the pipe (%>%) operator
model %>%
layer_dense(units = 1024, activation = 'relu', input_shape = c(784)) %>%
layer_dropout(rate = 0.4) %>%
layer_dense(units = 256, activation = 'relu', input_shape = c(784)) %>%
layer_dropout(rate = 0.3) %>%
layer_dense(units = 64, activation = 'relu') %>%
layer_dropout(rate = 0.2) %>%
layer_dense(units = 10, activation = 'softmax')
# model %>%
# layer_dense(units = 512, activation = 'relu', input_shape = c(784)) %>%
# layer_dense(units = 10, activation = 'softmax')
# Next, compile the model with appropriate loss function, optimizer, and metrics:
model %>% compile(
loss = 'categorical_crossentropy',
optimizer = optimizer_rmsprop(),
metrics = c('accuracy')
)
# print the model config
print(summary(model))
# Use the fit() function to train the model for N epochs using batches of batch_size images
history <- model %>% fit(
epochs = 10, batch_size = 256,
#callbacks = callback_tensorboard("logs/run_a"),
validation_split = 0.2,
x_train, y_train
)
plot(history)
# Evaluate the model's performance on the test data:
eval = model %>% evaluate(x_test, y_test)
print(eval)