Redes Neuronales Artificiales
Ejercicio de clase:
Probemos si una RNA puede calcular probabilidades, sin saber la fórmula de como hacerlo. Por ejemplo, simula probabilidades de alguna distribución normal o distribución Binomial y analiza que tan cerca puede estimar sus probabilidades.
Ejemplo de datos normales simulados.
Código R
set.seed(1234)
x <- round(seq(-3, 3, length = 100), 1)
y <- round(pnorm(x), 4)
data <- data.frame(x = x, y = y)
plot(data)Creamos el data frame y se asignan los valores de entrenamiento y de prueba.
Red Neuronal
Código R
library(neuralnet)
RNA <- neuralnet(y ~ x, entrenamiento, hidden = 6, rep = 6)
# plot(RNA, rep = "best")
output <- compute(RNA, prueba[ , c("x", "y")], rep = 5)
Comp <- data.frame(
Real = round(prueba$y,3),
Pred = round(output$net.result,3),
Error = round(abs(prueba$y - output$net.result)/prueba$y,3))
Comp## Real Pred Error
## 1 0.001 0.000 1.110
## 2 0.002 0.000 0.832
## 3 0.002 0.000 0.832
## 4 0.004 0.002 0.332
## 5 0.005 0.004 0.155
## 6 0.005 0.004 0.155
## 7 0.006 0.006 0.020
## 8 0.008 0.009 0.061
## 9 0.011 0.012 0.113
## 10 0.011 0.012 0.113
## 11 0.014 0.016 0.134
## 12 0.023 0.026 0.128
## 13 0.023 0.026 0.128
## 14 0.029 0.032 0.115
## 15 0.036 0.039 0.094
## 16 0.045 0.048 0.070
## 17 0.055 0.058 0.049
## 18 0.067 0.069 0.029
## 19 0.067 0.069 0.029
## 20 0.081 0.082 0.010
## 21 0.097 0.096 0.004
## 22 0.115 0.113 0.015
## 23 0.115 0.113 0.015
## 24 0.136 0.133 0.023
## 25 0.136 0.133 0.023
## 26 0.159 0.154 0.027
## 27 0.184 0.179 0.028
## 28 0.184 0.179 0.028
## 29 0.212 0.206 0.026
## 30 0.242 0.237 0.021
## 31 0.274 0.270 0.014
## 32 0.308 0.307 0.006
## 33 0.308 0.307 0.006
## 34 0.345 0.346 0.003
## 35 0.382 0.386 0.011
## 36 0.382 0.386 0.011
## 37 0.421 0.429 0.019
## 38 0.421 0.429 0.019
## 39 0.460 0.471 0.024
## 40 0.500 0.514 0.029
## 41 0.500 0.514 0.029
## 42 0.540 0.556 0.031
## 43 0.579 0.597 0.031
## 44 0.579 0.597 0.031
## 45 0.618 0.636 0.029
## 46 0.655 0.672 0.026
## 47 0.692 0.706 0.021
## 48 0.692 0.706 0.021
## 49 0.726 0.737 0.016
## 50 0.726 0.737 0.016
## 51 0.788 0.792 0.005
## 52 0.788 0.792 0.005
## 53 0.816 0.816 0.000
## 54 0.816 0.816 0.000
## 55 0.841 0.837 0.005
## 56 0.864 0.857 0.009
## 57 0.864 0.857 0.009
## 58 0.885 0.875 0.012
## 59 0.903 0.891 0.014
## 60 0.919 0.906 0.015
## 61 0.919 0.906 0.015
## 62 0.933 0.919 0.015
## 63 0.933 0.919 0.015
## 64 0.945 0.931 0.015
## 65 0.964 0.953 0.011
## 66 0.971 0.963 0.009
## 67 0.977 0.972 0.006
## 68 0.982 0.980 0.003
## 69 0.986 0.987 0.001
## 70 0.989 0.994 0.005
## 71 0.989 0.994 0.005
## 72 0.994 1.006 0.012
## 73 0.994 1.006 0.012
## 74 0.995 1.011 0.016
## 75 0.997 1.015 0.019
## 76 0.997 1.020 0.022
## 77 0.997 1.020 0.022
## 78 0.998 1.023 0.025
## 79 0.998 1.023 0.025
## 80 0.999 1.027 0.028
## [1] 0.0683375
Se entrena la red neuronal con los datos de entrenamiento, luego comparamos los resultados con las probabilidades reales.
Visualización de resultados
Con un 20% de los datos para entrenamiento, desconociendo la fórmula de pnorm(x), a la RNA le bastó para hacer una simulación de probabilidades realista, y almenos visualmente semejante.
Mayor cantidad de datos de entrenamiento
Código R
set.seed(1234)
n <- round(N*0.40)
indices <- sample(1:N,n)
entrenamiento <- data[indices,]
prueba <- data[-indices,]
RNA1 <- neuralnet(y ~ x, entrenamiento, hidden = 6, rep = 6)
plot(RNA1, rep = "best")output <- compute(RNA1, prueba[ , c("x", "y")], rep = 5)
Comp1<-data.frame(
Real = round(prueba$y,3),
Pred = round(output$net.result,3),
Error = round(abs(prueba$y - output$net.result)/prueba$y,3))
Comp1## Real Pred Error
## 1 0.001 -0.003 3.660
## 2 0.005 0.003 0.422
## 3 0.005 0.003 0.422
## 4 0.006 0.005 0.122
## 5 0.008 0.009 0.066
## 6 0.011 0.013 0.183
## 7 0.011 0.013 0.183
## 8 0.014 0.017 0.242
## 9 0.023 0.029 0.263
## 10 0.023 0.029 0.263
## 11 0.029 0.036 0.250
## 12 0.036 0.044 0.225
## 13 0.045 0.053 0.191
## 14 0.055 0.064 0.159
## 15 0.067 0.075 0.126
## 16 0.067 0.075 0.126
## 17 0.081 0.088 0.095
## 18 0.115 0.120 0.043
## 19 0.115 0.120 0.043
## 20 0.136 0.139 0.022
## 21 0.159 0.160 0.006
## 22 0.184 0.183 0.006
## 23 0.184 0.183 0.006
## 24 0.212 0.209 0.014
## 25 0.242 0.237 0.019
## 26 0.345 0.339 0.015
## 27 0.382 0.378 0.010
## 28 0.382 0.378 0.010
## 29 0.500 0.503 0.006
## 30 0.540 0.545 0.010
## 31 0.579 0.587 0.013
## 32 0.618 0.627 0.014
## 33 0.655 0.665 0.014
## 34 0.692 0.700 0.013
## 35 0.692 0.700 0.013
## 36 0.726 0.734 0.011
## 37 0.726 0.734 0.011
## 38 0.788 0.792 0.005
## 39 0.788 0.792 0.005
## 40 0.816 0.817 0.001
## 41 0.864 0.861 0.004
## 42 0.864 0.861 0.004
## 43 0.885 0.879 0.007
## 44 0.903 0.896 0.008
## 45 0.919 0.911 0.009
## 46 0.919 0.911 0.009
## 47 0.933 0.924 0.010
## 48 0.933 0.924 0.010
## 49 0.971 0.965 0.006
## 50 0.977 0.973 0.004
## 51 0.982 0.980 0.002
## 52 0.986 0.986 0.000
## 53 0.989 0.991 0.002
## 54 0.989 0.991 0.002
## 55 0.994 0.999 0.005
## 56 0.995 1.002 0.007
## 57 0.997 1.005 0.008
## 58 0.997 1.007 0.010
## 59 0.998 1.009 0.010
## 60 0.999 1.010 0.011
## [1] 0.1239333
Con el doble de datos de entrenamientos, en esta semilla, tenemos una notable disminución de error medio en los datos predictivos, respecto a los reales.