Una forma sencilla y rápida de aprender JAVA, observando y deduciendo cómo se comporta el lenguaje a través de ejemplos prácticos.

jueves, 11 de diciembre de 2014

Red neuronal artificial (II). Generando estructura de red neuronal aleatoria condicionada.

La siguiente fase y teniendo en cuenta el post anterior creamos nuestra primera estructura de red neuronal aleatoria condicionada. Decimos que es condicionada porque hay más probabilidades de que haya más enlaces de entre neuronas cercanas  que de entre neuronas alejadas.


Ejemplo:

* Propiedades de la red neuronal:

Número de neuronas = 9
Número de enlaces usados = 17
Enlaces aleatorios condicionados por una curva cotangente.


* Posible resultado (aleatorio condicionado):

SALIDAS:
neuroS[0]: [0, 1]
neuroS[1]: [1, 6]
neuroS[2]: [2]
neuroS[3]: [3]
neuroS[4]: [3]
neuroS[5]: [5, 6, 7]
neuroS[6]: [4, 6]
neuroS[7]: [2, 5, 7, 8]
neuroS[8]: [8]

ENTRADAS:
neuroE[0]: [0]
neuroE[1]: [0, 1]
neuroE[2]: [2, 7]
neuroE[3]: [3, 4]
neuroE[4]: [6]
neuroE[5]: [5, 7]
neuroE[6]: [1, 5, 6]
neuroE[7]: [5, 7]
neuroE[8]: [7, 8]


Esquema visual:


























Código 1: (RedNeuronal1.java):

package redneuronal1;

import java.util.ArrayList;
import java.util.Random;

public class RedNeuronal1 {

    public static void main(String[] args) {

        int nNeuronas = 50;
        int nEnlaces = 500;

        ArrayList<Integer>[] neuroS = new ArrayList[nNeuronas];
        ArrayList<Integer>[] neuroE = new ArrayList[nNeuronas];

        AleatoriedadCondicionada ac = new AleatoriedadCondicionada();
        Random r1 = new Random();
        neuroS[0] = new ArrayList();
        int nSalidas;
        int neurona;

        //Crea red neuronal aleatoria
        for (int i = 0; i < neuroS.length; i++) {
            neuroS[i] = new ArrayList();
            neuroE[i] = new ArrayList();
            nSalidas = r1.nextInt(nEnlaces / nNeuronas) + 1;
            for (int j = 0; j < nSalidas; j++) {

                neurona = r1.nextInt(nNeuronas);

                //Aplicación de la Aleatoriedad Condicionada
                neurona = (int) Math.rint(ac.getSalida(nNeuronas, neurona));
                if (r1.nextBoolean()) {
                    //Conexion hacia delante
                    neurona = i + neurona;
                    if (neurona > nNeuronas) {
                        neurona = neurona - nNeuronas;
                    }
                } else {
                    //Conexion hacia atrás
                    neurona = i - neurona;
                    if (neurona < 0) {
                        neurona = neurona + nNeuronas;
                    }
                }
                //evita repeticiones
                if (neuroS[i].contains(neurona) == false) { 
                    neuroS[i].add(neurona); //salida
                }
            }
        }

        //Traspaso entradas a partir de las salidas
        for (int i = 0; i < neuroS.length; i++) {
            for (int j = 0; j < neuroE.length; j++) {
                if (neuroS[i].contains(j)) {
                    neuroE[j].add(i);
                }
            }
        }

        //mostrar resultados
        for (int i = 0; i < neuroS.length; i++) {
            System.out.println("neuroS[" + i + "]: " + neuroS[i].toString());
        }
        System.out.println("\n");
        for (int i = 0; i < neuroE.length; i++) {
            System.out.println("neuroE[" + i + "]: " + neuroE[i].toString());
        }

        //Recuento número de enlaces usados
        int nSinapsis = 0;
        for (int i = 0; i < neuroS.length; i++) {
            nSinapsis += neuroS[i].size();
        }
        System.out.println("\nNúmero de enlaces: " + nSinapsis);

    }

}


Código 2: (AleatoriedadCondicionada.java):

package redneuronal1;

public class AleatoriedadCondicionada {
    double c = Math.atan(10);
    double entrada, salida;

    public double getSalida(double nNeuro, double num) {
        entrada = num * 100 / nNeuro;       
        salida = (Math.atan((entrada / 10) - 10) * 100 / c) + 100;        
        salida = salida * nNeuro / 100;   
        return salida; 
    }

}


Resultado:

neuroS[0]: [9, 0, 30, 47]
neuroS[1]: [6, 1, 2]
neuroS[2]: [5]
neuroS[3]: [0, 35, 26, 8, 2, 6, 7]
neuroS[4]: [4, 49, 13, 6, 27]
neuroS[5]: [23, 41, 5, 49, 47, 6, 2, 35]
neuroS[6]: [29]
neuroS[7]: [7, 1]
neuroS[8]: [16, 10, 26, 1, 0, 8, 13, 9]
neuroS[9]: [9, 14, 10, 0, 6]
neuroS[10]: [22, 10, 12]
neuroS[11]: [13, 5, 29, 11, 17, 18, 16, 9]
neuroS[12]: [44, 30, 1, 13, 9, 11]
neuroS[13]: [14, 21, 15]
neuroS[14]: [14]
neuroS[15]: [38, 10, 14, 16, 15, 13, 7, 8]
neuroS[16]: [23, 14, 16, 21, 13, 17, 15]
neuroS[17]: [19, 47, 23, 16, 7, 14]
neuroS[18]: [18, 5, 36, 31, 17]
neuroS[19]: [18, 46, 3, 22]
neuroS[20]: [47, 40]
neuroS[21]: [21, 16, 23, 20]
neuroS[22]: [19, 25, 16, 6, 15, 21, 27]
neuroS[23]: [17, 22]
neuroS[24]: [22, 23, 21]
neuroS[25]: [33, 26, 23, 22, 25]
neuroS[26]: [24, 26, 25, 23, 34, 27]
neuroS[27]: [40, 24]
neuroS[28]: [1, 31, 23]
neuroS[29]: [25, 26, 33, 17, 28]
neuroS[30]: [33, 30, 35, 27]
neuroS[31]: [49, 38, 33]
neuroS[32]: [32, 29, 30, 34, 39]
neuroS[33]: [30, 49, 35, 44, 31]
neuroS[34]: [34, 36, 29, 47, 40, 48]
neuroS[35]: [34, 35, 42, 17]
neuroS[36]: [36, 40]
neuroS[37]: [5, 37]
neuroS[38]: [41, 8]
neuroS[39]: [7, 38, 37]
neuroS[40]: [40, 43, 41, 39, 42, 35]
neuroS[41]: [40]
neuroS[42]: [40, 49, 5, 36, 24, 6, 38]
neuroS[43]: [49]
neuroS[44]: [45, 49, 21, 41, 43, 48]
neuroS[45]: [47, 1, 45]
neuroS[46]: [35, 34, 3, 46]
neuroS[47]: [48, 1, 5, 37]
neuroS[48]: [48, 2, 44, 37]
neuroS[49]: [41]


neuroE[0]: [0, 3, 8, 9]
neuroE[1]: [1, 7, 8, 12, 28, 45, 47]
neuroE[2]: [1, 3, 5, 48]
neuroE[3]: [19, 46]
neuroE[4]: [4]
neuroE[5]: [2, 5, 11, 18, 37, 42, 47]
neuroE[6]: [1, 3, 4, 5, 9, 22, 42]
neuroE[7]: [3, 7, 15, 17, 39]
neuroE[8]: [3, 8, 15, 38]
neuroE[9]: [0, 8, 9, 11, 12]
neuroE[10]: [8, 9, 10, 15]
neuroE[11]: [11, 12]
neuroE[12]: [10]
neuroE[13]: [4, 8, 11, 12, 15, 16]
neuroE[14]: [9, 13, 14, 15, 16, 17]
neuroE[15]: [13, 15, 16, 22]
neuroE[16]: [8, 11, 15, 16, 17, 21, 22]
neuroE[17]: [11, 16, 18, 23, 29, 35]
neuroE[18]: [11, 18, 19]
neuroE[19]: [17, 22]
neuroE[20]: [21]
neuroE[21]: [13, 16, 21, 22, 24, 44]
neuroE[22]: [10, 19, 23, 24, 25]
neuroE[23]: [5, 16, 17, 21, 24, 25, 26, 28]
neuroE[24]: [26, 27, 42]
neuroE[25]: [22, 25, 26, 29]
neuroE[26]: [3, 8, 25, 26, 29]
neuroE[27]: [4, 22, 26, 30]
neuroE[28]: [29]
neuroE[29]: [6, 11, 32, 34]
neuroE[30]: [0, 12, 30, 32, 33]
neuroE[31]: [18, 28, 33]
neuroE[32]: [32]
neuroE[33]: [25, 29, 30, 31]
neuroE[34]: [26, 32, 34, 35, 46]
neuroE[35]: [3, 5, 30, 33, 35, 40, 46]
neuroE[36]: [18, 34, 36, 42]
neuroE[37]: [37, 39, 47, 48]
neuroE[38]: [15, 31, 39, 42]
neuroE[39]: [32, 40]
neuroE[40]: [20, 27, 34, 36, 40, 41, 42]
neuroE[41]: [5, 38, 40, 44, 49]
neuroE[42]: [35, 40]
neuroE[43]: [40, 44]
neuroE[44]: [12, 33, 48]
neuroE[45]: [44, 45]
neuroE[46]: [19, 46]
neuroE[47]: [0, 5, 17, 20, 34, 45]
neuroE[48]: [34, 44, 47, 48]
neuroE[49]: [4, 5, 31, 33, 42, 43, 44]

Número de enlaces: 207

No hay comentarios:

Publicar un comentario

Con la tecnología de Blogger.