-
Notifications
You must be signed in to change notification settings - Fork 0
/
autodiff.ml
297 lines (222 loc) · 8.48 KB
/
autodiff.ml
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
open Core
module Make (Floatlike : Floatlike.For_autodiff) = struct
module Univar = struct
type t =
{ f : Floatlike.t -> Floatlike.t
; f' : t Lazy.t
}
let eval { f; f' = _ } y = f y
let d { f = _; f' } = Lazy.force f'
let rec c y =
{ f = Fn.const y
; f' = Lazy.from_fun (fun () -> c Floatlike.zero)
}
let zero = c Floatlike.zero
let one = c Floatlike.one
let two = c Floatlike.(one + one)
let x =
{ f = Fn.id
; f' = Lazy.from_fun (fun () -> one)
}
let rec scale t k =
{ f = (fun y -> Floatlike.( * ) k (eval t y))
; f' = Lazy.from_fun (fun () -> scale (d t) k)
}
let rec (+) g h =
{ f = (fun y -> Floatlike.(+) (eval g y) (eval h y))
; f' = Lazy.from_fun (fun () -> d g + d h)
}
let rec (-) g h =
{ f = (fun y -> Floatlike.(-) (eval g y) (eval h y))
; f' = Lazy.from_fun (fun () -> d g - d h)
}
let rec ( * ) g h =
{ f = (fun y -> Floatlike.( * ) (eval g y) (eval h y))
; f' = Lazy.from_fun (fun () -> g * d h + d g * h)
}
let rec compose g h =
{ f = (fun y -> eval g (eval h y))
; f' = Lazy.from_fun (fun () -> compose (d g) h * d h)
}
module Uncomposed = struct
let int_pow n =
let rec int_pow_and_scale n k =
{ f = (fun y -> Floatlike.(k * int_pow y n))
; f' = Lazy.from_fun (fun () -> int_pow_and_scale (Int.pred n) Floatlike.(of_int n * k))
}
in
int_pow_and_scale n Floatlike.one
let (/) g h = g * (compose (int_pow (-1)) h)
let pow p =
let rec pow_and_scale ~power:p k =
{ f = (fun y -> Floatlike.(k * (y ** p)))
; f' = Lazy.from_fun (fun () -> pow_and_scale ~power:Floatlike.(p - one) Floatlike.(p * k))
}
in
pow_and_scale ~power:p Floatlike.one
let exp =
let rec exp' () =
{ f = Floatlike.exp
; f' = Lazy.from_fun (fun () -> exp' ()) }
in
exp' ()
let log =
{ f = Floatlike.log
; f' = Lazy.from_fun (fun () -> int_pow (-1))
}
let sin, cos =
let rec sin' () =
{ f = Floatlike.sin
; f' = Lazy.from_fun (fun () -> cos' ()) }
and cos' () =
{ f = Floatlike.cos
; f' = Lazy.from_fun (fun () -> scale (sin' ()) Floatlike.(zero - one)) }
in
sin' (), cos' ()
let tan = sin / cos
let abs =
let rec abs' () =
{ f = Floatlike.abs
; f' = Lazy.from_fun (fun () -> abs' () * int_pow (-1))
}
in
abs' ()
let step = (abs + x) / (two * x)
let relu = (abs + x) / two
let softplus = compose log (one + exp)
let sigmoid =
let rec sigmoid' () =
{ f = (fun y -> Floatlike.(one * int_pow (one + exp ((zero - one) * y)) (-1)))
; f' = Lazy.from_fun (fun () -> sigmoid' () * (one - sigmoid' ()))
}
in
sigmoid' ()
end
let int_pow t n = compose (Uncomposed.int_pow n) t
let (/) = Uncomposed.(/)
let pow t p = compose (Uncomposed.pow p) t
let exp t = compose Uncomposed.exp t
let log t = compose Uncomposed.log t
let ( ** ) g h = exp (h * log g)
let sin t = compose Uncomposed.sin t
let cos t = compose Uncomposed.cos t
let tan t = compose Uncomposed.tan t
let abs t = compose Uncomposed.abs t
let step t = compose Uncomposed.step t
let relu t = compose Uncomposed.relu t
let softplus t = compose Uncomposed.softplus t
let sigmoid t = compose Uncomposed.sigmoid t
end
type t =
{ f : Floatlike.t Infinite_list.t -> Floatlike.t
; f' : t Infinite_list.t Lazy.t
}
let eval { f; f' = _ } y = f y
let eval' t y = eval t (Infinite_list.of_list y ~default:Floatlike.zero)
let grad { f = _; f' } = Lazy.force f'
let rec c y =
{ f = Fn.const y
; f' = Lazy.from_fun (fun () -> Infinite_list.constant ~default:(c Floatlike.zero))
}
let zero = c Floatlike.zero
let one = c Floatlike.one
let two = c Floatlike.(one + one)
let x_i i =
{ f = (fun ys -> Infinite_list.nth_exn ys i)
; f' = Lazy.from_fun (fun () -> Infinite_list.e_i i ~zero ~one)
}
let x_0 = x_i 0
let x_1 = x_i 1
let x_2 = x_i 2
let rec scale t k =
{ f = (fun ys -> Floatlike.( * ) k (eval t ys))
; f' = Lazy.from_fun (fun () -> Infinite_list.map (grad t) ~f:(fun dt -> scale dt k))
}
let rec (+) g h =
{ f = (fun ys -> Floatlike.(+) (eval g ys) (eval h ys))
; f' = Lazy.from_fun (fun () -> Infinite_list.map2 (grad g) (grad h) ~f:(+))
}
let rec (-) g h =
{ f = (fun ys -> Floatlike.(-) (eval g ys) (eval h ys))
; f' = Lazy.from_fun (fun () -> Infinite_list.map2 (grad g) (grad h) ~f:(-))
}
let rec ( * ) g h =
{ f = (fun ys -> Floatlike.( * ) (eval g ys) (eval h ys))
; f' = Lazy.from_fun (fun () -> Infinite_list.map2 (grad g) (grad h) ~f:(fun dg dh -> g * dh + dg * h))
}
let rec compose_univar g h =
{ f = (fun ys -> (Univar.eval g (eval h ys)))
; f' = Lazy.from_fun (fun () ->
let dg_of_h = compose_univar (Univar.d g) h in
Infinite_list.map (grad h) ~f:(fun dh -> dg_of_h * dh))
}
let rec compose g hs =
{ f = (fun ys -> eval g (Infinite_list.map hs ~f:(fun h -> eval h ys)))
; f' = Lazy.from_fun (fun () ->
let grad_g_of_hs =
Infinite_list.map (grad g) ~f:(fun dg -> compose dg hs)
in
let grad_hs = Infinite_list.map hs ~f:grad in
let grad_hs_transposed = Infinite_list.transpose grad_hs in
Infinite_list.map grad_hs_transposed ~f:(fun dh ->
let terms = Infinite_list.map2 grad_g_of_hs dh ~f:( * ) in
Infinite_list.fold terms ~init:zero ~f:(+) ~f_default:(fun acc _ -> acc)))
}
let compose' g hs = compose g (Infinite_list.of_list hs ~default:zero)
let rec compose_list g hss =
{ f = (fun ys ->
List.fold (List.rev hss) ~init:ys
~f:(fun inputs hs -> Infinite_list.map hs ~f:(fun h -> eval h inputs))
|> eval g)
; f' = Lazy.from_fun (fun () ->
match
List.fold (List.rev hss) ~init:(None, []) ~f:(fun (grad_acc_option, acc) hs ->
let grad_hs = Infinite_list.map hs ~f:grad in
match grad_acc_option with
| None -> (Some grad_hs, hs :: acc)
| Some grad_acc ->
let grad_hs_of_acc =
Infinite_list.map grad_hs
~f:(Infinite_list.map ~f:(fun dh -> compose_list dh acc))
in
let grad_acc_transposed = Infinite_list.transpose grad_acc in
let grad_acc_new =
Infinite_list.map grad_hs_of_acc ~f:(fun grad_h_of_acc ->
Infinite_list.map grad_acc_transposed ~f:(fun dacc ->
let terms = Infinite_list.map2 grad_h_of_acc dacc ~f:( * ) in
Infinite_list.fold terms ~init:zero ~f:(+)
~f_default:(fun acc _ -> acc)))
in
(Some grad_acc_new, hs :: acc))
with
| None, _ -> grad g
| Some grad_hss, _ ->
let grad_g_of_hss =
Infinite_list.map (grad g) ~f:(fun dg -> compose_list dg hss)
in
let grad_hss_transposed = Infinite_list.transpose grad_hss in
Infinite_list.map grad_hss_transposed ~f:(fun dh ->
let terms = Infinite_list.map2 grad_g_of_hss dh ~f:( * ) in
Infinite_list.fold terms ~init:zero ~f:(+) ~f_default:(fun acc _ -> acc)))
}
let compose_list' g hss =
compose_list g (List.map hss ~f:(fun hs -> Infinite_list.of_list hs ~default:zero))
let compose_list'' = function
| [] -> []
| gs :: hss -> List.map gs ~f:(fun g -> compose_list' g hss)
let int_pow t n = compose_univar (Univar.Uncomposed.int_pow n) t
let (/) g h = g * (int_pow h (-1))
let pow t p = compose_univar (Univar.Uncomposed.pow p) t
let exp t = compose_univar Univar.Uncomposed.exp t
let log t = compose_univar Univar.Uncomposed.log t
let ( ** ) g h = exp (h * log g)
let sin t = compose_univar Univar.Uncomposed.sin t
let cos t = compose_univar Univar.Uncomposed.cos t
let tan t = compose_univar Univar.Uncomposed.tan t
let abs t = compose_univar Univar.Uncomposed.abs t
let step t = compose_univar Univar.Uncomposed.step t
let relu t = compose_univar Univar.Uncomposed.relu t
let softplus t = compose_univar Univar.Uncomposed.softplus t
let sigmoid t = compose_univar Univar.Uncomposed.sigmoid t
end
module Float = Make(Floatlike.Float)