Skip to content

Commit

Permalink
Informes Introducción Redes Neuronales
Browse files Browse the repository at this point in the history
Primeros informes realizados como introducción al trabajo con Redes
Neuronales en R.
  • Loading branch information
fpulgar authored and fpulgar committed Dec 11, 2015
1 parent 1f38960 commit 892bfe3
Show file tree
Hide file tree
Showing 3 changed files with 763 additions and 0 deletions.
117 changes: 117 additions & 0 deletions H2O_Example.Rmd
@@ -0,0 +1,117 @@
---
title: "H2O_Example"
author: "Fran"
date: "10 de diciembre de 2015"
output: html_document
---

Carga de datos (ejemplo MNIST):

1- Cargamos la librería.

```{r}
library(h2o)
```

2- Establecemos el número de hebras al máximo de cores disponibles y cargamos los datos de entrenamiento y test:

```{r}
h2o.init (nthreads = -1)
train_file <- "https://h2o-public-test-data.s3.amazonaws.com/bigdata/laptop/mnist/train.csv.gz"
test_file <- "https://h2o-public-test-data.s3.amazonaws.com/bigdata/laptop/mnist/test.csv.gz"
train<-h2o.importFile(train_file)
test<-h2o.importFile(test_file)
```

3- Obtener un resumen de los datos:

```{r}
summary(train)
summary(test)
```

4- Especificar las columnas de respuesta y predicción:

```{r}
y <- "C785"
x <- setdiff(names(train), y)
```

5- Codificar la columna de respuestas como categórica para claslificación multinominal.

```{r}
train[,y] <- as.factor(train[,y])
test[,y] <- as.factor(test[,y])
```

6- Entrenar el modelo y validar con test.
```{r}
model <- h2o.deeplearning(x = x, y = y, training_frame=train, validation_frame=test, distribution ="multinomial", activation = "RectifierWithDropout", hidden = c(32,32,32), input_dropout_ratio = 0.2, sparse = TRUE, l1 =1e-5, epochs =10)
```

7- Realizar 5cv en training.
```{r}
model_cv <- h2o.deeplearning(x = x, y = y, training_frame=train, validation_frame=test, distribution ="multinomial", activation = "RectifierWithDropout", hidden = c(32,32,32), input_dropout_ratio = 0.2, sparse = TRUE, l1 =1e-5, epochs =10, nfolds = 5)
```

8- View model result

8.1- Parametros del modelo.

- Modelo sin cv.
```{r}
model@parameters
```

- Modelo con cv.
```{r}
model_cv@parameters
```

8.2- Métricas de rendimiento (train y test)

- Modelo sin cv:
```{r}
model
h2o.performance(model, train = TRUE)
h2o.performance(model, valid = TRUE)
```

- Modelo con cv:
```{r}
model_cv
h2o.performance(model_cv, train = TRUE)
h2o.performance(model_cv, valid = TRUE)
```

8.3- Obtener sólo MSE
```{r}
h2o.mse(model, valid=TRUE)
```

8.4- Cross-validated MSE
```{r}
h2o.mse(model_cv, xval=TRUE)
```

9- Clasificación del fichero de test.

- Modelo sin cv:
```{r}
pred <- h2o.predict(model,newdata = test)
head(pred)
```

- Modelo con cv:
```{r}
pred_cv <- h2o.predict(model_cv,newdata = test)
head(pred_cv)
```

221 changes: 221 additions & 0 deletions InformeRedesNeuronalesR.Rmd
@@ -0,0 +1,221 @@
---
title: "Redes Neuronales"
author: "Fran"
date: "18 de noviembre de 2015"
output: html_document
---

Informe sobre utilización de Redes Neuronales en R con varios dataset:

- Carga del paquete, introduciendo en la consola de R el siguiente comando:

```{r, message=FALSE}
library(neuralnet)
```

- Dataset Iris:

1- Carga del dataset.

```{r}
data(iris)
head(iris)
```


2- Partición en test y train.

```{r}
fold.test <- sample(nrow(iris), nrow(iris) / 3)
test <- iris[fold.test, ]
train <- iris[-fold.test, ]
```

3- Primera configuración:

3.1- Entrenamiento de la red neuronal.
Parámetros: 1 capa oculta de 10 neuronas y 3 repeticiones.
Resto de parámetros por defecto.

```{r}
ann <- neuralnet(as.numeric(Species) ~ Sepal.Length + Sepal.Width +
Petal.Length + Petal.Width, train, hidden = 10, rep=3)
ann
```

3.2- Estructura de la red producida:

```{r}
plot(ann, rep = "best")
par(mfrow=c(2,2))
gwplot(ann, selected.covariate = "Sepal.Length")
gwplot(ann, selected.covariate = "Sepal.Width")
gwplot(ann, selected.covariate = "Petal.Length")
gwplot(ann, selected.covariate = "Petal.Width")
```

3.3- Predicción de nuevos resultados (a través del fichero de test):

```{r}
output <- compute(ann, test[ , c("Sepal.Length", "Sepal.Width",
"Petal.Length", "Petal.Width")])
result <- data.frame(
Real = test$Species,
Predicted = levels(iris$Species)[round(output$net.result)])
table(result$Predicted, result$Real)
```

3.4- Error para la configuración establecida:

```{r,echo=FALSE}
error<- table(result$Predicted==result$Real)
barplot(error)
error1<-if(length(error)==1){0}else{error[[1]]/(error[[1]]+error[[2]])}
error1
```
4- Segunda configuración:

4.1- Entrenamiento de la red neuronal.
Parámetros: 2 capa oculta de 5 y 10 neuronas y 1 repetición.
Resto de parámetros por defecto.

```{r}
ann <- neuralnet(as.numeric(Species) ~ Sepal.Length + Sepal.Width +
Petal.Length + Petal.Width, train, hidden = c(5,10))
ann
```

4.2- Estructura de la red producida:

```{r}
plot(ann, rep = "best")
par(mfrow=c(2,2))
gwplot(ann, selected.covariate = "Sepal.Length")
gwplot(ann, selected.covariate = "Sepal.Width")
gwplot(ann, selected.covariate = "Petal.Length")
gwplot(ann, selected.covariate = "Petal.Width")
```

4.3- Predicción de nuevos resultados (a través del fichero de test):

```{r}
output <- compute(ann, test[ , c("Sepal.Length", "Sepal.Width",
"Petal.Length", "Petal.Width")])
result <- data.frame(
Real = test$Species,
Predicted = levels(iris$Species)[round(output$net.result)])
table(result$Predicted, result$Real)
```

4.4- Error para la configuración establecida:

```{r,echo=FALSE}
error<- table(result$Predicted==result$Real)
barplot(error)
error2<-if(length(error)==1){0}else{error[[1]]/(error[[1]]+error[[2]])}
error2
```

5- Tercera configuración:

5.1- Entrenamiento de la red neuronal.
Parámetros: 3 capa oculta de 15, 10 y 5 neuronas y 3 repeticiones.
Resto de parámetros por defecto.

```{r}
ann <- neuralnet(as.numeric(Species) ~ Sepal.Length + Sepal.Width +
Petal.Length + Petal.Width, train, hidden = c(15,10,5), rep=3)
ann
```

5.2- Estructura de la red producida:

```{r}
plot(ann, rep = "best")
par(mfrow=c(2,2))
gwplot(ann, selected.covariate = "Sepal.Length")
gwplot(ann, selected.covariate = "Sepal.Width")
gwplot(ann, selected.covariate = "Petal.Length")
gwplot(ann, selected.covariate = "Petal.Width")
```

5.3- Predicción de nuevos resultados (a través del fichero de test):

```{r}
output <- compute(ann, test[ , c("Sepal.Length", "Sepal.Width",
"Petal.Length", "Petal.Width")])
result <- data.frame(
Real = test$Species,
Predicted = levels(iris$Species)[round(output$net.result)])
table(result$Predicted, result$Real)
```

5.4- Error para la configuración establecida:

```{r,echo=FALSE}
error<- table(result$Predicted==result$Real)
barplot(error)
error3<-if(length(error)==1){0}else{error[[1]]/(error[[1]]+error[[2]])}
error3
````

6- Cuarta configuración:

6.1- Entrenamiento de la red neuronal.
Parámetros: 4 capas ocultas de 20, 15, 10 y 5 neuronas y 3 repeticiones.
Resto de parámetros por defecto.

```{r}
ann <- neuralnet(as.numeric(Species) ~ Sepal.Length + Sepal.Width +
Petal.Length + Petal.Width, train, hidden = c(20,15,10,5), rep=3)
ann
```

6.2- Estructura de la red producida:

```{r}
plot(ann, rep = "best")
par(mfrow=c(2,2))
gwplot(ann, selected.covariate = "Sepal.Length")
gwplot(ann, selected.covariate = "Sepal.Width")
gwplot(ann, selected.covariate = "Petal.Length")
gwplot(ann, selected.covariate = "Petal.Width")
```

6.3- Predicción de nuevos resultados (a través del fichero de test):

```{r}
output <- compute(ann, test[ , c("Sepal.Length", "Sepal.Width",
"Petal.Length", "Petal.Width")])
result <- data.frame(
Real = test$Species,
Predicted = levels(iris$Species)[round(output$net.result)])
table(result$Predicted, result$Real)
```

6.4- Error para la configuración establecida:

```{r,echo=FALSE}
error<- table(result$Predicted==result$Real)
barplot<-error
error4<-if(length(error)==1){0}else{error[[1]]/(error[[1]]+error[[2]])}
error4
````


7- Errores obtenidos:

```{r,echo=FALSE}
barplot(c(error1,error2,error3,error4),names.arg=c("Error1","Error2","Error3","Error4"),main="Errores Obtenidos",xlab="Configuración",ylab="Error")
```


0 comments on commit 892bfe3

Please sign in to comment.