Reading data from a .data file and partitioning the data values into an array of Nodes

StackOverflow https://stackoverflow.com/questions/22890814

  •  28-06-2023
  •  | 
  •  

Question

Background:

Trying to build a neural network which will have 13 input nodes, 8 hidden nodes, and 1 output node

Data That Is Being Read

Data

Code That I Have So Far

NeuralNetwork.java

public class NeuralNetwork {


//Setup an array of Node to store values from a .data file
Node[] dataSet;

//Declare a double[][] array, and randomize the weights of the double array in constructor
protected double[] weights;

//We set a double field named eta equal to 0.05.
protected double eta = 0.05;

private final String comma = ",";
private final String qMarks = "?";
private List<InputNode> input;


//We initialize a constructor which only takes a parameter int n.
public NeuralNetwork(File f){

    List<InputNode> network = new ArrayList<InputNode>();
    this.input = network;

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

        //While there is another line in inFile.
        while (inFile.hasNextLine()){
            //Store that line into String line
            String line = inFile.nextLine();
            //Parition values separated by a comma
            String[] columns = line.split(comma);
            /*System.out.println(Arrays.toString(columns)); //Test code to see length of each column
            * code works and prints out row
            * */
            //create a double array of size columns
            InputNode[] rows = new InputNode[columns.length];
            for (int i = 0; i < columns.length; i++){
                //For each row...
                if (!columns[i].equals(qMarks)){
                    //If the values in each row do not equal "?"
                    //Set rows[i] to the values in column[i]
                    rows[i] = new InputNode(Double.parseDouble(columns[i]));
                }
                else {
                    rows[i] = new InputNode(0);
                }

            }
            input.add(rows);
            }
        System.out.println(input.size());
        //System.out.println(input.size()); //Test code to see how many rows in .data file

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

    //Create Hidden Layer Network
    for (int i = 0; i < input.size(); i++){

    }

}

Node.java

public class Node {
private double number;

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

public double output(){
    return number;
}

}

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) {
    super(value);
    // TODO Auto-generated constructor stub
}
//Create method to initialize input nodes
public void set(double tempValue){
    this.value = tempValue;
}

public double get(Node s){
    return s.output();
}

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

}

}

HiddenLayer.java

public class HiddenLayer extends Node {

protected List<InputNode> nodes = new ArrayList<InputNode>();

public HiddenLayer(double value) {
    super(value);
    // TODO Auto-generated constructor stub
}

//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
     */

    //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 < nodes.size(); i++){
        net += nodes.get(i).output();
    }
    return net;
}


}

Desired Outcome

I would like my NeuralNetwork(File f) constructor partition each number for each row of the data into separate input nodes

For example:

For row 1: [28, 1, 2, 130, 132, 0, 2, 185, 0, 0, ?, ?, ?, 0]

You get InputNodes of:

InputNode[1] = 28
InputNode[2] = 1
InputNode[3] = 2
.....

This is only for each row only, the method should loop over each row. Also I have been having a hard time trying to figure out how to setup the HiddenLayer objects.

For each HiddenNode the network should sum up all the input node values (in each row) and multiply it times the weight of the hidden node, this is then fed into a sigmoid function

Was it helpful?

Solution

This code will loop through the input list and then the array and print the output.

for(int i=0;i < input.size(); i++) {
    System.out.println("For Row " + i + " of file:");    
    InputNode[] = input.get(i);
    for (int j=0;j < InputNode.length; j++ ) {
       System.out.println("   InputNode[ " + j + "] = " + InputNode[j].output());
    } 
}

You can use the same logic in HiddenLayer and do processing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top