Amazing technological breakthrough possible @S-Logix pro@slogix.in

Office Address

  • #5, First Floor, 4th Street Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 Landmark : Samiyar Madam
  • pro@slogix.in
  • +91- 81240 01111

Social List

How to predict the outcome of connect-4 game for a player who plays first using deep neural network in R?

Description

To predict the outcome of connect-4 game for a player who plays first using deep neural network in R

Functions Used

modellayer_dense() – Add a input,output or hidden layer
layer_activation() – Activation function for the layercompile() – To compile the model
fit() – To fit the model using the train set
predict() – To predict using the test set
evaluate() – To evaluate the loss and metrics

Libraries Required :

library(keras)

Process

  Load the necessary libraries

  Load the data set

  Convert the categorical variables to equivalent numeric classes

  Split the data set as train set and test set

  Initialize the keras sequential model

  Build the model with input layers,hidden layers and output layer as per the data size along with the activation function(here 42 I/P layer with relu activation and 3 O/P layer with softmax activation)

  Compile the model with required loss,metrics and optimizer(here loss=categorical_crossentropy,optimizer=adam,metrics=accuracy)

  Fit the model using the train set

  Predict using the test set

  Evaluate the metrics

Sapmle Code

library(caret)
library(keras)
data=read.csv(‘/…../connect-4.csv’)
#Function to convert the categorical variables into its equivalent numeric classes
cat_to_num {
for (i in ind)
{
print(i)
data[,i]=factor(data[,i],labels=c(0:(length(levels(data[,i]))-1)))
}
return(data)
}
cat_cols=c(1:42)
#To convert the categorical target into numeric class
data$X43=factor(data$X43,levels=c(“loss”,”draw”,”win”),labels=c(0:(length(levels(data[,i]))-1)))
data=cat_to_num(cat_cols,data)
#To Split 70% of data as training data
smp_size train_ind #Train set
train_data<-data[train_ind,]
xtrain=train_data[,1:42]
ytrain=train_data$X43
#Test set
test=data[-train_ind,]
xtest=test[,1:42]
ytest=test$X43
#converting the target variable to once hot encoded vectors using keras inbuilt function
train_y<-to_categorical(ytrain)
test_y #defining a keras sequential model
model %
layer_dense(units = 70, input_shape = 42 )%>%
layer_activation(activation = ‘relu’) %>%
layer_dense(units = 3)%>%
layer_activation(activation= “softmax”)
#compiling the defined model with metric = accuracy and optimiser as adam.
model %>% compile(
loss = ‘categorical_crossentropy’,
optimizer = ‘Adam’,
metrics = ‘accuracy’
)
#Summary of the model
summary(model)
#fitting the model on the training dataset
model %>% fit(as.matrix(xtrain), train_y, epochs = 100,batch_size=50)
#Predict using the Test data
yt=predict_classes(model,as.matrix(xtest))
loss_and_metrics % evaluate(as.matrix(xtest), test_y)
print(loss_and_metrics)
cat(“The confusion matrix is \n”)
print(confusionMatrix(as.factor(yt),as.factor(ytest)))

Screenshots
predict the outcome of connect-4 game
player who plays first using deep neural network in R
To fit the model using the train set
evaluate the loss and metrics