-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRECONHECIMENTO_DE_LOCUTOR6.R
328 lines (256 loc) · 11.1 KB
/
RECONHECIMENTO_DE_LOCUTOR6.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#install.packages("mxnet")
library(neuralnet)
library(RSNNS)
library(caret)
library(datasets)
library(nnet)
library(mxnet)
############################## DATASET #############################
seed <- 123
metric <- "Accuracy"
setwd("C:/Program Files/RStudio")
tabela <-read.csv("gender_voice_dataset.csv", header = T, sep = ",", dec = ".")
tabela <-na.omit(tabela)
tabelaClass <- tabela[21]
# Apresenta quantidade respectiva as classes de veiculos presentes no conjunto de dados
#summary(tabela)
#table(tabela$Class)
## Preparacao dos dados: Separa 80% para treinamento e 20% para teste
set.seed(seed)
grupos <- sample(2, nrow(tabela), replace=TRUE, prob=c(0.8, 0.2))
dados_treinamento <- tabela[grupos==1,]
dados_teste <- tabela[grupos==2,]
#library(caret) caso usar o normalizeData
# Remove a coluna que identifica a classe de cada veiculo em ambos os grupos
dados_treinamento_sem_label <- dados_treinamento[-21]
#dados_treinamento_sem_label_normalizado <- normalizeData(dados_treinamento_sem_label, "norm")
dados_treinamento_sem_label_scaled = as.data.frame(lapply(dados_treinamento_sem_label, scale))
dados_teste_sem_label <- dados_teste[-21]
dados_teste_sem_label_scaled = as.data.frame(lapply(dados_teste_sem_label, scale))
# Guarda a classe que identifica cada veiculo em ambos os grupos
labels_dados_treinamento <- dados_treinamento$label
labels_dados_treinamento_decode <- decodeClassLabels(labels_dados_treinamento)
labels_dados_teste <- dados_teste$label
labels_dados_teste_decode <- decodeClassLabels(labels_dados_teste)
#entrar na lista usar [[2]] por exemplo
############################## PARAMETROS FUNCAO TRAIN ##############################
########## RNA 1 ##########
# create empty data frame
learnCurve <- data.frame(m = integer(21),
trainACC = integer(21),
)
# Define funcao trainControl para validacao cruzada
#Resampling: Cross-Validated (10 fold, repeated 10 times)
# Run algorithms using 10-fold cross validation with 3 repeats
ctrl <- trainControl(method = "repeatedcv",
number = 10,
classProbs= TRUE,
verboseIter = TRUE,
savePredictions = TRUE,
returnResamp ="all",
returnData=TRUE,
summaryFunction = defaultSummary,
selectionFunction = "best")
#organiza as 3 layers
#mlp_grid1 = data.frame(layer1 =(1:20), layer2 =(1:20), layer3 =(1:20))
mlp_grid1 = data.frame(layer1 =1, layer2 =1, layer3 =1)
#modelLookup(model = 'neuralnet')
modelLookup(model = 'mlpML')
set.seed(seed)
# start timer
#start_time <- Sys.time()
# loop over training examples
#for (i in 1:5) {
# learnCurve$m[i] <- i
model_rna1 <- train(label~.,
data=dados_treinamento,
method="mlpML",
tuneGrid =mlp_grid1,
linout=TRUE,
trace=TRUE,
verboseIter=TRUE,
#Std_Backpropagation, BackpropBatch, BackpropChunk, BackpropMomentum, BackpropWeightDecay, Rprop, Quickprop, SCG (scaled conjugate gradient),
learnFunc = "Std_Backpropagation",
initFunc = "Randomize_Weights",
#initFuncParams = c(-0.3, 0.3),
#learnFuncParams = c(0.2, 0),
#updateFunc = "Topological_Order",
#updateFuncParams = c(0),
hiddenActFunc = "Act_Logistic",
shufflePatterns = TRUE,
#linOut = FALSE,
#inputsTest = NULL,
#targetsTest = NULL,
#pruneFunc = NULL,
#pruneFuncParams = NULL,
metric=metric,
maxit = 100,
tuneLength = "",
preProcess = c("center","scale"), #com pca
trControl=ctrl,
learningrate = 0.01,
threshold = 0.01,
stepmax = 1e+05)
#learnCurve$trainACC[i] <- model_rna1$results$Accuracy
#}
#In the neural network terminology:
#one epoch = one forward pass and one backward pass of all the training examples
#batch size = the number of training examples in one forward/backward pass. The higher the batch size, the more memory space you'll need.
#number of iterations = number of passes, each pass using [batch size] number of examples. To be clear, one pass = one forward pass + one backward pass (we do not count the forward pass and backward pass as two different passes).
#Example: if you have 1000 training examples, and your batch size is 500, then it will take 2 iterations to complete 1 epoch.
getModelInfo(model_rna1)
model_rna1$results
model_rna1$resample
model_rna1$results["Accuracy"]
plot(log(learnCurve$trainACC),type = "o",col = "red", xlab = "EPOCH",
ylab = "ACC", main = "Linear Model Learning Curve")
install.packages("NeuralNetTools")
library("NeuralNetTools")
plotnet(model_rna1)
print.train(model_rna1)
model_rna1$results
model_rna1$resample
df_acc <- model_rna1$resample
df_acc$Accuracy
plot(df_acc$Accuracy,type="b",frame = FALSE, pch=19)
model_rna1
histogram(model_rna1)
densityplot(model_rna1)
resampleHist(model_rna1)
summary(model_rna1)
class(model_rna1)
attributes(model_rna1)
model_rna1$finalModel
class(model_rna1$finalModel)
getModelInfo(model_rna1)
model_rna1$results
model_rna1
summary(model_rna1)
print(model_rna1)
print(model_rna1, printCall = TRUE, details =TRUE, showSD = TRUE)
print(model_rna1$finalModel$archParams)
model_rna1$finalModel[1]
model_rna1$metric
########## RNA 2 ##########
control <- trainControl(method = "repeatedcv", number = 10, repeats = 1, savePredictions = T, classProbs = T, returnResamp = "all")
set.seed(seed)
model_rna2 <- train(label~., data=dados_treinamento,trControl=control,metric=metric, method="nnet", preProcess = c("center","scale"))
plot(model_rna2)
ggplot(model_rna2)
model_rna2$results
model_rna2$resample
model_rna2
########## RNA 3 ##########
ctrl <- trainControl(method = "repeatedcv", number = 10,classProbs= TRUE, verboseIter = TRUE,savePredictions = TRUE,returnResamp ="all", repeats=3)
pruneFuncParams <- list(max_pr_error_increase = 10.0, pr_accepted_error = 1.0,
no_of_pr_retrain_cycles = 1000, min_error_to_stop = 0.01, init_matrix_value = 1e-6,
input_pruning = TRUE, hidden_pruning = TRUE)
mlp_grid3 = data.frame(layer1 =1, layer2 =0, layer3 =0)
set.seed(seed)
model_rna3 <- train(label~.,
data=dados_treinamento,
method="mlpML",
tuneGrid =mlp_grid3,
linout=TRUE,
trace=TRUE,
verbose=TRUE,
#Std_Backpropagation, BackpropBatch, BackpropChunk, BackpropMomentum, BackpropWeightDecay, Rprop, Quickprop, SCG (scaled conjugate gradient),
learnFunc = "Std_Backpropagation",
initFunc = "Randomize_Weights",
#initFuncParams = c(-0.3, 0.3),
#learnFuncParams = c(0.2, 0),
#updateFunc = "Topological_Order",
#updateFuncParams = c(0),
hiddenActFunc = "Act_Logistic",
shufflePatterns = TRUE,
#linOut = FALSE,
#inputsTest = NULL,
#targetsTest = NULL,
pruneFunc = "OptimalBrainSurgeon",
pruneFuncParams = pruneFuncParams,
metric=metric,
maxit = 30,
tuneLength = 8,
preProcess = c("center","scale"), #com pca
trControl=ctrl,
learningrate = 0.01,
threshold = 0.01,
stepmax = 500)
model_rna3
plot(model_rna3)
ggplot(model_rna3)
library(RSNNS)
extractnetinfo(model_rna3)
##########Comparação RNAS############
#model_rna2
#set.seed(seed)
#model_rna2 <- train(label~., data=dados_treinamento, trControl=ctrl,metric=metric, method="mlp", preProcess = c("center","scale"))
#set.seed(seed)
#model_rna3 <- train(label~., data=dados_treinamento, method="mlpML", metric=metric, preProcess = c("center","scale"),trControl=ctrl)
results <- resamples(list("RNA 1"=model_rna1, "RNA 2" = model_rna2))
results2 <- list("RNA 1"=model_rna1, "RNA2"= model_rna2) #so pra teste
dotplot(results)
densityplot(results,
metric = "Accuracy",
auto.key = TRUE,
pch = "|")
bwplot(results,
metric = "Accuracy")
xyplot(results)
resampleHist(model_rna1)
# Comparacao
summary(results)
# Boxplots dos resultados
accuracy_training_models=bwplot(results,pch='|')
plot(accuracy_training_models[1])
########################## RESULTADOS ##########################
######## RNA 1
model_rna1
model_rna1$results
predicao_rna1 <- predict(model_rna1, dados_teste_sem_label)
predicao_lista <- predict(results2) #pra ver o que da
t1<-table(predictions = predicao_rna1, actual = labels_dados_teste)
t1
prop.table(table(predicao_rna1 == labels_dados_teste))
######### RNA 2
predicao_rna2 <- predict(model_rna2, dados_teste_sem_label)
predicao_lista <- predict(results2) #pra ver o que da
t2<-table(predictions = predicao_rna2, actual = labels_dados_teste)
t2
prop.table(table(predicao_rna2 == labels_dados_teste))
######### RNA 3
predicao_rna3 <- predict(model_rna3, dados_teste_sem_label)
predicao_lista <- predict(results2) #pra ver o que da
t3<-table(predictions = predicao_rna2, actual = labels_dados_teste)
t3
prop.table(table(predicao_rna3 == labels_dados_teste))
####################### Matriz de confusao ####################
cfm_rna1 <- confusionMatrix(predicao_rna1, labels_dados_teste,mode = "everything")
cfm_rna1
cfm_rna2 <- confusionMatrix(predicao_rna2, labels_dados_teste,mode = "everything")
cfm_rna2
cfm_rna3 <- confusionMatrix(predicao_rna3, labels_dados_teste,mode = "everything")
cfm_rna3
results_final <- list("RNA 1"=cfm_rna1$overall[1], "RNA2"= cfm_rna2$overall[1], "RNA3" = cfm_rna3$overall[1])
library (plyr)
df <- ldply (results_final, data.frame)
df <- rename(df, c(".id"="Technique","X..i.."="Acc"))
df
#df$Technique <- rownames(df)
df$Technique
plot(df$Acc,type="b",axes=FALSE,pch=19)
axis(1,at=seq(df$Technique),labels=df$Technique) #to draw x axis
axis(2) #to draw y axis .since your y axis is well computed dont need to use 'at'
grid()
extractPrediction(results2, dados_teste_sem_label)
extractProb(results2, dados_teste_sem_label)
#testar essa comparação
model_comp <- as.data.frame(
rbind(
summod(cm_knn1, knn_model1),
summod(cm_knn2, knn_model2),
summod(cm_knn3, knn_model3)))
rownames(model_comp) <- c("Model 1", "Model 2", "Model 3")
pander(model_comp[,-3], split.tables=Inf, keep.trailing.zeros=TRUE,
style="rmarkdown",
caption="Model results when comparing predictions and test set")