Beispiel #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);
  }
Beispiel #2
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);
  }