Sākums > Programmēšana > Neirālais tīkls (programmēšana Java platformā)

Neirālais tīkls (programmēšana Java platformā)


Image via Wikipedia

Piedāvāju interesentiem savu pamatkodu neirālā tīkla implementācijai Java SE6,

package neuralnetwork;

public class NNx {

    public NNx() {
        w = new float[2];
        x = new float[4][2];
        s = new float[4][2];
        t = new float[4];

    }

    private void Initialization() {
        System.out.println("Giving input values");
        System.out.println("");
        //i integrated AND logical statement
        //i dont know why, but it didnt worked till i defined it for t[0],t[1],t[2],t[3]
        s[0][0] = 0;
        s[0][1] = 0;
        t[0] = -1;
        s[1][0] = 1;
        s[1][1] = 0;
        t[1] = -1;
        s[2][0] = 0;
        s[2][1] = 1;
        t[2] = -1;
        s[3][0] = 1;
        s[3][1] = 1;
        t[3] = 1;
        for (int i = 0; i < w.length; i++) {
            w[i] = (float)Math.random();
        }

        b = 1;
    }

    private void Training() {
        float sum = 0;

        for (int i = 0; i < x.length; i++) {
            for (int j = 0; j < x[i].length; j++) {
                x[i][j] = s[i][j];

            }

        }
        System.out.println("Network training in progress :");
        System.out.println("");

        for (iteration = 0; iteration < max_iteration; iteration++) {

            System.out.println("step " + (iteration + 1) + " done");
            for (int i = 0; i < x.length; i++) {

                sum = 0;
                for (int j = 0; j < x[i].length; j++) {
                    sum += x[i][j] * w[j];
                }
                y_in = b + sum;
                if (y_in >= threshold) {
                    y = 1;
                } else if (y_in < threshold) {
                    y = -1;
                }

                if (y != t[i]) {
                    for (int j = 0; j < w.length; j++) {
                        w[j] = w[j] + (learn_rate * t[i] * x[i][j]);
                    }
                    b = b + learn_rate * t[i];

                }
            }
        }
        System.out.println("");
        System.out.println("Solution has been found");
        System.out.println("");
    }

    private void Results() {
        float sum = 0;
        float[] res_output = new float[4];

        for (int i = 0; i < x.length; i++) {
            for (int j = 0; j < x[i].length; j++) {
                x[i][j] = s[i][j];
            }
        }

        System.out.println("Results : (for checking)");
        for (int i = 0; i < x.length; i++) {
            sum = 0;
            for (int j = 0; j < x[i].length; j++) {
                sum += x[i][j] * w[j];
            }
            y_in = b + sum;
            if (y_in >= threshold) {
                y = 1;
            } else if (y_in < threshold) {
                y = -1;
            }
            if (y == -1) {
                res_output[i] = 0;
            } else {
                res_output[i] = 1;
            }

        }
        System.out.println("");
        System.out.println("0 AND 0 = " + res_output[0]);
        System.out.println("0 AND 1 = " + res_output[1]);
        System.out.println("1 AND 0 = " + res_output[2]);
        System.out.println("1 AND 1 = " + res_output[3]);
    }

    public void RunPerceptron() {
        learn_rate = (float) 0.5;
        threshold = (float) 0.2;
        max_iteration = 10;
        Initialization();
        Training();
        Results();
    }

    public static void main(String[] args) {
        NNx perceptron = new NNx();
        perceptron.RunPerceptron();
    }
    private float[] w;
    private float b;
    private float[][] x;
    private float[][] s;
    private float y;
    private float learn_rate;
    private float threshold;
    private float[] t;
    private float y_in;
    private int iteration;
    private int max_iteration;
}

Tātad tīkls atrisina elementāru AND funkciju izmantojot leraning algoritmu un single – layer elementus. PM ja ir kādi jautājumi, kodu var droši pārizmantot ;)

Reblog this post [with Zemanta]
Advertisements
  1. Vēl nav komentāru.
  1. No trackbacks yet.

Komentēt

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Mainīt )

Twitter picture

You are commenting using your Twitter account. Log Out / Mainīt )

Facebook photo

You are commenting using your Facebook account. Log Out / Mainīt )

Google+ photo

You are commenting using your Google+ account. Log Out / Mainīt )

Connecting to %s

%d bloggers like this: