-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNMIST1
More file actions
57 lines (45 loc) · 1.63 KB
/
NMIST1
File metadata and controls
57 lines (45 loc) · 1.63 KB
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
####Deep Learning with R Chapter 1 ####
####Loading in the MNIST dataset into Keras####
library(keras)
mnist <- dataset_mnist() #Note - there are several other data sets in Keras
train_images <- mnist$train$x
train_labels <- mnist$train$y
test_images <- mnist$test$x
test_labels <- mnist$test$y
#characteristics of our data sets#
dim(train_images)
class(train_images)
typeof(train_images)
#### View the four data sets ####
View(train_images)
View(train_labels)
View(test_images)
View(test_labels)
#### Setting up our first network ####
network <- keras_model_sequential() %>%
layer_dense(units = 512, activation = "relu", input_shape = c(28 * 28)) %>%
layer_dense(units = 10, activation = "softmax")
summary(network) #a look at our network
#### Setting up an optimizer, loss function, and our metrics ####
network %>% compile(
optimizer = "rmsprop",
loss = "categorical_crossentropy",
metrics = c("accuracy")
)
#### Prepare the image data ####
train_images <- array_reshape(train_images, c(60000, 28 * 28))
train_images <- train_images / 255
test_images <- array_reshape(test_images, c(10000, 28 * 28))
test_images <- test_images / 255
#### Preparing the labels ####
train_labels <- to_categorical(train_labels)
test_labels <- to_categorical(test_labels)
#View(train_images[1:10])
#View(test_labels)
#### Train the network ####
network %>% fit(train_images, train_labels, epochs = 5, batch_size = 128) #notice the call to the fit function
#### Evaluate how the network did on the *test* data
metrics <- network %>% evaluate(test_images, test_labels)
metrics
#### How to generate predictions ####
network %>% predict_classes(test_images[1:10,])