java.lang.NullPointerException,找不到有什么问题

时间:2013-09-27 20:28:11

标签: java arrays

我正在尝试重写我发现创建神经网络的程序 代码似乎非常好(至少对我来说)但我一直得到 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));
    }

}

这是主要的课程

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 个答案:

答案 0 :(得分:3)

首先,Network.java无法编译 - 你在构造函数中缺少一个右括号。

您收到NullPointerException,因为您正在ihWeights初始化setLayers(),但在致电setLayers()时根本未调用ComputeOutputs()