Frage

I have 3 classes, Node, HiddenLayer, and InputNode class.

My HiddenLayer and InputNode classes are subclasses of the Node class and they would share the output() method from the Node class (polymorphism).

My question is: why is my object returning 0's in the HiddenLayer class?

Attached is my code:

Node.java

public class Node {
    protected double number;

    public double output(){
        return number;
    }
}

HiddenLayer.java

public class HiddenLayer extends Node {

    protected Node[] input;

    public HiddenLayer(Node[] nodes) {
        this.input = nodes;
    }

    //Some activation functions which can be called upon.
    class ActivationFunction {

        //Sigmoid activation function
        public double sigmoid(double x) {
            return (1.0 / (1 + Math.pow(Math.E, -x)));
        }

        public double deriveSigmoid(double d){
        return d * (1.0 - d);
        }

        // Hyperbolic Tan Activation Function
        public double hyperTanFunction(double x) {
            return (Math.pow(Math.E, x) - Math.pow(Math.E, -x)) / (Math.pow(Math.E, x) + Math.pow(Math.E, -x));
        }

        public double deriveHyperTanFunction(double d){
            return (1.0 - Math.pow(hyperTanFunction(d), 2.0));
        }
    }

    //Output method for the HiddenNode class which will sum up all the input nodes for
    //a specific hidden node
    public double output(){

        /*Here we will be implementing the following function
         *  Sigma(x[i] * weights[i]) = net
         *  Pass net into the hyberbolic tangent function to get an output between -1 to 1
         *  We will pass net into the activation function in the train method of Neural Network
         */
        //Store inputs into s temp double array.
        double[] tempArray = new double[input.length];
        for (int j = 0; j < input.length; j++){
            tempArray[j] = input[j].output();
            System.out.println(tempArray[j]);
        }
        //Setup a double sum variable to represent net
        double net = 0;
        //Setup for loop to loop over input nodes array for a hidden node
        for (int i = 0; i < tempArray.length; i++){
            net = net + tempArray[i];
        }

        return net;
    }


    public static void main(String[] args) {

        InputNode node1 = new InputNode(28);
        InputNode node2 = new InputNode(0);
        InputNode node3 = new InputNode(165);
        InputNode node4 = new InputNode(30);
        InputNode node5 = new InputNode(0);
        InputNode node6 = new InputNode(0);
        InputNode node7 = new InputNode(0);

        InputNode[] nodes = {node1, node2, node3, node4, node5, node6, node7};

        HiddenLayer h1 = new HiddenLayer(nodes);

        h1.output();
    }
}

InputNode.java

public class InputNode extends Node {
    //Declare a double variable to represent the holding value for InputNode
    private double value;

    public InputNode(double value) {

    }
    //Create method to initialize input nodes
    public void set(double tempValue){
        this.value = tempValue;
    }

    public double get(){
        return value;
    }

    //Override method from Node class
    //This method will grab the sum of all input node values.
    public double output(){
        return number;

    }
}
War es hilfreich?

Lösung

Well, as far as Java is concerned, you're only ever supplying the program with 0.

Take this line:

tempArray[j] = input[j].output();

output will be 0.0, as its initial value upon construction will be 0.0. The reason for this: output uses the field number to return its value, and since there's nowhere in your code that you explicitly assign number to a value, it will always be returning 0.0.

To fix it, depending on your logic or desired operation, you should initialize it either on InputNode's construction...

public InputNode(double value) {
    number = value;
}

...or on Node's construction - forcing you to use the constructor described above:

public Node(double number) {
    this.number = number;
}

public InputNode(double number) {
    super(number);
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top