am trying to rewrite a program I found to create a neural network the code seems to be very fine (atleast for me) but I keep getting java.lang.NullPointerException error with ihWeights[i][j] = weights[k++];,can't find out what is wrong
我试图重写一个程序,我发现创建一个神经网络代码似乎非常好(至少对我而言)但我一直得到java.lang.NullPointerException错误与ihWeights [i] [j] =权重[k ++] ;,找不出有什么问题
public class Network {
int numInput,numHidden,numOutput;
private double[]InputLayer;
private double[]OutputLayer;
private double[][]ihWeights;
private double[]ihBias;
private double[]ihSums;
private double[] ihOutput;
private double[][]hoWeights;
private double[]hoBias;
private double[]hoSums;
public Network(int i,int h,int o){
this.numInput=i;
this.numHidden=h;
this.numOutput=o;
InputLayer = new double[numInput];
ihWeights = MakeMatrix(numInput, numHidden);
ihBias = new double[numHidden];
ihSums = new double[numHidden];
ihOutput = new double[numHidden];
hoWeights = MakeMatrix(numHidden, numOutput);
hoSums = new double[numOutput];
hoBias = new double[numOutput];
OutputLayer = new double[numOutput];
}
public void propWeights(double[] weights){
int numWeights = (numInput * numHidden) + (numHidden * numOutput) + numHidden + numOutput;
if (weights.length != numWeights)
System.out.println("Size of arrays doesn't match");
int k=0;
for (int i = 0; i <= numInput; i++)
for (int j = 0; j <= numHidden; j++)
ihWeights[i][j] = weights[k++];
for (int i = 0; i < numHidden; ++i)
ihBias[i] = weights[k++];
for (int i = 0; i < numHidden; ++i)
for (int j = 0; j < numOutput; ++j)
hoWeights[i][j] = weights[k++];
for (int i = 0; i < numOutput; ++i)
hoBias[i] = weights[k++];
}
public static double[][] MakeMatrix(int rows, int cols)
{
double[][] result = new double[rows][];
for (int i = 0; i < rows; ++i)
result[i] = new double[cols];
return result;
}
public double[] ComputeOutputs(double[] xValues)
{
if (xValues.length != numInput)
System.out.println("Size of arrays doesn't match");
for (int i = 0; i < numHidden; ++i)
ihSums[i] = 0.0;
for (int i = 0; i < numOutput; ++i)
hoSums[i] = 0.0;
for (int i = 0; i < xValues.length; ++i)
this.InputLayer[i] = xValues[i];
for (int j = 0; j < numHidden; ++j)
for (int i = 0; i < numInput; ++i)
ihSums[j] += this.InputLayer[i] * ihWeights[i][j];
for (int i = 0; i < numHidden; ++i)
ihSums[i] += ihBias[i];
for (int i = 0; i < numHidden; ++i)
ihOutput[i] = Network.SigmoidFunction(ihSums[i]);
for (int j = 0; j < numOutput; ++j)
for (int i = 0; i < numHidden; ++i)
hoSums[j] += ihOutput[i] * hoWeights[i][j];
for (int i = 0; i < numOutput; ++i)
hoSums[i] += hoBias[i];
for (int i = 0; i < numOutput; ++i)
this.OutputLayer[i] = SigmoidFunction(hoSums[i]);
double[] result = new double[numOutput];
result=OutputLayer;
return result;
}
public static double SigmoidFunction(double x)
{
/*
* if (x < -45.0) return 0.0;
else if (x > 45.0) return 1.0;
else
*/ return 1.0 / (1.0 + Math.exp(-x));
}
}
this is the main class
这是主要的课程
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Network N=new Network(3, 4, 2);
double[] weights = new double[] {
0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2,
-2.0, -6.0, -1.0, -7.0,
1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0,
-2.5, -5.0 };
N.propWeights(weights);
double[] xValues = new double[] { 1.0, 2.0, 3.0 };
double[] yValues = N.ComputeOutputs(xValues);
for(double n:yValues){
System.out.println(n);
}
}
}
1 个解决方案
#1
3
First, Network.java
doesn't compile - you're missing a closing brace in the constructor.
首先,Network.java不编译 - 你在构造函数中缺少一个右括号。
You're getting a NullPointerException
because you're initializing ihWeights
in setLayers()
, but setLayers()
isn't invoked at all when you call ComputeOutputs()
.
你得到一个NullPointerException,因为你在setLayers()中初始化ihWeights,但是当你调用ComputeOutputs()时根本不会调用setLayers()。
#1
3
First, Network.java
doesn't compile - you're missing a closing brace in the constructor.
首先,Network.java不编译 - 你在构造函数中缺少一个右括号。
You're getting a NullPointerException
because you're initializing ihWeights
in setLayers()
, but setLayers()
isn't invoked at all when you call ComputeOutputs()
.
你得到一个NullPointerException,因为你在setLayers()中初始化ihWeights,但是当你调用ComputeOutputs()时根本不会调用setLayers()。