Home Artificial Intelligence Digit Classification using deep learning model created using R language. Introduction: Importing crucial libraries: Loading and Preparing MNIST Preparing MNIST and the Collected Handwritten Digits for Training Train and Validate Your Model

Digit Classification using deep learning model created using R language. Introduction: Importing crucial libraries: Loading and Preparing MNIST Preparing MNIST and the Collected Handwritten Digits for Training Train and Validate Your Model

1
Digit Classification using deep learning model created using R language.
Introduction:
Importing crucial libraries:
Loading and Preparing MNIST
Preparing MNIST and the Collected Handwritten Digits for Training
Train and Validate Your Model

source: tembler.net
library(dslabs)
library(keras)
library(tensorflow)
mnist <- read_mnist()
i <- 5
image(1:28, 1:28, matrix(mnist$test$images[i,], nrow=28)[ , 28:1],
col = gray(seq(0, 1, 0.05)), xlab = "", ylab="")
Image displayed.
chd <- read.csv("combined_digits_1.csv")
dim(chd)
xtest = chd[,1:784]
ytest = chd[,785]

xtest = as.matrix(xtest)
xtest <- array_reshape(xtest, c(nrow(xtest), 28, 28, 1))

mnist = read_mnist()  
x_train = mnist$train$images
y_train = mnist$train$labels
x_val = mnist$test$images
y_val = mnist$test$labels

x_train = array_reshape(x_train, c(nrow(x_train), 28, 28, 1))
x_val = array_reshape(x_val, c(nrow(x_val), 28, 28, 1))

y_train = to_categorical(y_train, 10)
y_val = to_categorical(y_val, 10)
ytest = to_categorical(ytest,10)


#Model Constructing: Code Here
input_shape <- c(28, 28, 1)
batch_size <- 128
num_classes <- 10
epochs <- 10

model <- keras_model_sequential() %>%
layer_conv_2d(filters = 32, kernel_size = c(3,3), activation = 'relu', input_shape = input_shape) %>%
layer_max_pooling_2d(pool_size = c(2, 2)) %>%
layer_conv_2d(filters = 64, kernel_size = c(3,3), activation = 'relu') %>%
layer_max_pooling_2d(pool_size = c(2, 2)) %>%
layer_dropout(rate = 0.25) %>%
layer_flatten() %>%
layer_dense(units = 128, activation = 'relu') %>%
layer_dropout(rate = 0.5) %>%
layer_dense(units = num_classes, activation = 'softmax')

summary(model);

Model Architecture.

# compiling our model
model %>% compile(
loss = loss_categorical_crossentropy,
optimizer = optimizer_adadelta(),
metrics = c('accuracy')
)
# fitting the model(training it)  
model_history <- model %
fit(x_train, y_train,
batch_size = batch_size,
epochs = epochs,
validation_data = list(x_val, y_val),
verbose = 1)
Model training in motion.
#Model Testing
model %>% evaluate(xtest, ytest)
Loss and Accuracy.
#saveRDS(model,"/_model.RDS")
saveRDS(model, "digit_classifier.rds")

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here