java - Getting 0.0's returned from my object -
i have 3 classes, node, hiddenlayer, , inputnode class.
my hiddenlayer , inputnode classes subclasses of node class , share output() method node class (polymorphism).
my question is: why object returning 0's in hiddenlayer class?
attached 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 can 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 hiddennode class sum input nodes //a specific hidden node public double output(){ /*here implementing following function * sigma(x[i] * weights[i]) = net * pass net hyberbolic tangent function output between -1 1 * pass net activation function in train method of neural network */ //store inputs s temp double array. double[] temparray = new double[input.length]; (int j = 0; j < input.length; j++){ temparray[j] = input[j].output(); system.out.println(temparray[j]); } //setup double sum variable represent net double net = 0; //setup loop loop on input nodes array hidden node (int = 0; < 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 double variable represent holding value inputnode private double value; public inputnode(double value) { } //create method initialize input nodes public void set(double tempvalue){ this.value = tempvalue; } public double get(){ return value; } //override method node class //this method grab sum of input node values. public double output(){ return number; } }
well, far java concerned, you're ever supplying program 0
.
take line:
temparray[j] = input[j].output();
output
0.0, initial value upon construction 0.0
. reason this: output
uses field number
return value, , since there's in code explicitly assign number
value, returning 0.0
.
to fix it, depending on logic or desired operation, should initialize either on inputnode
's construction...
public inputnode(double value) { number = value; }
...or on node
's construction - forcing use constructor described above:
public node(double number) { this.number = number; } public inputnode(double number) { super(number); }
Comments
Post a Comment