Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Is my Neural Network correct/good?

  1. #1
    Junior Member
    Join Date
    Apr 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Is my Neural Network correct/good?

    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:
    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);
    	}*/
     
    }
    (Please tell me if my English doesn't make sense, I'm German and I'm not really good at English grammar)

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Is my Neural Network correct/good?

    Can you tell us what you trying to do ?
    Whatever you are, be a good one

  3. #3
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Is my Neural Network correct/good?

    Yeah it would be nice to know what you're trying to do.
    Also i did try it, the second part is almost always consistent at around 0.062 - 0.065 but the first part
    is fluctuation from around 0.6 - 0.9.
    "Tick, tack"

Similar Threads

  1. Neural Network Programming - Wrong result after training
    By DarioP in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 18th, 2013, 04:50 PM
  2. Stock Picker and Neural Network
    By didsbub in forum Java Theory & Questions
    Replies: 0
    Last Post: July 29th, 2012, 07:08 PM
  3. Problem in code of artificial neural network
    By aduaitpokhriyal in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 31st, 2011, 07:44 AM
  4. Neural Networks and Prediction?
    By Superstar288 in forum Java Theory & Questions
    Replies: 5
    Last Post: November 28th, 2010, 11:12 AM

Tags for this Thread