Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

99
LINES

< > BotCompany Repo | #1026919 // Perceptron

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Libraryless. Click here for Pure Java version (4122L/26K).

// class is persistable
sclass Perceptron implements IPred<double[]> {
  srecord Example(double[] inputs, bool answer) {
    double getInput(int i) { ret i < inputs.length ? inputs[i] : 1; }
  }

  L<Example> examples = syncList();
  double c = 1;
  bool randomizeC = true, startWithRandomWeights = false;
  long trainingRound;
  double error = -1; // -1 = not trained yet
  double[] weights;
  
  *() {}
  *(L<Example> *examples) {}

  ItIt<Double> trainingIterator() {
    if (empty(examples)) ret emptyItIt();
    int n = l(first(examples).inputs)+1;
    if (weights == null) {
      weights = new double[n];
      if (startWithRandomWeights)
        for i over weights: weights[i] = random(-1.0, 1.0);
    }
    ret iff(() -> {
      if (error == 0) ret endMarker();
      ++trainingRound;
      double lastError = error;
      trainARound();
      ret error != lastError ? error : null;
    });
  }
  
  double trainARound() {
    double error = 0;
    int n = l(examples);
    if (n == 0) ret -1;
    for (Example e : concIter(examples)) error += abs(trainAnExample(e));
    ret this.error = error/n;
  }
  
  int feedForward(double[] inputs) {
    double sum = last(weights);
    for i over inputs: sum += inputs[i] * weights[i];
    ret activate(sum);
  }
  
  public Bool get(double[] inputs) {
    ret feedForward(inputs) > 0;
  }

  int activate(double s) {
    return s > 0 ? 1 : -1;
  }
  
  double recalcError() {
    double error = 0;
    int n = l(examples);
    if (n == 0) ret -1;
    for (Example e : concIter(examples))
      error += abs(errorForExample(e));
    ret this.error = error/n;
  }
    
  double errorForExample(Example e) {
    int guess = feedForward(e.inputs);
    ret (e.answer ? 1 : -1) - guess;
  }

  double trainAnExample(Example e) {
    double error = errorForExample(e);
    if (error != 0) {
      double cc = c/l(examples);
      for i over weights:
        weights[i] += (randomizeC ? rand(cc) : cc) * error * e.getInput(i);
    }
    ret error;
  }
  
  void printWithWeights {
    print("Error: " + error + ", round " + trainingRound + ", weights: " + sfu(weights));
  }
  
  // scaling the weights doesn't make a difference
  // returns true if it worked (error did not increase)
  bool scaleWeights(double factor) {
    for i over weights:
      weights[i] *= factor;
    double lastError = error;
    if (recalcError() > lastError)
      ret false with warn("Error increased from \*lastError*/ to \*error*/ during scale weight with factor \*factor*/");
    true;
  }
  
  void addExample(double[] inputs, bool answer) {
    examples.add(new Example(inputs, answer));
    error = -1;
  }
}

Author comment

Began life as a copy of #1026916

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1026919
Snippet name: Perceptron
Eternal ID of this version: #1026919/29
Text MD5: 0434286372a7b18bbdec746ba10d70c5
Transpilation MD5: a79007d898549c7551a569120a13c602
Author: stefan
Category: javax
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-02 21:06:57
Source code size: 2737 bytes / 99 lines
Pitched / IR pitched: No / No
Views / Downloads: 316 / 1027
Version history: 28 change(s)
Referenced in: [show references]