devtools::install_github(“rstudio/keras”) library(keras) install_keras()
Once you have done that, we can attempt to attack the MNIST dataset.
library(keras)
train=read.csv("d:/mnist/train.csv")
train.x=train[,-1]
colnames(train.x)=NULL
for (i in 1:784){train.x[,i]=as.numeric(train.x[,i])}
train.x=as.matrix(train.x)
train.y=as.matrix(train[,1])
test=read.csv("d:/mnist/test.csv")
test=as.matrix(test)
# rescale
train.x=train.x / 255
test=test / 255
train.y = to_categorical(train.y, 10)
relu=rectifier, max(0,neuron weight)) tanh=hyperbolic arctangent softmax=normalized exponential function (a compressing function)
model = keras_model_sequential()
model %>%
layer_dense(units = 500, activation = 'relu', input_shape = c(784)) %>%
layer_dropout(rate = 0.4) %>%
layer_dense(units = 400, activation = 'tanh') %>%
layer_dropout(rate = 0.3) %>%
layer_dense(units = 300, activation = 'relu') %>%
layer_dropout(rate = 0.2) %>%
layer_dense(units = 200, activation = 'tanh') %>%
layer_dropout(rate = 0.2) %>%
layer_dense(units = 100, activation = 'relu') %>%
layer_dropout(rate = 0.2) %>%
layer_dense(units = 50, activation = 'tanh') %>%
layer_dropout(rate = 0.2) %>%
layer_dense(units = 25, activation = 'relu') %>%
layer_dropout(rate = 0.1) %>%
layer_dense(units = 10, activation = 'softmax')
model %>% compile(
loss = 'categorical_crossentropy',
optimizer = optimizer_rmsprop(),
metrics = c('accuracy')
)
history <- model %>% fit(
train.x,train.y,
epochs = 50, batch_size = 256,
validation_split = 0.2
)
plot(history)
summary(model)
## ___________________________________________________________________________
## Layer (type) Output Shape Param #
## ===========================================================================
## dense_1 (Dense) (None, 500) 392500
## ___________________________________________________________________________
## dropout_1 (Dropout) (None, 500) 0
## ___________________________________________________________________________
## dense_2 (Dense) (None, 400) 200400
## ___________________________________________________________________________
## dropout_2 (Dropout) (None, 400) 0
## ___________________________________________________________________________
## dense_3 (Dense) (None, 300) 120300
## ___________________________________________________________________________
## dropout_3 (Dropout) (None, 300) 0
## ___________________________________________________________________________
## dense_4 (Dense) (None, 200) 60200
## ___________________________________________________________________________
## dropout_4 (Dropout) (None, 200) 0
## ___________________________________________________________________________
## dense_5 (Dense) (None, 100) 20100
## ___________________________________________________________________________
## dropout_5 (Dropout) (None, 100) 0
## ___________________________________________________________________________
## dense_6 (Dense) (None, 50) 5050
## ___________________________________________________________________________
## dropout_6 (Dropout) (None, 50) 0
## ___________________________________________________________________________
## dense_7 (Dense) (None, 25) 1275
## ___________________________________________________________________________
## dropout_7 (Dropout) (None, 25) 0
## ___________________________________________________________________________
## dense_8 (Dense) (None, 10) 260
## ===========================================================================
## Total params: 800,085
## Trainable params: 800,085
## Non-trainable params: 0
## ___________________________________________________________________________
eval=model %>% evaluate(train.x, train.y)
eval
## $loss
## [1] 0.02806159
##
## $acc
## [1] 0.9955
a=model %>% predict_classes(test)
b=seq(1:28000)
t=cbind(b,a)
colnames(t)=c("ImageId", "Label")
write.csv(t, "D:/mnist/keras.csv", row.names=FALSE)