Uses 911K of libraries. Click here for Pure Java version (5136L/26K).
1 | !7 |
2 | |
3 | cmodule PerceptronSpike > DynPrintLogAndEnabled {
|
4 | sclass Example {
|
5 | double[] inputs; |
6 | int answer; |
7 | |
8 | *() {}
|
9 | *(double x, double y, int a) {
|
10 | inputs = new double[] {x, y, 1};
|
11 | answer = a; |
12 | } |
13 | } |
14 | |
15 | switchable int nExamples = 200; |
16 | L<Example> examples; |
17 | switchable double c = 1; |
18 | switchable bool randomizeC = false; |
19 | switchable bool startWithRandomWeights = true; |
20 | transient double[] weights; |
21 | transient double error; |
22 | |
23 | start-thread {
|
24 | if (l(examples) != nExamples) |
25 | setField(examples := repF(nExamples, () -> {
|
26 | int x = rand(640), y = rand(360); |
27 | ret new Example(x, y, y < f(x) ? -1 : 1); |
28 | })); |
29 | |
30 | int n = l(first(examples).inputs); |
31 | print(+n); |
32 | weights = new double[n]; |
33 | if (startWithRandomWeights) |
34 | for i over weights: weights[i] = random(-1.0, 1.0); |
35 | |
36 | double lastError = -1; |
37 | long round = 0; |
38 | while ping (lastError != 0) {
|
39 | if (!enabled) continue with sleepSeconds(1); |
40 | ++round; |
41 | double error = train(); |
42 | if (error != lastError) {
|
43 | print("Error: " + error + ", round " + round + ", weights: " + sfu(weights));
|
44 | lastError = error; |
45 | } |
46 | } |
47 | } |
48 | |
49 | double train() {
|
50 | double error = 0; |
51 | for (Example e : examples) error += abs(train(e)); |
52 | ret error/l(examples); |
53 | } |
54 | |
55 | // function to be learned |
56 | private double f(double x) {
|
57 | ret x * 0.7 + 40; |
58 | } |
59 | |
60 | int feedForward(double[] inputs) {
|
61 | double sum = 0; |
62 | for i over weights: sum += inputs[i] * weights[i]; |
63 | ret activate(sum); |
64 | } |
65 | |
66 | int activate(double s) {
|
67 | return s > 0 ? 1 : -1; |
68 | } |
69 | |
70 | double train(Example e) {
|
71 | int guess = feedForward(e.inputs); |
72 | double error = e.answer - guess; |
73 | double cc = c/nExamples; |
74 | for i over weights: weights[i] += (randomizeC ? rand(cc) : cc) * error * e.inputs[i]; |
75 | ret error; |
76 | } |
77 | } |
Began life as a copy of #1026915
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: | #1026916 |
| Snippet name: | Perceptron Spike [learn 3 weights, OK] |
| Eternal ID of this version: | #1026916/26 |
| Text MD5: | c50d057b14283b664241c70d4bf22ad5 |
| Transpilation MD5: | aaa111a1b7c37167503790de9492ef60 |
| 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 19:02:11 |
| Source code size: | 1916 bytes / 77 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 550 / 1050 |
| Version history: | 25 change(s) |
| Referenced in: | [show references] |