-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperceptron.elm
73 lines (56 loc) · 1.44 KB
/
perceptron.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
module Perceptron exposing (Perceptron, perceive, view)
import List.Extra as List
import Svg exposing (..)
import Svg.Attributes exposing (..)
type alias Perceptron =
{ bias : Float
, weights : List Float
}
perceive : Perceptron -> List Bool -> Bool
perceive perceptron inputs =
inputs
|> List.map floatify
|> List.zip perceptron.weights
|> List.map (\( w, i ) -> w * i)
|> List.sum
|> (>) perceptron.bias
floatify : Bool -> Float
floatify i =
case i of
True ->
1.0
False ->
0.0
-- View
view : Perceptron -> List Bool -> Svg msg
view p inputs =
svg
[ class "perceptron" ]
[ g
[ stroke "black"
, fill "white"
, color "black"
]
(circle
[ cx "100"
, cy "50"
, r "30"
]
[ text <| toString <| perceive p inputs ]
:: inputsToLines inputs
)
]
inputsToLines : List Bool -> List (Svg msg)
inputsToLines inputs =
inputs
|> List.map floatify
|> List.indexedMap (inputToLine <| List.length inputs)
inputToLine : Int -> Int -> Float -> Svg msg
inputToLine count index input =
line
[ x1 "0"
, y1 <| toString (50 / (toFloat count - 1) * (toFloat index))
, x2 "70"
, y2 "50"
]
[ text <| toString input ]