大家好我有一個neuralnetwork類,它的構造函數應該初始化:遇到問題初始化對象的ArrayList構造函數中
- 二維雙陣列,雙[在.data文件的行#] [每行中的數字的#]
- [每行中的數字]的# - 1個InputNodes
- 2/3(向下)* HiddenNodes
- 1輸出節點(其也是HiddenNode對象)#由需要所有的隱藏節點
什麼我有
- 讓Java創建InputNodes,HiddenNodes和OutputNode自動根據提供的數據麻煩,所以到目前爲止,我有手動創建節點
輸入節點將存儲在數組列表或數組中,然後傳遞給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));
}
實測值和校正的誤差
我不得不初始化輸入=新InputNode [米]中的for循環中,因爲有沒有InputNodes開頭
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");
你的問題是?你提到在某處遇到麻煩,但我看不到在哪裏。 – PakkuDon
我在嘗試創建InputNodes,HiddenNodes和outputNode的最後端遇到了問題。我正在手動創建它們,但是我希望java能夠根據我在帖子開頭 –