-1

This is my code

import java.lang.Math;

public class Demo {

    // inputs are declared
    private int x[]={1,0,1,0};
    //weights are declared
    private double w[]={0.4,0.6,0.7,0.8};
    private double temp;

    private double z=0;
    private int desiredOutput=0;

    private double actualOutput=0;
    private double error=0;
    private double definedValue=0.004;
    private double weightChange[]={0,0,0,0};
    private double learningRate=0.7;

    public void calculate(){
        System.out.println("hi");

        for(int i=0; i<w.length; i++){
            temp=0;
            temp=x[i]*w[i];
            z+=temp;
            System.out.println("Z value"+z);
        }

        actualOutput=1/(1+Math.exp(-z));

        System.out.println("actualOutput "+actualOutput);

        if(actualOutput!=desiredOutput){
            calculateError();
        }
        else{
            printWeights();
        }
    }
    public void calculateError(){
        error=0.5*(desiredOutput-actualOutput)*(desiredOutput-actualOutput);
        System.out.println("error "+error);
        checkingCondition();
        //error=0.003;
    }
    public void checkingCondition(){
        if(error<definedValue){
            printWeights();
        } else {
            balanceWeights();
        }
    }

    public void balanceWeights(){
        for(int i=0;i<w.length;i++){

            weightChange[i]=((-learningRate)*(desiredOutput-actualOutput)*actualOutput*(1-actualOutput)*x[i]);
            System.out.println(weightChange[i]);
            w[i]=w[i]+weightChange[i];
        }
        calculate();
    }

    public void printWeights(){
        System.out.print("The balanced weights are:");

        for(int i=0;i<4;i++){
            System.out.print(w[i]+"\t");
        }    
    }
}

this is my main class

    public class Main {

    public static void main(String[] args) {
        Demo object= new Demo();
        object.calculate();
    }

}

but without printing out "hi" which I used to check the running path on the first method that runs, compiler executes other methods called with in the first call method, and print "hi" on the next call

this is the initial part of output I get

Z value1543.3571456014363
Z value1544.2242990218328
Z value1544.2242990218328
actualOutput 1.0
error 0.5
0.0
0.0
0.0
0.0
hi
Z value1544.7914524422295
Z value1544.7914524422295
Z value1545.658605862626
Z value1545.658605862626
actualOutput 1.0
error 0.5
0.0
0.0
0.0
0.0
hi
Z value1546.2257592830226
Z value1546.2257592830226
Z value1547.0929127034192
Z value1547.0929127034192
actualOutput 1.0
error 0.5
0.0
0.0
0.0
0.0
hi
Z value1547.66006612

at at final part of the output these errors are given

hi
Z value1861.77326425752
Z value1861.77326425752
Z value1862.6404176779165
Z value1862.6404176779165
actualOutput 1.0
Exception in thread "main" java.lang.StackOverflowError
    at sun.misc.FloatingDecimal$BinaryToASCIIBuffer.dtoa(FloatingDecimal.java:431)
    at sun.misc.FloatingDecimal$BinaryToASCIIBuffer.access$100(FloatingDecimal.java:259)
    at sun.misc.FloatingDecimal.getBinaryToASCIIConverter(FloatingDecimal.java:1785)
    at sun.misc.FloatingDecimal.getBinaryToASCIIConverter(FloatingDecimal.java:1738)
    at sun.misc.FloatingDecimal.appendTo(FloatingDecimal.java:89)
    at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:709)
    at java.lang.StringBuilder.append(StringBuilder.java:226)
    at Neural4.Demo.calculateError(Demo.java:55)
    at Neural4.Demo.calculate(Demo.java:41)
    at Neural4.Demo.balanceWeights(Demo.java:84)
    at Neural4.Demo.checkingCondition(Demo.java:68)
    at Neural4.Demo.calculateError(Demo.java:56)
    at Neural4.Demo.calculate(Demo.java:41)
    at Neural4.Demo.balanceWeights(Demo.java:84)
    at Neural4.Demo.checkingCondition(Demo.java:68)
    at Neural4.Demo.calculateError(Demo.java:56)
    at Neural4.Demo.calculate(Demo.java:41)
    at Neural4.Demo.balanceWeights(Demo.java:84)
    at Neural4.Demo.checkingCondition(Demo.java:68)
    at Neural4.Demo.calculateError(Demo.java:56)
    at Neural4.Demo.calculate(Demo.java:41)
    at Neural4.Demo.balanceWeights(Demo.java:84)
    at Neural4.Demo.checkingCondition(Demo.java:68)
    at Neural4.Demo.calculateError(Demo.java:56)
    at Neural4.Demo.calculate(Demo.java:41)
    at Neural4.Demo.balanceWeights(Demo.java:84)
    at Neural4.Demo.checkingCondition(Demo.java:68)
    at Neural4.Demo.calculateError(Demo.java:56)
    at Neural4.Demo.calculate(Demo.java:41)
    at Neural4.Demo.balanceWeights(Demo.java:84)
    at Neural4.Demo.checkingCondition(Demo.java:68)
    at Neural4.Demo.calculateError(Demo.java:56)
    at Neural4.Demo.calculate(Demo.java:41)
    at Neural4.Demo.balanceWeights(Demo.java:84)

why is this happening, when the variable desiredOutput is set to 1 the code runs fine, problem comes when it is set to 0

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
Kasun Siyambalapitiya
  • 3,956
  • 8
  • 38
  • 58

2 Answers2

1

Please check the value/calculation/updating of error, it is 0.5 in your output and does not change over time (at least in the presented output). This means, that you will go to calculate(), calculateError(), checkingCondition(), balanceWeights() and calculate() over and over again. This results in the given StackOverflowError.

Stefan Freitag
  • 3,578
  • 3
  • 26
  • 33
  • Thankyou I didnot see that, I solved my problem with the link commented http://stackoverflow.com/questions/1088216/whats-wrong-with-using-to-compare-floats-in-java – Kasun Siyambalapitiya Jan 14 '16 at 01:41
0

Balance weight should not call calculate at all. It should be (pseudocode)

set initial weights();
    while(currentError>=required){
       checkConfitions();
       balanceWeigth();
       calculate();
    }

plus comapring nonintegers with == will not work. See What's wrong with using == to compare floats in Java?

Community
  • 1
  • 1
Antoniossss
  • 31,590
  • 6
  • 57
  • 99