forked from rossng/depennd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Neural.idr
194 lines (172 loc) · 5.29 KB
/
Neural.idr
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
module Neural
import Data.Vect
import Matrix
%access export
public export
interface Layer (layer : Nat -> Nat -> Type) where
runLayer : Vect i Double
-> layer i o
-> Vect o Double
public export
data FullyConnected : Nat -> Nat -> Type where
MkFullyConnected : (biases : Vect o Double)
-> (weights : Matrix o i Double)
-> FullyConnected i o
Show (FullyConnected i o) where
show (MkFullyConnected biases weights) = "FullyConnected\n " ++ (show biases) ++ "\n " ++ (show weights)
public export
data ReLU : Nat -> Nat -> Type where
MkReLU : ReLU s s
Show (ReLU s s) where
show MkReLU = "ReLU"
public export
data Softmax : Nat -> Nat -> Type where
MkSoftmax : Softmax s s
Show (Softmax s s) where
show MkSoftmax = "Softmax"
public export
data Logit : Nat -> Nat -> Type where
MkLogit : Logit s s
Show (Logit s s) where
show MkLogit = "Logit"
-- infixr 5 :>:
--
-- data Network : Nat -> List Nat -> Nat -> Type -> Type where
-- Output : Layer i o a
-- -> Network i [] o a
-- (:>:) : Layer i h a
-- -> Network h hs o a
-- -> Network i (h :: hs) o a
--
Num a => Num (Vect n a) where
(+) = liftA2 (+)
(*) = liftA2 (*)
fromInteger {n} = replicate n . fromInteger
interface Scalable a where
scale : Double -> a -> a
Scalable Double where
scale = (*)
Scalable a => Scalable (Vect n a) where
scale lambda = map (scale lambda)
Neg a => Neg (Vect n a) where
(-) = liftA2 (-)
negate = map negate
abs = map abs
Layer FullyConnected where
runLayer input (MkFullyConnected biases weights) = weights .* input + biases
Layer ReLU where
runLayer input MkReLU = map (\e => if e > 0 then e else 0) input
Layer Softmax where
runLayer input MkSoftmax = map (/ s) input'
where input' = map exp input
s = sum input'
Layer Logit where
runLayer input MkLogit = map logistic input
where logistic x = 1 / (1 + exp (-x))
--
-- sigmoidD : Double -> Double
-- sigmoidD a = 1 / (1 + exp (-a))
--
-- sigmoidD' : Double -> Double
-- sigmoidD' a = let s = sigmoidD a
-- in s * (1 - s)
--
-- sigmoid : Vect n Double -> Vect n Double
-- sigmoid = map sigmoidD
--
-- sigmoid' : Vect n Double -> Vect n Double
-- sigmoid' = map sigmoidD'
--
-- calc : Vect i Double
-- -> Vect o Double
-- -> Matrix o i Double
-- -> Vect o Double
-- calc input bias weights = weights .* input + bias
--
-- runLayer : Vect i Double
-- -> Layer i o Double
-- -> Vect o Double
-- runLayer input (MkLayer bias weights) = calc input bias weights
--
-- runLayerS : Vect i Double
-- -> Layer i o Double
-- -> Vect o Double
-- runLayerS input layer = sigmoid $ runLayer input layer
--
-- feedForward : Vect i Double
-- -> Network i hs o Double
-- -> Vect o Double
-- feedForward input (l :>: ls) = let input' = (runLayerS input l)
-- in feedForward input' ls
-- feedForward input (Output layer) = runLayerS input layer
--
-- outer : Num a
-- => Vect m a
-- -> Vect n a
-- -> Matrix m n a
-- outer vm vn = (transpose [vm]) #*# [vn]
--
-- predictionError : Vect i Double
-- -> Vect o Double
-- -> Network i hs o Double
-- -> Vect o Double
-- predictionError input target net = target - (feedForward input net)
--
-- backprop : Double
-- -> Vect i Double
-- -> Vect o Double
-- -> Network i hs o Double
-- -> Network i hs o Double
-- backprop eta input target net = fst (go input target net)
-- where
-- go : Vect i Double
-- -> Vect o Double
-- -> Network i hs o Double
-- -> (Network i hs o Double, Vect i Double)
-- go input target (layer@(MkLayer bias weights) :>: rest) =
-- let y = runLayer input layer
-- output = sigmoid y
--
-- (rest', dWs') = go output target rest
--
-- dEdy = (sigmoid' y) * dWs'
-- --
-- bias' = bias - (eta `scale` dEdy)
-- weights' = weights - (eta `scale` (outer dEdy input))
-- layer' = (MkLayer bias' weights')
--
-- dWs = (transpose weights) .* dEdy
-- in (layer' :>: rest', dWs)
--
-- go input target (Output layer@(MkLayer bias weights)) =
-- let y = runLayer input layer
-- output = sigmoid y
--
-- dEdy = (sigmoid' y) * (output - target)
-- --
-- bias' = bias - (eta `scale` dEdy)
-- weights' = weights - (eta `scale` (outer dEdy input))
-- layer' = (MkLayer bias' weights')
--
-- dWs = (transpose weights) .* dEdy
-- in (Output layer', dWs)
--
-- initialNet : Network 2 [2] 2 Double
-- initialNet = first :>: second
-- where first = MkLayer [0.35, 0.35]
-- [ [0.15, 0.20]
-- , [0.25, 0.30]
-- ]
-- second = Output
-- $ MkLayer [0.60, 0.60]
-- [ [0.40, 0.45]
-- , [0.50, 0.55]
-- ]
--
-- input : Vect 2 Double
-- input = [0.05,0.10]
--
-- -- should be "target", but meh
-- output : Vect 2 Double
-- output = [0.01,0.99]
--