예제 #1
0
  public ImageRecord[] update(ImageRecord[] deltas, Integer batchSize, Double learningRate)
      throws ScaleNotMatchException {
    int imageSize = deltas.length;
    ImageRecord[] deltaResult = new ImageRecord[this.inMapSize];
    for (int i = 0; i < imageSize; i++)
      deltas[i] =
          new ImageRecord(
              MathUtils.MatrixElementMultiply(
                  deltas[i].getImage(),
                  MathUtils.MatrixElementMultiply(
                      outputs[i].getImage(),
                      MathUtils.ContantMinusArray(outputs[i].getImage(), 1.0))),
              -999);
    this.d = deltas; // 保存卷积层的灵敏度

    for (int i = 0; i < inMapSize; i++) {
      Double[][] image = new Double[inputSize.GetLeftElement()][inputSize.GetRightElement()];
      for (int j = 0; j < inputSize.GetLeftElement(); j++)
        for (int k = 0; k < inputSize.GetRightElement(); k++) image[j][k] = 0.0;

      for (int j = 0; j < imageSize; j++) {
        image =
            MathUtils.addArrays(
                image,
                MathUtils.conv(deltas[j].getImage(), kernels[i][j].getRotatedKernel(), "full"));
      }
      deltaResult[i] = new ImageRecord(image, -999);
    }
    calculateGradient();
    this.batchCount += 1;
    // 按batchsize更新权重
    if (this.batchCount == batchSize - 1) {
      updateWeight(learningRate);
      this.batchCount = 0;
    }
    return deltaResult;
  }
예제 #2
0
 public ImageRecord[] calculateOutputs(ImageRecord[] inputs) throws ScaleNotMatchException {
   this.inputs = inputs;
   for (int i = 0; i < outMapSize; i++) {
     Double[][] z =
         new Double[outputSize.GetLeftElement()][outputSize.GetRightElement()]; // 初始化输出图像
     for (int j = 0; j < inputs.length; j++) {
       ImageRecord input = inputs[j];
       z =
           MathUtils.addArrays(
               z, MathUtils.conv(input.getImage(), kernels[j][i].getKernel(), "valid")); // 图像卷积
     }
     z = MathUtils.AddConstantToArray(z, b[i]);
     z = MathUtils.SigmoidForImage(z);
     outputs[i] = new ImageRecord(z, -999);
   }
   return outputs;
 }
예제 #3
0
  public ConvLayer(
      Pair<Integer, Integer> inputSize,
      Integer inMapSize,
      Integer outMapSize,
      Integer kernelSize,
      String activationFunction) {
    super();
    type = "Convolutional Layer";
    this.activationFunction = activationFunction;
    this.inputSize = new Pair<Integer, Integer>(inputSize);
    this.outputSize =
        new Pair<Integer, Integer>(
            inputSize.GetLeftElement() - kernelSize + 1,
            inputSize.GetRightElement() - kernelSize + 1);
    this.inMapSize = inMapSize;
    this.kernelSize = kernelSize;
    this.outMapSize = outMapSize;
    this.fan_in = inMapSize * (kernelSize * kernelSize); // 该卷积层的一个map对应的卷积核权重数
    this.fan_out = outMapSize * (kernelSize * kernelSize); // 该卷积层的卷积核权重数
    this.outputs = new ImageRecord[outMapSize];

    init_Kernels();
    init_b();
  }