gpt4 book ai didi

java - 在构造函数内初始化对象的 ArrayList 时遇到问题

转载 作者:行者123 更新时间:2023-12-01 13:10:32 27 4
gpt4 key购买 nike

大家好,我有一个神经网络类,它的构造函数应该初始化:

  • 二维 double 组,double[.data 文件中的行数][每行中的数字数]
  • [每行中的数字数量] - 1 个输入节点
  • 2/3(向下舍入)* 隐藏节点数
  • 1 个输出节点(也是一个 HiddenNode 对象),接收所有 HiddenNode

我遇到的问题

  • 让java根据给定的数据自动创建InputNodes、HiddenNodes和OutputNode,到目前为止我必须手动创建节点

输入节点将存储在数组列表或数组中,然后传递给 HiddenNode 的构造函数参数,HiddenNode 只能接受以下参数:

HiddenNode(Node[] nodes) Where a Node object is a superclass of HiddenNode and 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],因为没有可用的 InputNode

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");

最佳答案

你的意思是这样的吗? (顺便问一下,你的hiddenNodeLayer在哪里定义的?)

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,您可以像隐藏节点层一样将新节点推送到那里。

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));
}

关于java - 在构造函数内初始化对象的 ArrayList 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22901646/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com