ニューラル ネットワークを作成するために見つけたプログラムを書き直そうとしています。コードは非常に優れているようですが (少なくとも私にとっては)、ihWeights[i][j] = weights[k++]; でjava.lang.NullPointerExceptionエラーが発生し続けます。何が悪いのかわからない
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));
}
}
これがメインクラスです
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);
}
}
}