Example #1
0
 public double[] normalizedInstance(Instance inst) {
   // Normalize Instance
   double[] normalizedInstance = new double[inst.numAttributes()];
   for (int j = 0; j < inst.numAttributes() - 1; j++) {
     int instAttIndex = modelAttIndexToInstanceAttIndex(j, inst);
     double mean = perceptronattributeStatistics.getValue(j) / perceptronYSeen;
     double sd =
         computeSD(
             squaredperceptronattributeStatistics.getValue(j),
             perceptronattributeStatistics.getValue(j),
             perceptronYSeen);
     if (sd > SD_THRESHOLD) normalizedInstance[j] = (inst.value(instAttIndex) - mean) / sd;
     else normalizedInstance[j] = inst.value(instAttIndex) - mean;
   }
   return normalizedInstance;
 }
Example #2
0
  /** Update the model using the provided instance */
  public void trainOnInstanceImpl(Instance inst) {
    accumulatedError =
        Math.abs(this.prediction(inst) - inst.classValue()) + fadingFactor * accumulatedError;
    nError = 1 + fadingFactor * nError;
    // Initialise Perceptron if necessary
    if (this.initialisePerceptron == true) {
      this.fadingFactor = this.fadingFactorOption.getValue();
      this.classifierRandom.setSeed(randomSeedOption.getValue());
      this.initialisePerceptron = false; // not in resetLearningImpl() because it needs Instance!
      this.weightAttribute = new double[inst.numAttributes()];
      for (int j = 0; j < inst.numAttributes(); j++) {
        weightAttribute[j] = 2 * this.classifierRandom.nextDouble() - 1;
      }
      // Update Learning Rate
      learningRatio = learningRatioOption.getValue();
      this.learningRateDecay = learningRateDecayOption.getValue();
    }

    // Update attribute statistics
    this.perceptronInstancesSeen++;
    this.perceptronYSeen++;

    for (int j = 0; j < inst.numAttributes() - 1; j++) {
      perceptronattributeStatistics.addToValue(j, inst.value(j));
      squaredperceptronattributeStatistics.addToValue(j, inst.value(j) * inst.value(j));
    }
    this.perceptronsumY += inst.classValue();
    this.squaredperceptronsumY += inst.classValue() * inst.classValue();

    if (constantLearningRatioDecayOption.isSet() == false) {
      learningRatio =
          learningRatioOption.getValue() / (1 + perceptronInstancesSeen * learningRateDecay);
    }

    // double prediction = this.updateWeights(inst,learningRatio);
    // accumulatedError= Math.abs(prediction-inst.classValue()) + fadingFactor*accumulatedError;

    this.updateWeights(inst, learningRatio);
  }