Question

Hi everyone I have a neuralnetwork class, its constructor should initialize:

  • a 2-dimensional double array, double[# of rows in .data file][# of numbers in each row]
  • [# of numbers in each row] - 1 InputNodes
  • 2/3 (rounded down) * # of HiddenNodes
  • 1 output node (which is also a HiddenNode object) that takes in all the HiddenNodes

What I am having trouble with

  • Having java create the InputNodes, HiddenNodes, and OutputNode automatically based on the data given, so far I have to manually create the Nodes

The inputnodes would be stored in either an array list or array, and then passed into constructor parameters of a HiddenNode, a HiddenNode can only take parameters of:

HiddenNode(Node[] nodes) Where a Node object is a superclass of HiddenNode and InputNode

Here is the code I have so far for my constructor:

NeuralNetwork.java

/*
 * These are values for the NeuralNetwork Constructor
 */
private final String comma = ",";
private final String qMarks = "?";
private List<InputNode> input = new ArrayList<InputNode>(); // Input Nodes
private List<HiddenNode> hiddenNodeLayer = new ArrayList<HiddenNode>(); // ArrayList of HiddenNode[] arrays
private List<HiddenNode> outputNode = new ArrayList<HiddenNode>();

public NeuralNetwork(File f){


    try {
        @SuppressWarnings("resource")
        Scanner inFile = new Scanner(f);

        int noOfNodes;

        //While there is another line in inFile.
        while (inFile.hasNextLine()){
            //Store that line into String line
            String line = inFile.nextLine();
            //System.out.println(line);//Test code to see what the file looks like
            //Parition values separated by a comma
            String[] numbers = line.split(comma);
            //System.out.println(Arrays.toString(columns)); //Test code to see length of each column
            /*code works and prints out row
            * */
            /*
             * initialize noOfNodes to length of numbers - 1
             */
            noOfNodes = numbers.length - 1;

            //Counter for number of rows in .data file
            int noOfRowsInData = 0;



            //This will count the number of rows in the .data file
            LineNumberReader lnr = new LineNumberReader(new FileReader(f));
            try {
                lnr.skip(Long.MAX_VALUE);
                noOfRowsInData = lnr.getLineNumber();
                lnr.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            data = new double[noOfRowsInData][numbers.length];
            //System.out.println(data[0].length); //Test code works properly, and prints numbers.length

            //Copy over data form the .data file
            for (int i = 0; i < data.length; i++) {
                for (int j = 0; j < data[i].length; j++) {
                    // For each row...
                    if (!numbers[j].equals(qMarks)) {
                        // If the values in each row do not equal "?"
                        // Set rows[i] to the values in column[i]
                        data[i][j] = Double.parseDouble(numbers[j]);
                        //System.out.println(data[i][j]); //Test code to see what's printed here
                    } else {
                        data[i][j] = 0;
                        //System.out.println(data[i][j]); //Test code to see what's printed here
                    }
                }
            }
        }

        //System.out.println(data.length); //See what the length of the data double array is. works
        //Test code to print out the 2-d double array and see what is being stored
//          for(int i = 0; i < data.length; i++) {
//              System.out.println("For Row[" + i + "] of file:"); //Works
//              for (int j = 0; j < data[i].length; j++) {
//                 System.out.println("   data[" + i + "][" + 
//              j + "] = " + data[i][j]); //Works
//              } 
//          }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //Create 13 InputNode Objects initialized to 0.0
    for (int n = 0; n < 13; n++){
        input[n] = new InputNode(0.0);
    }
    //Create 8 HiddenNode objects **HAVING PROBLEMS HERE**
    for (int o = 0; o < 8; o++){
        hiddenNodeLayer.add(new HiddenNode(input));
    }

    System.out.println(hiddenNodeLayer.size() + " HiddenNodes created");
    //Create one output Node, which is a hidden node
    outputNode = new ArrayList<HiddenNode>(1);
    System.out.println(outputNode.size() + " OutputNode created");
}

Update: NullPointerException Error

Getting an error here:

for (int m = 0; m < noOfNodes; m++){
            input[m] = new InputNode(0.0); //NullPointerException error
        }
        System.out.println(input.length);
        //Create 8 HiddenNode objects **HAVING PROBLEMS HERE**
        int noOfHiddenNodes = (int)Math.floor(2.*noOfNodes/3.);
        System.out.println(noOfHiddenNodes);
        for (int o = 0; o < noOfHiddenNodes; o++){
            hiddenNodeLayer.add(new HiddenNode(input));
        }

Found and Corrected Error

I had to initialize input = new InputNode[m] in the for loop, because there were no InputNodes to begin with

for (int m = 0; m < noOfNodes; m++){
            input = new InputNode[m];
        }
        System.out.println(input.length + "");
        //Create 8 HiddenNode objects **HAVING PROBLEMS HERE**
        int noOfHiddenNodes = (int)Math.floor(2.*noOfNodes/3.);
        for (int o = 0; o < noOfHiddenNodes; o++){
            hiddenNodeLayer.add(new HiddenNode(input));
        }
        System.out.println(hiddenNodeLayer.size() + " HiddenNodes created");
        //Create one output Node, which is a hidden node
        outputNode = new ArrayList<HiddenNode>(1);
        System.out.println(outputNode.size() + " OutputNode created");  
Was it helpful?

Solution

Do you mean something like this? (By the way, where is your hiddenNodeLayer defined?)

for (int n = 0; n < noOfNodes; n++){
    input[n] = new InputNode(0.0);
}
//Create 8 HiddenNode objects **HAVING PROBLEMS HERE**
int noOfHiddenNodes = (int)math.floor(2.*noOfNodes/3.);
for (int o = 0; o < noOfHiddenNodes; o++){
    hiddenNodeLayer.add(new HiddenNode(input));
}

UPDATE: given that you have defined input as an ArrayList, you can just push new nodes there in the same was as for hiddenNodeLayer.

private List<InputNode> input = new ArrayList<InputNode>(); // Input Nodes

for (int n = 0; n < noOfNodes; n++){
    input.add(new InputNode(0.0));
}
//Create 8 HiddenNode objects **HAVING PROBLEMS HERE**
int noOfHiddenNodes = (int)math.floor(2.*noOfNodes/3.);
for (int o = 0; o < noOfHiddenNodes; o++){
    hiddenNodeLayer.add(new HiddenNode(input));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top