예제 #1
0
  /** {@inheritDoc} */
  @Override
  public void decodeFromArray(double[] encoded) {
    EngineArray.arrayCopy(encoded, 0, getFlat().getWeights(), 0, getFlat().getWeights().length);

    int index = getFlat().getWeights().length;

    for (RadialBasisFunction rbf : flat.getRBF()) {
      rbf.setWidth(encoded[index++]);
      EngineArray.arrayCopy(encoded, index, rbf.getCenters(), 0, rbf.getCenters().length);
      index += rbf.getCenters().length;
    }
  }
  public static ObjectPair<double[][], double[][]> trainingToArray(MLDataSet training) {
    int length = (int) training.getRecordCount();
    double[][] a = new double[length][training.getInputSize()];
    double[][] b = new double[length][training.getIdealSize()];

    int index = 0;
    for (MLDataPair pair : training) {
      EngineArray.arrayCopy(pair.getInputArray(), a[index]);
      EngineArray.arrayCopy(pair.getIdealArray(), b[index]);
      index++;
    }

    return new ObjectPair<double[][], double[][]>(a, b);
  }
  /**
   * Calculate the output for the given input.
   *
   * @param input The input.
   * @param output Output will be placed here.
   */
  public void compute(final double[] input, final double[] output) {
    final int sourceIndex = this.layerOutput.length - this.layerCounts[this.layerCounts.length - 1];

    EngineArray.arrayCopy(input, 0, this.layerOutput, sourceIndex, this.inputCount);

    for (int i = this.layerIndex.length - 1; i > 0; i--) {
      computeLayer(i);
    }

    // update context values
    final int offset = this.contextTargetOffset[0];

    EngineArray.arrayCopy(this.layerOutput, 0, layerOutput, offset, this.contextTargetSize[0]);

    EngineArray.arrayCopy(this.layerOutput, 0, output, 0, this.outputCount);
  }
  /**
   * Construct a 2d neighborhood function based on the sizes for the x and y dimensions.
   *
   * @param type The RBF type to use.
   * @param x The size of the x-dimension.
   * @param y The size of the y-dimension.
   */
  public NeighborhoodRBF(final RBFEnum type, final int x, final int y) {
    final int[] size = new int[2];
    size[0] = x;
    size[1] = y;

    final double[] centerArray = new double[2];
    centerArray[0] = 0;
    centerArray[1] = 0;

    final double[] widthArray = new double[2];
    widthArray[0] = 1;
    widthArray[1] = 1;

    switch (type) {
      case Gaussian:
        this.rbf = new GaussianFunction(2);
        break;
      case InverseMultiquadric:
        this.rbf = new InverseMultiquadricFunction(2);
        break;
      case Multiquadric:
        this.rbf = new MultiquadricFunction(2);
        break;
      case MexicanHat:
        this.rbf = new MexicanHatFunction(2);
        break;
    }

    this.rbf.setWidth(1);
    EngineArray.arrayCopy(centerArray, this.rbf.getCenters());

    this.size = size;

    calculateDisplacement();
  }
예제 #5
0
  /**
   * Determine the class using part of an array.
   *
   * @param pos The position to begin.
   * @param data The array to check.
   * @return The class item.
   */
  public final ClassItem determineClass(final int pos, final double[] data) {
    int resultIndex = 0;
    final double[] d = new double[getColumnsNeeded()];
    EngineArray.arrayCopy(data, pos, d, 0, d.length);

    switch (this.action) {
      case Equilateral:
        resultIndex = this.eq.decode(d);
        break;
      case OneOf:
        resultIndex = EngineArray.indexOfLargest(d);
        break;
      case SingleField:
        resultIndex = (int) d[0];
        break;
      default:
        throw new AnalystError("Invalid action: " + this.action);
    }

    if (resultIndex < 0) {
      return null;
    }

    return this.classes.get(resultIndex);
  }
 /**
  * Decode the specified data into the weights of the neural network. This method performs the
  * opposite of encodeNetwork.
  *
  * @param data The data to be decoded.
  */
 public void decodeNetwork(final double[] data) {
   if (data.length != this.weights.length) {
     throw new EncogError(
         "Incompatible weight sizes, can't assign length="
             + data.length
             + " to length="
             + this.weights.length);
   }
   this.weights = EngineArray.arrayCopy(data);
 }
  /**
   * Clone into the flat network passed in.
   *
   * @param result The network to copy into.
   */
  public void cloneFlatNetwork(final FlatNetwork result) {
    result.inputCount = this.inputCount;
    result.layerCounts = EngineArray.arrayCopy(this.layerCounts);
    result.layerIndex = EngineArray.arrayCopy(this.layerIndex);
    result.layerOutput = EngineArray.arrayCopy(this.layerOutput);
    result.layerSums = EngineArray.arrayCopy(this.layerSums);
    result.layerFeedCounts = EngineArray.arrayCopy(this.layerFeedCounts);
    result.contextTargetOffset = EngineArray.arrayCopy(this.contextTargetOffset);
    result.contextTargetSize = EngineArray.arrayCopy(this.contextTargetSize);
    result.layerContextCount = EngineArray.arrayCopy(this.layerContextCount);
    result.biasActivation = EngineArray.arrayCopy(this.biasActivation);
    result.outputCount = this.outputCount;
    result.weightIndex = this.weightIndex;
    result.weights = this.weights;
    result.layerDropoutRates = EngineArray.arrayCopy(this.layerDropoutRates);

    result.activationFunctions = new ActivationFunction[this.activationFunctions.length];
    for (int i = 0; i < result.activationFunctions.length; i++) {
      result.activationFunctions[i] = this.activationFunctions[i].clone();
    }

    result.beginTraining = this.beginTraining;
    result.endTraining = this.endTraining;
  }
  /**
   * Calculate a layer.
   *
   * @param currentLayer The layer to calculate.
   */
  protected void computeLayer(final int currentLayer) {

    final int inputIndex = this.layerIndex[currentLayer];
    final int outputIndex = this.layerIndex[currentLayer - 1];
    final int inputSize = this.layerCounts[currentLayer];
    final int outputSize = this.layerFeedCounts[currentLayer - 1];
    final double dropoutRate;
    if (this.layerDropoutRates.length > currentLayer - 1) {
      dropoutRate = this.layerDropoutRates[currentLayer - 1];
    } else {
      dropoutRate = 0;
    }

    int index = this.weightIndex[currentLayer - 1];

    final int limitX = outputIndex + outputSize;
    final int limitY = inputIndex + inputSize;

    // weight values
    for (int x = outputIndex; x < limitX; x++) {
      double sum = 0;
      for (int y = inputIndex; y < limitY; y++) {
        sum += this.weights[index++] * this.layerOutput[y] * (1 - dropoutRate);
      }
      this.layerSums[x] = sum;
      this.layerOutput[x] = sum;
    }

    this.activationFunctions[currentLayer - 1].activationFunction(
        this.layerOutput, outputIndex, outputSize);

    // update context values
    final int offset = this.contextTargetOffset[currentLayer];

    EngineArray.arrayCopy(
        this.layerOutput,
        outputIndex,
        this.layerOutput,
        offset,
        this.contextTargetSize[currentLayer]);
  }
 /**
  * Set the layer sums.
  *
  * @param d The layer sums.
  */
 public void setLayerSums(double[] d) {
   this.layerSums = EngineArray.arrayCopy(d);
 }
예제 #10
0
 /**
  * Set the weights.
  *
  * @param weights The weights.
  */
 public void setWeights(final double[] weights) {
   this.weights = EngineArray.arrayCopy(weights);
 }
예제 #11
0
 /**
  * Set the weight index.
  *
  * @param weightIndex The weight index.
  */
 public void setWeightIndex(final int[] weightIndex) {
   this.weightIndex = EngineArray.arrayCopy(weightIndex);
 }
예제 #12
0
 /**
  * Set the layer output.
  *
  * @param layerOutput The layer output.
  */
 public void setLayerOutput(final double[] layerOutput) {
   this.layerOutput = EngineArray.arrayCopy(layerOutput);
 }
예제 #13
0
 /**
  * Set the layer index.
  *
  * @param i The layer index.
  */
 public void setLayerIndex(final int[] i) {
   this.layerIndex = EngineArray.arrayCopy(i);
 }
예제 #14
0
 public void setLayerFeedCounts(final int[] layerFeedCounts) {
   this.layerFeedCounts = EngineArray.arrayCopy(layerFeedCounts);
 }
예제 #15
0
 /**
  * Set the layer context count.
  *
  * @param layerContextCount The layer context count.
  */
 public void setLayerContextCount(final int[] layerContextCount) {
   this.layerContextCount = EngineArray.arrayCopy(layerContextCount);
 }
예제 #16
0
 /**
  * Set the context target size.
  *
  * @param contextTargetSize The context target size.
  */
 public void setContextTargetSize(final int[] contextTargetSize) {
   this.contextTargetSize = EngineArray.arrayCopy(contextTargetSize);
 }
예제 #17
0
 /**
  * Copy a byte array.
  *
  * @param input The array to copy.
  * @return The result of the copy.
  */
 public static byte[] arrayCopy(final byte[] input) {
   final byte[] result = new byte[input.length];
   EngineArray.arrayCopy(input, result);
   return result;
 }
예제 #18
0
 /**
  * Construct a fold from the specified flat network.
  *
  * @param flat THe flat network.
  */
 public NetworkFold(final FlatNetwork flat) {
   this.weights = EngineArray.arrayCopy(flat.getWeights());
   this.output = EngineArray.arrayCopy(flat.getLayerOutput());
 }
예제 #19
0
 /**
  * Copy weights and output to the network.
  *
  * @param target The network to copy to.
  */
 public final void copyToNetwork(final FlatNetwork target) {
   EngineArray.arrayCopy(this.weights, target.getWeights());
   EngineArray.arrayCopy(this.output, target.getLayerOutput());
 }
예제 #20
0
 /**
  * Copy the weights and output from the network.
  *
  * @param source The network to copy from.
  */
 public final void copyFromNetwork(final FlatNetwork source) {
   EngineArray.arrayCopy(source.getWeights(), this.weights);
   EngineArray.arrayCopy(source.getLayerOutput(), this.output);
 }
예제 #21
0
 /**
  * Set the context target offset.
  *
  * @param contextTargetOffset The context target offset.
  */
 public void setContextTargetOffset(final int[] contextTargetOffset) {
   this.contextTargetOffset = EngineArray.arrayCopy(contextTargetOffset);
 }
예제 #22
0
 /**
  * Copy an int array.
  *
  * @param input The array to copy.
  * @return The result of the copy.
  */
 public static int[] arrayCopy(final int[] input) {
   final int[] result = new int[input.length];
   EngineArray.arrayCopy(input, result);
   return result;
 }