Código
package matrix; import java.util.function.BiFunction; import java.util.function.Function; public class Main { double[][] training_set_inputs = new double[][] { { 0, 0, 1 }, { 1, 1, 1 }, { 1, 0, 1 }, { 0, 1, 0 } }; double[][] training_set_outputs = t(new double[][] { { 0, 1, 1, 0 } }); double[][] synaptic_weights = new double[][] { { -0.5 }, { -0.5}, { -0.5} }; for (int c = 0; c < 700_000; c++) { double[][] output = div(1, sum(1, exp(neg(dot(training_set_inputs, synaptic_weights))))); double[][] error = subtract(training_set_outputs,output); double[][] sigder = mul(output,min(1,output)); double[][] adjustment = dot(t( training_set_inputs ),mul(error, sigder)); synaptic_weights= sum(synaptic_weights,adjustment); } print(div(1, sum(1, exp(neg(dot( new double[][] { {1 , 0 , 0 } }, synaptic_weights)))))); } private static void println() { } static double[][] dot(double[][] a, double[][] b) { double[][] result = zero(a.length, b[0].length); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[0].length; j++) { for (int k = 0; k < b[0].length; k++) { result[i][k] += a[i][j] * b[j][k]; } } } return result; } double[][] result = zero(a.length, b[0].length); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[0].length; j++) { result[i][j] = call.apply(a[i][j], b[i][j]); } } return result; } double[][] result = zero(b.length, b[0].length); for (int i = 0; i < b.length; i++) { for (int j = 0; j < b[0].length; j++) { result[i][j] = call.apply(a, b[i][j]); } } return result; } static double[][] operate(double[][] a, Function<Double, Double> call) { double[][] result = zero(a.length, a[0].length); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[0].length; j++) { result[i][j] = call.apply(a[i][j]); } } return result; } static double[][] div(double a, double[][] b) { return operate(a, b, (p, q) -> p / q); } static double[][] sum(double a, double[][] b) { return operate(a, b, (p, q) -> p + q); } static double[][] min(double a, double[][] b) { return operate(a, b, (p, q) -> p - q); } static double[][] div(double[][] a, double[][] b) { return operate(a, b, (p, q) -> p / q); } static double[][] mul(double[][] a, double[][] b) { return operate(a, b, (p, q) -> p * q); } static double[][] mul(double a, double[][] b) { return operate(a, b, (p, q) -> p * q); } static double[][] sum(double[][] a, double[][] b) { return operate(a, b, (p, q) -> p + q); } static double[][] subtract(double[][] a, double[][] b) { return operate(a, b, (p, q) -> p - q); } static double[][] exp(double[][] a) { } static double[][] neg(double[][] a) { return mul(-1, a); } private static double[][] zero(int rows, int cols) { return new double[rows][cols]; } static double[][] expand(double[][] a, double value) { int firstDimension = a.length; int secondDimensio = a[0].length; double [][]result = zero(firstDimension, secondDimensio); for (int i = 0; i < firstDimension; i++) { for (int j = 0; j < secondDimensio; j++) { result[i][j] = value; } } return result; } static double[][] t(double[][] a) { int firstDimension = a.length; int secondDimensio = a[0].length; double[][] result = new double[secondDimensio][firstDimension]; for (int i = 0; i < firstDimension; i++) { for (int j = 0; j < secondDimensio; j++) { result[j][i] = a[i][j]; } } return result; } static void print(double[][] a) { int firstDimension = a.length; int secondDimensio = a[0].length; for (int i = 0; i < firstDimension; i++) { for (int j = 0; j < secondDimensio; j++) { } println(); } } }