Thats my first time coding a neural network and it seems to work but I'm not sure if everything is made correctly (The Neural Network not the code).
I would be happy if someone could check the code for me and tell me if something doesn't make sense.
here's the code:
(Please tell me if my English doesn't make sense, I'm German and I'm not really good at English grammar)package NN; import java.util.Random; public class FFBackprop { private double[] weightsH; private double[] weightsO; private double[] Hidden; private double[] Output; private double[] SumH; private double[] SumO; private double[] Input; private double[] biasesH; private double[] biasesO; private double lr; private final Random rnd; public FFBackprop(int layerIN, int layerHID, int layerOUT, double learningRate) { rnd = new Random(); weightsH = new double[layerIN*layerHID]; weightsO = new double[layerHID*layerOUT]; Hidden = new double[layerHID]; Output = new double[layerOUT]; SumH = new double[layerHID]; SumO = new double[layerOUT]; biasesH = new double[layerHID]; biasesO = new double[layerOUT]; lr = learningRate; resetWeights(); } public void resetWeights() { for(int i = 0; i<weightsH.length; i++) { weightsH[i] = rnd.nextDouble(); } for(int i = 0; i<weightsO.length; i++) { weightsO[i] = rnd.nextDouble(); } for(int i = 0; i<biasesH.length; i++) { biasesH[i] = rnd.nextDouble(); } for(int i = 0; i<biasesO.length; i++) { biasesO[i] = rnd.nextDouble(); } } private double Func(double x) { return 1/(1+Math.exp(-x)); } public double[] ff(double[] input) { this.Input = input; for(int i = 0; i<Hidden.length; i++) { SumH[i]=biasesH[i]; for(int j = 0; j<input.length; j++) { SumH[i] += weightsH[j+i*input.length]*input[j]; } Hidden[i] = Func(SumH[i]); } for(int i = 0; i<Output.length; i++) { SumO[i]=biasesO[i]; for(int j = 0; j<Hidden.length; j++) { SumO[i] += weightsO[j+i*Hidden.length]*Hidden[j]; } Output[i] = Func(SumO[i]); } return Output; } public void backprop(double[] t) { double[] dWeightsO = new double[weightsO.length]; double[] partialErrorArr = new double[Output.length]; double[] partialOutputArr = new double[Output.length]; for(int i = 0; i<Output.length; i++) { double partialError = Output[i]-t[i]; double partialOutput = Output[i]*(1-Output[i]); partialErrorArr[i]=partialError; partialOutputArr[i]=partialOutput; for(int j = 0; j<Hidden.length; j++) { double partialSum = Hidden[j]; double delta = partialError*partialOutput*partialSum; dWeightsO[j+i*Hidden.length] = delta; } } double[] partialHiddenOut = new double[weightsO.length]; for(int i = 0; i<Hidden.length; i++) { for(int j = 0; j<Output.length; j++) { partialHiddenOut[j+i*Output.length] = weightsO[i+j*Output.length]; } } double[] partialHidden = new double[Hidden.length]; for(int i = 0; i<Hidden.length; i++) { double sum = 0; for(int j = 0; j<Output.length; j++) { sum += partialErrorArr[j]*partialOutputArr[j]*partialHiddenOut[j+i*Output.length]; } partialHidden[i]=sum; } double[] dWeightsH = new double[weightsH.length]; for(int i = 0; i<Hidden.length; i++) { for(int j = 0; j<Input.length; j++) { dWeightsH[j+i*Input.length] = partialHidden[i]*Hidden[i]*(1-Hidden[i])*Input[j]; } } double[] dBiasesO = new double[biasesO.length]; for(int i = 0; i<biasesO.length; i++) { biasesO[i] = partialErrorArr[i]*partialOutputArr[i]; } double[] dBiasesH = new double[biasesH.length]; for(int i = 0; i<biasesH.length; i++) { dBiasesH[i] = partialHidden[i]*Hidden[i]*(1-Hidden[i]); } for(int i = 0; i<biasesO.length; i++) { biasesO[i] -= lr*dBiasesO[i]; } for(int i = 0; i<biasesH.length; i++) { biasesH[i] -= lr*dBiasesH[i]; } for(int i = 0; i<weightsO.length; i++) { weightsO[i] -= lr*dWeightsO[i]; } for(int i = 0; i<weightsH.length; i++) { weightsH[i] -= lr*dWeightsH[i]; } } /*public static void main(String[] args) { FFBackprop ffp = new FFBackprop(2, 3, 1, 0.5); double out = ffp.ff(new double[] {1,1})[0]; System.out.println(out); for(int i = 0; i<10000; i++) { ffp.ff(new double[] {1,1}); ffp.backprop(new double[] {0}); } out = ffp.ff(new double[] {1,1})[0]; System.out.println(out); }*/ }