Reputation: 53
I am trying to setup a very simple neural network, which will use a multilayered perceptron approach.
I have 4 classes:
Node: which only has a constructor with nothing in it, and an output method (which I still have to define)
InputNode: will read data from a .data file and setup input nodes (in double types) according to data
HiddenLayer: This layer will be 2/3 the size of the length of each Input ArrayList
NeuralNetwork: This will be the main execution class, where I will setup a 2-d double array with randomized weights. Those weights will be multiplied by the input data to get a sum. There will be an error and a train method present here.
My question pertains to the InputNode and the HiddenLayer class. I want the InputNode class to read data from a .data file and setup each row from the .data into 13 input nodes. The last number of each row in the .data file represents a predictive number of 0-3: negative for something or above 3: positive for something
Example:
If I have 13 inputs, then I should have around 8 hidden nodes. Each hidden node will take in all the input nodes and multiply the input value of each input node times the weight of that specific hidden node and come up with either a 1 or -1 value.
How would I go about setting this up? Having some trouble with this since I had originally setup my NeuralNetwork class to setup an input ArrayList, but now have decided that it is better to create separate classes.
Here is what an example dataset would look like:
Here is the code for my Node Class:
public class Node {
public Node(){
}
public void output(){
//Don't know what to put here? Guess it can be modified for hiddenlayer and
// Node classes
}
}
Here is the code for my InputNode Class:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class InputNode extends Node {
//declare arraylist for input nodes
private List<Node> inputs = new ArrayList<Node>();
//Constuctor for input nodes
public InputNode(File f){
List<Node> tempInput = new ArrayList<Node>();
this.inputs = tempInput;
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(",");
/*System.out.println(columns.length); //Test code to see length of each column
* code works and prints 14
* */
//create a double array of size columns
double[] rows = new double[columns.length];
for (int i = 0; i < columns.length; i++){
//For each row...
if (!columns[i].equals("?")){
//If the values in each row do not equal "?"
//Set rows[i] to the values in column[i]
rows[i] = Double.parseDouble(columns[i]);
}
else {
rows[i] = 0;
}
}
inputs.add(rows);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Upvotes: 1
Views: 107
Reputation: 7652
I thought point of this question is how to identify '?' marks from mixed String array. I am not sure this is what you want but I suggest one possibility.
Follow is code to exchange '?' marks to double '0.0'.
I have changed some codes of yours. - change ArrayList Generic from 'Node' to 'double[]', since it can not assign double array(which means 'rows' in your code) to Object ArrayList. - add a try/catch phrase, because it can not cast literal signs to Integer or Double. so first we need to sort out this string is literal or numeral.
public class InputNode {
private final String REGEX = ",";
private final String Q_MARKS = "?";
private List<double[]> input;
@SuppressWarnings("resource")
public InputNode(File f){
this.input = new ArrayList<double[]>();
try{
Scanner inFile = new Scanner(f);
int cntLines = 0;
while(inFile.hasNextLine()){
String line = inFile.nextLine();
String columns[] = line.split(REGEX);
double rows[] = new double[columns.length];
for(int i = 0; i < columns.length; i++){
//if String is '?' then 0.0, else parseDouble
if(!columns[i].equals(Q_MARKS)){
try{
rows[i] = Double.parseDouble(columns[i]);
}catch(Exception e){
System.out.println("data is not '?'");
continue;
}
}else{
System.out.println("data is '?' mark");
rows[i] = 0;
}
}
input.add(rows);
cntLines++;
System.out.println("num : " + cntLines + ", value : " + Arrays.toString(rows));
}
System.out.println("total Lines read : " + cntLines);
}catch(Exception e){
e.printStackTrace();
}
}
};
My test a.data file is
1,2,3,4,5,6,7,8,9,10,11,12,3,0
1,2,3,4,5,6,7,8,9,10,?,12,6,0
1,2,3,4,5,6,7,8,9,10,?,?,?,0
1,2,3,4,5,6,7,8,9,10,?,?,?,0
1,2,3,4,5,6,7,8,9,10,?,?,?,0
1,2,3,4,5,6,7,8,9,10,?,?,?,1
1,2,3,4,5,6,7,18,19,10,?,?,?,0
1,2,3,4,5,6,7,8,9,10,?,?,3,0
1,2,3,4,5,6,7,8,9,10,?,?,?,1
1,2,3,4,5,6,7,8,9,10,?,?,?,0
1,2,3,4,5,6,7,8,9,10,?,?,2,0
1,2,3,4,5,6,7,8,9,10,?,?,?,0
1,2,3,4,5,6,7,8,9,10,?,?,?,0
And finally output was :
Riddles +_+
num : 1, value : [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 3.0, 0.0]
data is '?' mark
num : 2, value : [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 0.0, 12.0, 6.0, 0.0]
data is '?' mark
data is '?' mark
data is '?' mark
num : 3, value : [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 0.0, 0.0, 0.0, 0.0]
data is '?' mark
data is '?' mark
data is '?' mark
num : 4, value : [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 0.0, 0.0, 0.0, 0.0]
data is '?' mark
data is '?' mark
data is '?' mark
num : 5, value : [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 0.0, 0.0, 0.0, 0.0]
data is '?' mark
data is '?' mark
data is '?' mark
num : 6, value : [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 0.0, 0.0, 0.0, 1.0]
data is '?' mark
data is '?' mark
data is '?' mark
num : 7, value : [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 18.0, 19.0, 10.0, 0.0, 0.0, 0.0, 0.0]
data is '?' mark
data is '?' mark
num : 8, value : [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 0.0, 0.0, 3.0, 0.0]
data is '?' mark
data is '?' mark
data is '?' mark
num : 9, value : [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 0.0, 0.0, 0.0, 1.0]
data is '?' mark
data is '?' mark
data is '?' mark
num : 10, value : [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 0.0, 0.0, 0.0, 0.0]
data is '?' mark
data is '?' mark
num : 11, value : [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 0.0, 0.0, 2.0, 0.0]
data is '?' mark
data is '?' mark
data is '?' mark
num : 12, value : [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 0.0, 0.0, 0.0, 0.0]
data is '?' mark
data is '?' mark
data is '?' mark
num : 13, value : [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 0.0, 0.0, 0.0, 0.0]
total Lines read : 13
Exchange all '?' marks to '0.0', you can math with that:D
Upvotes: 2