Example #1
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);
  }
  /** {@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();
  }
  /**
   * Classify the input.
   *
   * @param input The input to classify.
   */
  @Override
  public int classify(MLData input) {

    if (this.classificationTarget < 0 || this.classificationTarget >= this.events.size()) {
      throw new BayesianError(
          "Must specify classification target by calling setClassificationTarget.");
    }

    int[] d = this.determineClasses(input);

    // properly tag all of the events
    for (int i = 0; i < this.events.size(); i++) {
      BayesianEvent event = this.events.get(i);
      if (i == this.classificationTarget) {
        this.query.defineEventType(event, EventType.Outcome);
      } else if (this.inputPresent[i]) {
        this.query.defineEventType(event, EventType.Evidence);
        this.query.setEventValue(event, d[i]);
      } else {
        this.query.defineEventType(event, EventType.Hidden);
        this.query.setEventValue(event, d[i]);
      }
    }

    // loop over and try each outcome choice
    BayesianEvent outcomeEvent = this.events.get(this.classificationTarget);
    this.classificationProbabilities = new double[outcomeEvent.getChoices().size()];
    for (int i = 0; i < outcomeEvent.getChoices().size(); i++) {
      this.query.setEventValue(outcomeEvent, i);
      this.query.execute();
      classificationProbabilities[i] = this.query.getProbability();
    }

    return EngineArray.maxIndex(this.classificationProbabilities);
  }
  /**
   * Construct a network analyze class. Analyze the specified network.
   *
   * @param network The network to analyze.
   */
  public AnalyzeNetwork(final BasicNetwork network) {
    final int assignDisabled = 0;
    final int assignedTotal = 0;
    final List<Double> biasList = new ArrayList<Double>();
    final List<Double> weightList = new ArrayList<Double>();
    final List<Double> allList = new ArrayList<Double>();

    for (int layerNumber = 0; layerNumber < network.getLayerCount() - 1; layerNumber++) {
      final int fromCount = network.getLayerNeuronCount(layerNumber);
      final int fromBiasCount = network.getLayerTotalNeuronCount(layerNumber);
      final int toCount = network.getLayerNeuronCount(layerNumber + 1);

      // weights
      for (int fromNeuron = 0; fromNeuron < fromCount; fromNeuron++) {
        for (int toNeuron = 0; toNeuron < toCount; toNeuron++) {
          final double v = network.getWeight(layerNumber, fromNeuron, toNeuron);
          weightList.add(v);
          allList.add(v);
        }
      }

      // bias
      if (fromCount != fromBiasCount) {
        final int biasNeuron = fromCount;
        for (int toNeuron = 0; toNeuron < toCount; toNeuron++) {
          final double v = network.getWeight(layerNumber, biasNeuron, toNeuron);
          biasList.add(v);
          allList.add(v);
        }
      }
    }

    for (final Layer layer : network.getStructure().getLayers()) {
      if (layer.hasBias()) {
        for (int i = 0; i < layer.getNeuronCount(); i++) {}
      }
    }

    this.disabledConnections = assignDisabled;
    this.totalConnections = assignedTotal;
    this.weights = new NumericRange(weightList);
    this.bias = new NumericRange(biasList);
    this.weightsAndBias = new NumericRange(allList);
    this.weightValues = EngineArray.listToDouble(weightList);
    this.allValues = EngineArray.listToDouble(allList);
    this.biasValues = EngineArray.listToDouble(biasList);
  }
 /**
  * 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);
 }
 public void testAnalyze() {
   BasicNetwork network = EncogUtility.simpleFeedForward(2, 2, 0, 1, false);
   double[] weights = new double[network.encodedArrayLength()];
   EngineArray.fill(weights, 1.0);
   network.decodeFromArray(weights);
   AnalyzeNetwork analyze = new AnalyzeNetwork(network);
   Assert.assertEquals(weights.length, analyze.getWeightsAndBias().getSamples());
   Assert.assertEquals(3, analyze.getBias().getSamples());
   Assert.assertEquals(6, analyze.getWeights().getSamples());
 }
  /** Finalize the structure of this Bayesian network. */
  public void finalizeStructure() {
    for (BayesianEvent e : this.eventMap.values()) {
      e.finalizeStructure();
    }

    if (this.query != null) {
      this.query.finalizeStructure();
    }

    this.inputPresent = new boolean[this.events.size()];
    EngineArray.fill(this.inputPresent, true);
    this.classificationTarget = -1;
  }
  /**
   * 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;
  }
Example #12
0
  /**
   * Determine what class the specified data belongs to.
   *
   * @param data The data to analyze.
   * @return The class the data belongs to.
   */
  public final ClassItem determineClass(final double[] data) {
    int resultIndex = 0;

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

    return this.classes.get(resultIndex);
  }
  /** {@inheritDoc} */
  @Override
  public final void iteration() {

    if (this.mustInit) {
      initWeights();
    }

    double worstDistance = Double.NEGATIVE_INFINITY;

    for (final MLDataPair pair : this.training) {
      final MLData out = this.network.computeInstar(pair.getInput());

      // determine winner
      final int winner = EngineArray.indexOfLargest(out.getData());

      // calculate the distance
      double distance = 0;
      for (int i = 0; i < pair.getInput().size(); i++) {
        final double diff =
            pair.getInput().getData(i) - this.network.getWeightsInputToInstar().get(i, winner);
        distance += diff * diff;
      }
      distance = BoundMath.sqrt(distance);

      if (distance > worstDistance) {
        worstDistance = distance;
      }

      // train
      for (int j = 0; j < this.network.getInputCount(); j++) {
        final double delta =
            this.learningRate
                * (pair.getInput().getData(j)
                    - this.network.getWeightsInputToInstar().get(j, winner));

        this.network.getWeightsInputToInstar().add(j, winner, delta);
      }
    }

    setError(worstDistance);
  }
  /**
   * 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]);
  }
Example #15
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;
 }
 /**
  * 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());
 }
 public int winner(MLData output) {
   return EngineArray.maxIndex(output.getData());
 }
 /**
  * Set the layer sums.
  *
  * @param d The layer sums.
  */
 public void setLayerSums(double[] d) {
   this.layerSums = EngineArray.arrayCopy(d);
 }
 /**
  * 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());
 }
 /**
  * Set the weight index.
  *
  * @param weightIndex The weight index.
  */
 public void setWeightIndex(final int[] weightIndex) {
   this.weightIndex = EngineArray.arrayCopy(weightIndex);
 }
 /**
  * Set the weights.
  *
  * @param weights The weights.
  */
 public void setWeights(final double[] weights) {
   this.weights = EngineArray.arrayCopy(weights);
 }
 /**
  * Set the layer index.
  *
  * @param i The layer index.
  */
 public void setLayerIndex(final int[] i) {
   this.layerIndex = EngineArray.arrayCopy(i);
 }
 /**
  * Set the layer output.
  *
  * @param layerOutput The layer output.
  */
 public void setLayerOutput(final double[] layerOutput) {
   this.layerOutput = EngineArray.arrayCopy(layerOutput);
 }
 /**
  * Set the layer context count.
  *
  * @param layerContextCount The layer context count.
  */
 public void setLayerContextCount(final int[] layerContextCount) {
   this.layerContextCount = EngineArray.arrayCopy(layerContextCount);
 }
 public void setLayerFeedCounts(final int[] layerFeedCounts) {
   this.layerFeedCounts = EngineArray.arrayCopy(layerFeedCounts);
 }
 /**
  * Set the context target size.
  *
  * @param contextTargetSize The context target size.
  */
 public void setContextTargetSize(final int[] contextTargetSize) {
   this.contextTargetSize = EngineArray.arrayCopy(contextTargetSize);
 }
 /**
  * Set the context target offset.
  *
  * @param contextTargetOffset The context target offset.
  */
 public void setContextTargetOffset(final int[] contextTargetOffset) {
   this.contextTargetOffset = EngineArray.arrayCopy(contextTargetOffset);
 }
 /**
  * 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);
 }
Example #29
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;
 }
 /** Clear the weight matrix. */
 public void clearMatrix() {
   EngineArray.fill(this.hopfield.getWeights(), 0);
 }