大家好我有一个神经网络课,它的构造函数应该初始化:
我遇到的问题
输入节点将存储在数组列表或数组中,然后传递给HiddenNode的构造函数参数,HiddenNode只能接受以下参数:
的超类
HiddenNode(Node[] nodes)
Node对象是HiddenNode和InputNode
这是我到目前为止构造函数的代码:
的 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");
}
更新:NullPointerException错误
在此处收到错误:
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));
}
找到并纠正错误
我必须在for循环中初始化input = new InputNode [m],因为没有以
开头的InputNodesfor (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");
答案 0 :(得分:1)
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));
}
更新:假设您已将输入定义为ArrayList,则可以将新节点推送到与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));
}