gpt4 book ai didi

java - 显示 NullPointerException 的自定义类数组

转载 作者:行者123 更新时间:2023-12-01 09:11:12 25 4
gpt4 key购买 nike

我通过运行以下代码收到 NullPointerException:

主要:

public class NeuralXOR {

public static void main(String[] args) {
double[] inputs = {1.0, 0.0};
NeuralNetwork network = new NeuralNetwork(2, 1, 3, 0.7, 0.9);
}

}

神经网络类:公共(public)类神经网络{

Node[] inputNodes;
Node[] hiddenNodes;
Node[] outputNodes;

Layer[] layers;

int noOfInputNodes;
int noOfOutputNodes;
int noOfLayers = 3;

public NeuralNetwork(int noOfInputNodes, int noOfOutputNodes, int noOfHiddenNodes, double learningRate, double momentum) {

this.outputNodes = new Node[noOfOutputNodes];
for (int i = 0; i < outputNodes.length; i++) {
outputNodes[i] = new Node(i);
}

this.hiddenNodes = new Node[noOfHiddenNodes];
for (int i = 0; i < hiddenNodes.length; i++) {
hiddenNodes[i] = new Node(i);
}

this.inputNodes = new Node[noOfInputNodes];
for (int i = 0; i < hiddenNodes.length; i++) {
hiddenNodes[i] = new Node(i);
}

layers[0] = new Layer(inputNodes);
layers[1] = new Layer(hiddenNodes);
layers[2] = new Layer(outputNodes);

}

public void setInputs(double[] inputs) {
if (inputs.length == this.inputNodes.length) {
System.out.println(inputs.length);
for (int i = 0; i < inputs.length; i++) {
this.inputNodes[i].activation = inputs[i];
}
}
}

public double[] getOutputs() {
double[] outputs = new double[this.outputNodes.length];
for (Node outNode : this.outputNodes) {
outputs[outNode.nodeNumber] = outNode.activation;
}
return outputs;
}

}

类节点{

int nodeNumber;
double sum; // z
double activation; //Output
double deltaNode;
double gradient;


double[] outWeights;

public Node (int nodeNumber) {
this.nodeNumber = nodeNumber;
}

public void setOutWeights(int noOfOutNodes) {
this.outWeights = new double[noOfOutNodes];
for (int i = 0; i < outWeights.length; i++) {
this.outWeights[i] = Math.random();
}
}

public double sigmoid(double x) {
return 1 / (1 + Math.pow(Math.E, -x));
}

}

类层{

double layerBias;
Node[] nodes;

public Layer (Node[] nodes) {
this.nodes = nodes;
}

}

错误点位于“layers[0] = new Layer(inputNodes);”行在神经网络类中。没有显示红色,也没有任何警告...

我认为这是由于缺乏 JVM 如何工作知识而导致的错误。到目前为止,我已经尝试将这些类分离到单独文件中的公共(public)类中,但这没有帮助(没想到会如此)。我还没有尝试过其他任何东西。有什么想法吗?提前致谢!

最佳答案

您需要初始化数组Layerlayers = new Layer[3];

关于java - 显示 NullPointerException 的自定义类数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40913287/

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