-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneuralNetwork.R
88 lines (60 loc) · 2.16 KB
/
neuralNetwork.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
cat("\nSTART\n")
startTime = proc.time()[3]
startTime
library(nnet)
#traing of neural network
modelName <- "neuralNetwork"
InputDataFileName="E:/Tech MAhindra Project/DigitRecognition-master/mnist_train.csv"
trainDataset <- read.csv(InputDataFileName)
trainDataset <- trainDataset[sample(nrow(trainDataset)),]
View(trainDataset)
head(trainDataset)
nrow(trainDataset)
names(trainDataset)
cat("\nStep 5: Choose Target Variable")
target <- names(trainDataset)[1] #label name
target
cat("\nStep 6: Choose Inputs Variable")
inputs <- setdiff(names(trainDataset),target)
inputs
length(inputs)
cat("\nStep 7: Select training dataset")
head(trainDataset)
nrow(trainDataset)
X=trainDataset[,-1]
Y=trainDataset[,1]
Xreduced <- X/255
Xcov <- cov(Xreduced)
pcaX <- prcomp(Xcov)
Xfinal <- as.matrix(Xreduced) %*% pcaX$rotation[,1:45]
Y <- class.ind(Y)
cat("\nStep 8: Select testing dataset")
testDataset <- read.csv("E:/Tech MAhindra Project/DigitRecognition-master/mnist_test.csv")
View(testDataset)
head(testDataset)
nrow(testDataset)
test=testDataset[,-1]
testreduced <- test/255
testreduced <- as.matrix(testreduced) %*% pcaX$rotation[,1:45]
cat("\nStep 9: Model Building -> ", modelName)
model <- nnet(Xfinal,Y,size=150,softmax=TRUE,maxit=130,MaxNWts =80000)
model
cat("\nStep 10: Prediction using -> ", modelName)
Predicted <- predict(model, testreduced,type="class")
Predicted <- as.data.frame(Predicted);
head(Predicted)
cat("\nStep 11: Extracting Actual")
Actual <- as.double(unlist(testDataset[target]))
head(Actual)
cat("\nStep 12: Model Evaluation")
totalTime = proc.time()[3] - startTime
totalTime
result <- data.frame(modelName,accuracy, totalTime)[1:1,]
result
cat("\nStep 13: Writing to file")
write.csv(result, file=paste(modelName,"-Evaluation-Result.csv",sep=''), row.names=FALSE)
write.csv(data.frame(Actual,Predicted), file=paste(modelName,"-ActualPredicted-Result.csv",sep=''), row.names=FALSE)
cat("\nStep 14: Saving the Model ->",modelName)
save.image(file=paste(modelName,"-Model.RData",sep=''))
cat("\nDone")
cat("\nTotal Time Taken: ", totalTime," sec")