-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFashion_MNIST.R
68 lines (48 loc) · 1.7 KB
/
Fashion_MNIST.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
library(tensorflow)
library(keras)
#Sys.setenv(RETICULATE_PYTHON = "./env/Scripts/python.exe")
library(reticulate)
fashion_mnist <- dataset_fashion_mnist()
c(train_images, train_labels) %<-% fashion_mnist$train
c(test_images, test_labels) %<-% fashion_mnist$test
dim(train_images)
str(train_images)
fobject <- train_images[5,,]
plot(as.raster(fobject, max = 255))
class_names = c('T-shirt', 'Trousers', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot')
class_names[train_labels[5]+1]
# Normalization
train_images <- train_images/255
test_images <- test_images/255
#Validation split
val_indices <- 1:5000
val_images <- train_images[val_indices,,]
part_train_images <- train_images[-val_indices,,]
val_labels <- train_labels[val_indices]
part_train_labels <- train_labels[-val_indices]
# Creating Model
model <- keras_model_sequential()
model %>%
layer_flatten(input_shape = c(28, 28)) %>%
layer_dense(units = 128, activation = 'relu') %>%
layer_dense(units = 10, activation = 'softmax')
model %>% compile(
optimizer = 'sgd',
loss = 'sparse_categorical_crossentropy',
metrics = c('accuracy')
)
#training
model %>% fit(part_train_images, part_train_labels, epochs = 30,
batch_size = 100, validation_data = list(val_images, val_labels))
# Test Performance
score <- model %>% evaluate(test_images,test_labels)
cat('Test loss:', score$loss, "\n")
cat('Test accuracy:', score$accuracy, "\n")
# Predicting on Test set
predictions <- model %>% predict(test_images)
predictions[1, ]
which.max(predictions[1, ])
class_names[which.max(predictions[1, ])]
plot(as.raster(test_images[1, , ], max = 1))
class_pred <- model %>% predict_classes(test_images)
class_pred[1:20]