Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Funciones para experimentación sobre Redes Neuronales en R.
- Loading branch information
fpulgar
authored and
fpulgar
committed
Feb 3, 2016
1 parent
892bfe3
commit e0fd42c
Showing
4 changed files
with
518 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
#Función para realizar las particiones necesarias para el proceso 2-5CV | ||
#Mediante un fichero de entrenamiento, test, un fichero con las clases de test y el nombre final | ||
do_partitions <- function(train,test,testC,name){ | ||
train<-read.table(train) | ||
test<-read.table(test) | ||
testR<-read.table(testC) | ||
|
||
y<-colnames(train)[ncol(train)] | ||
|
||
names(testR)[1]<-y | ||
test<-cbind(test,testR) | ||
|
||
train<-rbind(train,test) | ||
|
||
for(j in 1:2){ | ||
indicesTotal<-1:nrow(train) | ||
nTraining <- as.integer(nrow(train)*.20) | ||
for(i in 1:5){ | ||
nombreArc<-paste(name,j,"_","5-",i,".txt",sep="") | ||
if(i!=5){ | ||
indices <- sample(indicesTotal,nTraining) | ||
}else{ | ||
indices <- indicesTotal | ||
} | ||
indicesTotal <- indicesTotal[-match(indices,indicesTotal)] | ||
|
||
data<-train[indices,] | ||
|
||
write.table(data, nombreArc, sep='\t') | ||
} | ||
} | ||
|
||
} | ||
|
||
#Función para realizar las particiones necesarias para el proceso 2-5CV | ||
#Mediante un fichero de entrenamiento, test, el nombre final y si tiene o no cabecera (ficheros .csv) | ||
do_partitions_csv <- function(train,test,name,head){ | ||
train<-read.csv(train,header=head) | ||
test<-read.csv(test,header=head) | ||
|
||
train<-rbind(train,test) | ||
|
||
for(j in 1:2){ | ||
indicesTotal<-1:nrow(train) | ||
nTraining <- as.integer(nrow(train)*.20) | ||
for(i in 1:5){ | ||
nombreArc<-paste(name,j,"_","5-",i,".txt",sep="") | ||
if(i!=5){ | ||
indices <- sample(indicesTotal,nTraining) | ||
}else{ | ||
indices <- indicesTotal | ||
} | ||
indicesTotal <- indicesTotal[-match(indices,indicesTotal)] | ||
|
||
data<-train[indices,] | ||
|
||
write.table(data, nombreArc, sep='\t') | ||
} | ||
} | ||
|
||
} | ||
|
||
#Función para realizar las particiones necesarias para el proceso 2-5CV | ||
#Mediante un fichero de datos, el nombre final y el separador utilizado. | ||
do_partitions1 <- function(train,name,separ){ | ||
train<-read.table(train,sep=separ) | ||
|
||
for(j in 1:2){ | ||
indicesTotal<-1:nrow(train) | ||
nTraining <- as.integer(nrow(train)*.20) | ||
for(i in 1:5){ | ||
nombreArc<-paste(name,j,"_","5-",i,".txt",sep="") | ||
if(i!=5){ | ||
indices <- sample(indicesTotal,nTraining) | ||
}else{ | ||
indices <- indicesTotal | ||
} | ||
indicesTotal <- indicesTotal[-match(indices,indicesTotal)] | ||
|
||
data<-train[indices,] | ||
|
||
write.table(data, nombreArc, sep='\t') | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
#Función para realizar las particiones necesarias para el proceso 2-5CV | ||
#Mediante un fichero de datos, un fichero con las clases correspondientes y el nombre final. | ||
do_partitions2 <- function(train,trainC,name){ | ||
train<-read.table(train) | ||
trainR<-read.table(trainC) | ||
|
||
names(trainR)[1]<-"class" | ||
|
||
train<-cbind(train,trainR) | ||
|
||
for(j in 1:2){ | ||
indicesTotal<-1:nrow(train) | ||
nTraining <- as.integer(nrow(train)*.20) | ||
for(i in 1:5){ | ||
nombreArc<-paste(name,j,"_","5-",i,".txt",sep="") | ||
if(i!=5){ | ||
indices <- sample(indicesTotal,nTraining) | ||
}else{ | ||
indices <- indicesTotal | ||
} | ||
indicesTotal <- indicesTotal[-match(indices,indicesTotal)] | ||
|
||
data<-train[indices,] | ||
|
||
write.table(data, nombreArc, sep='\t') | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
#Función para obtener la clasificación de un dataset comprimido mediante diferentes clasificadores y modelos de compresión | ||
#data -> Dataset seleccionado. | ||
#porcLayers -> Vector con el porcentaje de las diferentes capas a aplicar, si no es válido se aplica sin compresión. | ||
#modelo -> Modelo seleccionado para realizar la compresión, si no es válido se aplica sin compresión. | ||
#clasif -> Clasificador que se aplicará, si no es válido no se obtiene resultados. | ||
deepLearning_classification <- function(data,porcLayers,modelo,clasif){ | ||
timTot<-Sys.time() | ||
porcLayersC<-paste(porcLayers,collapse="_") | ||
contents<-paste("Ejecución para el dataset",data,"con el modelo",modelo,"y el clasificador",clasif,":\n",sep=" ") | ||
contents<-paste(contents,"Porcentajes de las capas: ",porcLayersC,"\n",sep="") | ||
accTot<-0.0 | ||
timComTot<-0.0 | ||
timClasTot<-0.0 | ||
porcLayers<-sort(porcLayers,decreasing = TRUE) | ||
if(porcLayers[length(porcLayers)]<=0 || !is.numeric(porcLayers)){ | ||
modelo<-"NO_APLICA" | ||
} | ||
for(j in 1:2){ | ||
for(i in 1:5){ | ||
ej<-paste(j,"-",i,sep="") | ||
contents<-paste(contents,"----Ejecución",ej,":----\n",sep=" ") | ||
nombreTest<-paste(data,j,"_","5-",i,".txt",sep="") | ||
test<-read.table(nombreTest,sep='\t') | ||
train<-NULL | ||
layers<-as.integer(porcLayers*(ncol(test)-1)) | ||
for(k in 1:5){ | ||
if(k!=i){ | ||
nombreTrain<-paste(data,j,"_","5-",k,".txt",sep="") | ||
trainR<-read.table(nombreTrain, sep='\t') | ||
if(is.null(test)){ | ||
train<-trainR | ||
}else{ | ||
train<-rbind(train,trainR) | ||
} | ||
} | ||
} | ||
switch(modelo, | ||
H2O_v1={ | ||
timCom<-Sys.time() | ||
data_comp<-compress_data_h2ov1(train,test,layers) | ||
timCom<-as.numeric(Sys.time())-as.numeric(timCom) | ||
timComTot<-timComTot+timCom | ||
contents<-paste(contents,'Tiempo en comprimir dataset:',timCom,"seg.\n",sep=" ") | ||
}, | ||
H2O_v2={ | ||
timCom<-Sys.time() | ||
data_comp<-compress_data_h2ov2(train,test,layers) | ||
timCom<-as.numeric(Sys.time())-as.numeric(timCom) | ||
timComTot<-timComTot+timCom | ||
contents<-paste(contents,'Tiempo en comprimir dataset:',timCom,"seg.\n",sep=" ") | ||
}, | ||
autoencoders={ | ||
timCom<-Sys.time() | ||
data_comp<-compress_data_autoencoder(train,test,layers) | ||
timCom<-as.numeric(Sys.time())-as.numeric(timCom) | ||
timComTot<-timComTot+timCom | ||
contents<-paste(contents,'Tiempo en comprimir dataset:',timCom,"seg.\n",sep=" ") | ||
}, | ||
{ | ||
contents<-paste(contents,'Modelo no válido. No se aplica compresión.',"\n",sep=" ") | ||
y <- colnames(train)[ncol(train)] | ||
train[,y] <- as.factor(train[,y]) | ||
test[,y] <- as.factor(test[,y]) | ||
train_res <- as.h2o(train) | ||
test_res <- as.h2o(test) | ||
data_comp<-c(train_res,test_res) | ||
}) | ||
switch(clasif, | ||
RandomForest={ | ||
timCla<-Sys.time() | ||
result<-clasif_randomForest(data_comp) | ||
timCla<-as.numeric(Sys.time())-as.numeric(timCla) | ||
timClasTot<-timClasTot+timCla | ||
contents<-paste(contents,'Tiempo en clasificar dataset:',timCla,"seg.\n",sep=" ") | ||
tableResult<-table(result$Predicted, result$Real) | ||
if(nrow(tableResult)!= ncol(tableResult)){ | ||
dif<-setdiff(colnames(tableResult),rownames(tableResult)) | ||
for(z in 1:length(dif)){ | ||
name<-dif[z] | ||
newRow<-rep(0,ncol(tableResult)) | ||
tableResult<-rbind(tableResult,newRow) | ||
rownames(tableResult)[ncol(tableResult)]<-name | ||
} | ||
tableResult<-as.table(tableResult) | ||
} | ||
confMatrix<-confusionMatrix(tableResult) | ||
accuracy<-as.numeric(confMatrix[[3]][1]) | ||
contents<-paste(contents,'Precisión para la ejecución:',accuracy,"\n",sep=" ") | ||
accTot<-accTot+accuracy | ||
#print(accuracy) | ||
}, | ||
KNN={ | ||
timCla<-Sys.time() | ||
result<-clasif_KNN(data_comp) | ||
timCla<-as.numeric(Sys.time())-as.numeric(timCla) | ||
timClasTot<-timClasTot+timCla | ||
contents<-paste(contents,'Tiempo en clasificar dataset:',timCla,"seg.\n",sep=" ") | ||
tableResult<-table(result$Predicted, result$Real) | ||
if(nrow(tableResult)!= ncol(tableResult)){ | ||
dif<-setdiff(colnames(tableResult),rownames(tableResult)) | ||
for(z in 1:length(dif)){ | ||
name<-dif[z] | ||
newRow<-rep(0,ncol(tableResult)) | ||
tableResult<-rbind(tableResult,newRow) | ||
rownames(tableResult)[ncol(tableResult)]<-name | ||
} | ||
tableResult<-as.table(tableResult) | ||
} | ||
confMatrix<-confusionMatrix(tableResult) | ||
accuracy<-as.numeric(confMatrix[[3]][1]) | ||
contents<-paste(contents,'Precisión para la ejecución:',accuracy,"\n",sep=" ") | ||
accTot<-accTot+accuracy | ||
#print(accuracy) | ||
}, | ||
C4.5={ | ||
timCla<-Sys.time() | ||
result<-clasif_C45(data_comp) | ||
timCla<-as.numeric(Sys.time())-as.numeric(timCla) | ||
contents<-paste(contents,'Tiempo en clasificar dataset:',timCla,"seg.\n",sep=" ") | ||
timClasTot<-timClasTot+timCla | ||
tableResult<-table(result$Predicted, result$Real) | ||
if(nrow(tableResult)!= ncol(tableResult)){ | ||
dif<-setdiff(colnames(tableResult),rownames(tableResult)) | ||
for(z in 1:length(dif)){ | ||
name<-dif[z] | ||
newRow<-rep(0,ncol(tableResult)) | ||
tableResult<-rbind(tableResult,newRow) | ||
rownames(tableResult)[ncol(tableResult)]<-name | ||
} | ||
tableResult<-as.table(tableResult) | ||
} | ||
confMatrix<-confusionMatrix(tableResult) | ||
accuracy<-as.numeric(confMatrix[[3]][1]) | ||
contents<-paste(contents,'Precisión para la ejecución:',accuracy,"\n",sep=" ") | ||
accTot<-accTot+accuracy | ||
#print(accuracy) | ||
}, | ||
{ | ||
contents<-paste(contents,'Clasificador no válido.',"\n",sep=" ") | ||
}) | ||
} | ||
} | ||
accTot<-accTot/10 | ||
timClasTot<-timClasTot/10 | ||
timComTot<-timComTot/10 | ||
timTot<-as.numeric(Sys.time())-as.numeric(timTot) | ||
contents<-paste(contents,'----Resultados Finales----\n',sep=" ") | ||
contents<-paste(contents,'Tiempo medio en la compresión de datos:',timComTot,"seg.\n",sep=" ") | ||
contents<-paste(contents,'Tiempo medio en la clasificación:',timClasTot,"seg.\n",sep=" ") | ||
contents<-paste(contents,'Precisión media todas las ejecuciones:',accTot,"\n",sep=" ") | ||
contents<-paste(contents,'Tiempo total:',timTot,"seg.\n",sep=" ") | ||
nomArch<-paste(data,"_",porcLayersC,"_",modelo,"_",clasif,sep="") | ||
nomArch<-gsub('\\.','',nomArch) | ||
nomArch<-paste(nomArch,".txt",sep="") | ||
write(contents,nomArch) | ||
#cat(contents) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#Ejecuciones programadas | ||
#Carga de funciones utilizadas: | ||
source("~/Documents/RepositoriosGit/Redes-Neuronales/FuncionesDeepLearning.R") | ||
source("~/Documents/RepositoriosGit/Redes-Neuronales/DeepLearning_Classification.R") | ||
|
||
#1- Carga de los paquetes necesarios: | ||
if(!is.installed('autoencoder')) | ||
install.packages('autoencoder') | ||
library('autoencoder') | ||
|
||
if(!is.installed('h2o')) | ||
install.packages('h2o') | ||
library('h2o') | ||
h2o.init (nthreads = -1) | ||
|
||
if(!is.installed('RWeka')) | ||
install.packages('RWeka') | ||
library(RWeka) | ||
|
||
if(!is.installed('caret')) | ||
install.packages('caret') | ||
library(caret) | ||
|
||
if(!is.installed('kknn')) | ||
install.packages('kknn') | ||
library(kknn) | ||
|
||
#2- Establecer working directory. | ||
setwd("~/Documents/RepositoriosGit/Redes-Neuronales/Dataset") | ||
|
||
#3- Ejecuciones | ||
dataset<-c("coil2000","letter","MNIST","madelon","gisette","arcene") | ||
layers<-list(c(1.5,0.1),c(1.5,0.15),c(1.5,0.2),c(1.5,0.5,0.1),c(1.5,0.5,0.15),c(1.5,0.5,0.2),c(1.5,0.5,0.3,0.1),c(1.5,0.5,0.3,0.15),c(1.5,0.5,0.3,0.2)) | ||
model<-c("H2O_v1","H2O_v2","autoencoders") | ||
clasif<-c("RandomForest","KNN","C4.5") | ||
|
||
for(i in 1:length(dataset)){ | ||
for(j in 1:length(layers)){ | ||
for(k in 1:length(model)){ | ||
for(z in 1:length(clasif)){ | ||
deepLearning_classification(dataset[i],layers[[j]],model[k],clasif[z]) | ||
} | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.