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

114
LINES

< > BotCompany Repo | #1026915 // Perceptron Spike [from Rosetta Code]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Click here for Pure Java version (2454L/16K).

1  
!7
2  
3  
import javax.swing.Timer;
4  
5  
public class main extends JPanel {
6  
  class Trainer {
7  
      double[] inputs;
8  
      int answer;
9  
10  
      Trainer(double x, double y, int a) {
11  
          inputs = new double[]{x, y, 1};
12  
          answer = a;
13  
      }
14  
  }
15  
16  
  Trainer[] training = new Trainer[2000];
17  
  double[] weights;
18  
  double c = 0.00001;
19  
  int count;
20  
21  
  *(int n) {
22  
      Random r = new Random();
23  
      Dimension dim = new Dimension(640, 360);
24  
      setPreferredSize(dim);
25  
      setBackground(Color.white);
26  
27  
      weights = new double[n];
28  
      for (int i = 0; i < weights.length; i++) {
29  
          weights[i] = r.nextDouble() * 2 - 1;
30  
      }
31  
32  
      for (int i = 0; i < training.length; i++) {
33  
          double x = r.nextDouble() * dim.width;
34  
          double y = r.nextDouble() * dim.height;
35  
36  
          int answer = y < f(x) ? -1 : 1;
37  
38  
          training[i] = new Trainer(x, y, answer);
39  
      }
40  
41  
      new Timer(10, (ActionEvent e) -> {
42  
          repaint();
43  
      }).start();
44  
  }
45  
46  
  private double f(double x) {
47  
      return x * 0.7 + 40;
48  
  }
49  
50  
  int feedForward(double[] inputs) {
51  
      assert inputs.length == weights.length : "weights and input length mismatch";
52  
53  
      double sum = 0;
54  
      for (int i = 0; i < weights.length; i++) {
55  
          sum += inputs[i] * weights[i];
56  
      }
57  
      return activate(sum);
58  
  }
59  
60  
  int activate(double s) {
61  
      return s > 0 ? 1 : -1;
62  
  }
63  
64  
  void train(double[] inputs, int desired) {
65  
      int guess = feedForward(inputs);
66  
      double error = desired - guess;
67  
      for (int i = 0; i < weights.length; i++) {
68  
          weights[i] += c * error * inputs[i];
69  
      }
70  
  }
71  
72  
  @Override
73  
  public void paintComponent(Graphics gg) {
74  
      super.paintComponent(gg);
75  
      Graphics2D g = (Graphics2D) gg;
76  
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
77  
              RenderingHints.VALUE_ANTIALIAS_ON);
78  
79  
      // we're drawing upside down
80  
      int x = getWidth();
81  
      int y = (int) f(x);
82  
      g.setStroke(new BasicStroke(2));
83  
      g.setColor(Color.orange);
84  
      g.drawLine(0, (int) f(0), x, y);
85  
86  
      train(training[count].inputs, training[count].answer);
87  
      count = (count + 1) % training.length;
88  
89  
      g.setStroke(new BasicStroke(1));
90  
      g.setColor(Color.black);
91  
      for (int i = 0; i < count; i++) {
92  
          int guess = feedForward(training[i].inputs);
93  
94  
          x = (int) training[i].inputs[0] - 4;
95  
          y = (int) training[i].inputs[1] - 4;
96  
97  
          if (guess > 0)
98  
              g.drawOval(x, y, 8, 8);
99  
          else
100  
              g.fillOval(x, y, 8, 8);
101  
      }
102  
  }
103  
104  
  p-awt {
105  
    JFrame f = new JFrame();
106  
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
107  
    f.setTitle("Perceptron");
108  
    f.setResizable(false);
109  
    f.add(new main(3), BorderLayout.CENTER);
110  
    f.pack();
111  
    f.setLocationRelativeTo(null);
112  
    f.setVisible(true);
113  
  }
114  
}

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: #1026915
Snippet name: Perceptron Spike [from Rosetta Code]
Eternal ID of this version: #1026915/3
Text MD5: 9c1ddcedafe5b4002238e8e625b97fbe
Transpilation MD5: ba8555f596960c61c72e5bb9bb56e334
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-02 17:53:48
Source code size: 2919 bytes / 114 lines
Pitched / IR pitched: No / No
Views / Downloads: 315 / 883
Version history: 2 change(s)
Referenced in: [show references]