Jump to content

Perceptron: Difference between revisions

add REXX
m (→‎{{header|Go}}: Added reference to Graphics library used.)
(add REXX)
Line 860:
 
Run it and see the image for yourself, I can't get it onto RC!
 
=={{header|REXX}}==
{{trans|Java}}
<lang rexx>/* REXX */
Call init
Call time 'R'
try=0
Call show 0
Do d=1 To dots
x=x.d
y=y.d
Parse Value x y 1 with inputs.0 inputs.1 inputs.2
answer.d=sign(y-f(x))
Select
When f(x)<y Then r='<'
When f(x)>y Then r='>'
Otherwise r='='
End
training.d=x y 1 answer.d
End
Do try=1 To tries
Call time 'R'
zz=0
Do d=1 To dots
Parse Var training.d inputs.0 inputs.1 inputs.2 answer.d
Call train d
Do ii=1 To d
Parse Var training.ii inputs.0 inputs.1 inputs.2 answer.d
guess = feedForward(d)
End
End
Call show try
End
Exit
 
show:
Parse Arg run
show=wordpos(run,'0 1' tries)>0
If run>0 Then Say ' '
If show Then Say 'Point x f(x) r y ok ff zz'
zz=0
Do d=1 To dots
x=x.d
y=y.d
Parse Value x.d y.d 1 with inputs.0 inputs.1 inputs.2
ff=format(feedForward(),2)
Select
When f(x)<y Then r='<'
When f(x)>y Then r='>'
Otherwise r='='
End
If r='<' & ff=1 |,
r='>' & ff=-1 Then Do; tag='ok'; zz=zz+1; End
Else tag='--'
If show Then
Say format(d,5) format(x,4,0) format(f(x),4,0) r format(y,4,0) right(ff,2),
tag format(zz,4)
End
If show Then Say copies('-',33)
weights=format(weights.0,2,5) format(weights.1,2,5) format(weights.2,2,5)
Select
When run=0 Then txt='Initial pattern'
When run=1 Then txt='After one loop '
Otherwise txt='After' run 'loops'
End
Say left(txt,15) format(zz,4) 'points fire. weights='weights
Return
 
train: Procedure Expose inputs. weights.
desired=sign(inputs.1-f(inputs.0))
guess = feedForward()
error = desired-guess
Do i=0 To 2
weights.i=weights.i+0.00001*error*inputs.i
End
Return
 
f: Return arg(1)*0.7+40
 
nextDouble: /* random number between -1 and +1 */
Return random(100000)/100000
 
feedforward: Procedure Expose inputs. weights.
sum=0
Do i=0 To 2
sum=sum+inputs.i*weights.i
End
Return activate(sum)
 
activate:
If arg(1)>0 Then Return 1
Else Return -1
 
init:
Call random 10000,10000,333 /* seed the random function */
dots=30
width=640
height=360
tries=10
Do i=0 To 2
weights.i=nextDouble()
End
Do i=1 To dots
x.i=nextDouble()*width
y.i=nextDouble()*height
End
Return</lang>
{{out}}
<pre>Point x f(x) r y ok ff zz
1 100 110 < 204 1 ok 1
2 613 469 > 117 1 -- 1
3 528 409 > 125 1 -- 1
4 141 139 > 119 1 -- 1
5 32 62 < 245 1 ok 2
6 11 48 < 336 1 ok 3
7 435 344 > 270 1 -- 3
8 572 440 > 280 1 -- 3
9 442 350 > 141 1 -- 3
10 410 327 > 209 1 -- 3
11 290 243 < 355 1 ok 4
12 257 220 < 260 1 ok 5
13 235 205 > 51 1 -- 5
14 600 460 > 66 1 -- 5
15 21 55 < 182 1 ok 6
16 197 178 > 42 1 -- 6
17 444 351 > 150 1 -- 6
18 393 315 > 87 1 -- 6
19 622 475 > 280 1 -- 6
20 436 345 > 292 1 -- 6
21 553 427 > 261 1 -- 6
22 478 374 > 264 1 -- 6
23 373 301 > 120 1 -- 6
24 527 409 > 94 1 -- 6
25 558 431 > 49 1 -- 6
26 616 471 > 358 1 -- 6
27 241 209 > 68 1 -- 6
28 365 295 > 164 1 -- 6
29 371 299 > 155 1 -- 6
30 102 112 < 220 1 ok 7
---------------------------------
Initial pattern 7 points fire. weights= 0.28732 0.50931 0.45298
 
Point x f(x) r y ok ff zz
1 100 110 < 204 1 ok 1
2 613 469 > 117 1 -- 1
3 528 409 > 125 1 -- 1
4 141 139 > 119 1 -- 1
5 32 62 < 245 1 ok 2
6 11 48 < 336 1 ok 3
7 435 344 > 270 1 -- 3
8 572 440 > 280 1 -- 3
9 442 350 > 141 1 -- 3
10 410 327 > 209 1 -- 3
11 290 243 < 355 1 ok 4
12 257 220 < 260 1 ok 5
13 235 205 > 51 1 -- 5
14 600 460 > 66 1 -- 5
15 21 55 < 182 1 ok 6
16 197 178 > 42 1 -- 6
17 444 351 > 150 1 -- 6
18 393 315 > 87 1 -- 6
19 622 475 > 280 1 -- 6
20 436 345 > 292 1 -- 6
21 553 427 > 261 1 -- 6
22 478 374 > 264 1 -- 6
23 373 301 > 120 1 -- 6
24 527 409 > 94 1 -- 6
25 558 431 > 49 1 -- 6
26 616 471 > 358 1 -- 6
27 241 209 > 68 1 -- 6
28 365 295 > 164 1 -- 6
29 371 299 > 155 1 -- 6
30 102 112 < 220 1 ok 7
---------------------------------
After one loop 7 points fire. weights= 0.08433 0.43412 0.45252
 
After 2 loops 16 points fire. weights=-0.10749 0.35991 0.45208
 
After 3 loops 26 points fire. weights=-0.18168 0.31845 0.45192
 
After 4 loops 28 points fire. weights=-0.20192 0.30482 0.45186
 
After 5 loops 29 points fire. weights=-0.20473 0.30245 0.45184
 
After 6 loops 29 points fire. weights=-0.20755 0.30007 0.45182
 
After 7 loops 29 points fire. weights=-0.21037 0.29769 0.45180
 
After 8 loops 29 points fire. weights=-0.21319 0.29532 0.45178
 
After 9 loops 29 points fire. weights=-0.21601 0.29294 0.45176
 
Point x f(x) r y ok ff zz
1 100 110 < 204 1 ok 1
2 613 469 > 117 -1 ok 2
3 528 409 > 125 -1 ok 3
4 141 139 > 119 1 -- 3
5 32 62 < 245 1 ok 4
6 11 48 < 336 1 ok 5
7 435 344 > 270 -1 ok 6
8 572 440 > 280 -1 ok 7
9 442 350 > 141 -1 ok 8
10 410 327 > 209 -1 ok 9
11 290 243 < 355 1 ok 10
12 257 220 < 260 1 ok 11
13 235 205 > 51 -1 ok 12
14 600 460 > 66 -1 ok 13
15 21 55 < 182 1 ok 14
16 197 178 > 42 -1 ok 15
17 444 351 > 150 -1 ok 16
18 393 315 > 87 -1 ok 17
19 622 475 > 280 -1 ok 18
20 436 345 > 292 -1 ok 19
21 553 427 > 261 -1 ok 20
22 478 374 > 264 -1 ok 21
23 373 301 > 120 -1 ok 22
24 527 409 > 94 -1 ok 23
25 558 431 > 49 -1 ok 24
26 616 471 > 358 -1 ok 25
27 241 209 > 68 -1 ok 26
28 365 295 > 164 -1 ok 27
29 371 299 > 155 -1 ok 28
30 102 112 < 220 1 ok 29
---------------------------------
After 10 loops 29 points fire. weights=-0.21883 0.29057 0.45174</pre>
 
 
 
=={{header|Scala}}==
2,295

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.