Untitled

mail@pastecode.io avatar
unknown
java
a month ago
5.2 kB
23
Indexable
Never
import java.util.Arrays;
import java.util.Random;

public class Main {
    public static void main(String[] args) {

        double[][] inputArray = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
        int[] outputArray = {0, 1, 1, 0};

        Random random = new Random();

        double[][] firstLayerWeights = {
                {random.nextDouble() * 2 - 1, random.nextDouble() * 2 - 1},
                {random.nextDouble() * 2 - 1, random.nextDouble() * 2 - 1},
                {random.nextDouble() * 2 - 1, random.nextDouble() * 2 - 1}
        };

        double[] secondLayerWeights = {random.nextDouble() * 2 - 1, random.nextDouble() * 2 - 1, random.nextDouble() * 2 - 1};

        double[][] oldDiffFirstLayerWeights = {{0, 0}, {0, 0}, {0, 0}};
        double[] oldDiffSecondLayerWeights = {0, 0, 0};

        double learningRate = 0.65;
        double momentum = 0.35;
        int epochs = 11000;

        double u1, u2, u3, v1, v2, v3, delta1, delta2, delta3;

        double[][] diffFirstLayerWeights = new double[3][2];
        double[] diffSecondLayerWeights = new double[3];

        System.out.println("Sieć przed treningiem:");
        for (int i = 0; i < 4; i++) {
            u1 = firstLayerWeights[0][0] * inputArray[i][0] + firstLayerWeights[1][0] * inputArray[i][1] + firstLayerWeights[2][0];
            u2 = firstLayerWeights[0][1] * inputArray[i][0] + firstLayerWeights[1][1] * inputArray[i][1] + firstLayerWeights[2][1];
            u3 = Math.abs((u1 * secondLayerWeights[0] + u2 * secondLayerWeights[1] + secondLayerWeights[2])/2);
            System.out.println("Wejście: " + Arrays.toString(inputArray[i]) + ", Wyjście przed aktywacją: " + u3);
        }

        for (int iteration = 0; iteration < epochs; iteration++) {
            u1 = firstLayerWeights[0][0] * inputArray[iteration % 4][0] + firstLayerWeights[1][0] * inputArray[iteration % 4][1] + firstLayerWeights[2][0];
            u2 = firstLayerWeights[0][1] * inputArray[iteration % 4][0] + firstLayerWeights[1][1] * inputArray[iteration % 4][1] + firstLayerWeights[2][1];

            v1 = 1 / (1 + Math.exp(-u1));
            v2 = 1 / (1 + Math.exp(-u2));

            u3 = v1 * secondLayerWeights[0] + v2 * secondLayerWeights[1] + secondLayerWeights[2];
            v3 = 1 / (1 + Math.exp(-u3));

            delta3 = (outputArray[iteration % 4] - v3) * v3 * (1 - v3);

            diffSecondLayerWeights[0] = learningRate * delta3 * v1;
            diffSecondLayerWeights[1] = learningRate * delta3 * v2;
            diffSecondLayerWeights[2] = learningRate * delta3 * 1;

            delta1 = delta3 * secondLayerWeights[0] * v1 * (1 - v1);
            delta2 = delta3 * secondLayerWeights[1] * v2 * (1 - v2);

            diffFirstLayerWeights[0][0] = learningRate * delta1 * inputArray[iteration % 4][0];
            diffFirstLayerWeights[1][0] = learningRate * delta1 * inputArray[iteration % 4][1];
            diffFirstLayerWeights[2][0] = learningRate * delta1 * 1;
            diffFirstLayerWeights[0][1] = learningRate * delta2 * inputArray[iteration % 4][0];
            diffFirstLayerWeights[1][1] = learningRate * delta2 * inputArray[iteration % 4][1];
            diffFirstLayerWeights[2][1] = learningRate * delta2 * 1;

            secondLayerWeights[0] += diffSecondLayerWeights[0] + oldDiffSecondLayerWeights[0] * momentum;
            secondLayerWeights[1] += diffSecondLayerWeights[1] + oldDiffSecondLayerWeights[1] * momentum;
            secondLayerWeights[2] += diffSecondLayerWeights[2] + oldDiffSecondLayerWeights[2] * momentum;

            oldDiffSecondLayerWeights = diffSecondLayerWeights;

            firstLayerWeights[0][0] += diffFirstLayerWeights[0][0] + oldDiffFirstLayerWeights[0][0] * momentum;
            firstLayerWeights[1][0] += diffFirstLayerWeights[1][0] + oldDiffFirstLayerWeights[1][0] * momentum;
            firstLayerWeights[2][0] += diffFirstLayerWeights[2][0] + oldDiffFirstLayerWeights[2][0] * momentum;
            firstLayerWeights[0][1] += diffFirstLayerWeights[0][1] + oldDiffFirstLayerWeights[0][1] * momentum;
            firstLayerWeights[1][1] += diffFirstLayerWeights[1][1] + oldDiffFirstLayerWeights[1][1] * momentum;
            firstLayerWeights[2][1] += diffFirstLayerWeights[2][1] + oldDiffFirstLayerWeights[2][1] * momentum;

            oldDiffFirstLayerWeights = diffFirstLayerWeights;
        }

        System.out.println("\nSieć po treningu:");
        for (int i = 0; i < 4; i++) {
            u1 = firstLayerWeights[0][0] * inputArray[i % 4][0] + firstLayerWeights[1][0] * inputArray[i % 4][1] + firstLayerWeights[2][0];
            u2 = firstLayerWeights[0][1] * inputArray[i % 4][0] + firstLayerWeights[1][1] * inputArray[i % 4][1] + firstLayerWeights[2][1];

            v1 = 1 / (1 + Math.exp(-u1));
            v2 = 1 / (1 + Math.exp(-u2));

            u3 = v1 * secondLayerWeights[0] + v2 * secondLayerWeights[1] + secondLayerWeights[2];
            v3 = 1 / (1 + Math.exp(-u3));

            System.out.println("Wejście: " + Arrays.toString(inputArray[i]) + ", Wyjście: " + v3);
        }
    }
}
Leave a Comment