-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmit-categories.lhs
2191 lines (1772 loc) · 47.8 KB
/
mit-categories.lhs
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%% -*- latex -*-
% Presentation
%\documentclass[aspectratio=1610]{beamer} % Macbook Pro screen 16:10
\documentclass{beamer} % default aspect ratio 4:3
%% \documentclass[handout]{beamer}
%% %let full = True
\input{macros}
%include polycode.fmt
%include forall.fmt
%include greek.fmt
%include formatting.fmt
%% \title[]{The simple essence of automatic differentiation}
%% \subtitle{Differentiable programming made easy}
\title[]{Efficient automatic differentiation made easy \\[1.5ex] via elementary category theory}
\date{October 29, 2020}
\setlength{\itemsep}{2ex}
\setlength{\parskip}{1ex}
\setlength{\blanklineskip}{1.5ex}
\setlength\mathindent{4ex}
\nc\wow\emph
\usepackage{scalerel}
%% https://www.conference-publishing.com/Help.php
\usepackage[utf8]{inputenc}
%% \usepackage[T1]{fontenc}
%% \usepackage{microtype}
%format == = =
\begin{document}
% \large
\frame{\titlepage}
% \title{Essence of automatic differentiation}
% \title{Simple essence of AD}
\title{Efficient automatic differentiation made easy}
\date{October, 2020}
%format der = "\mathcal{D}"
\framet{What is differentiation? (Fréchet)}{\mathindent20ex
\vspace{-4ex}
On Banach spaces |a| and |b|,
> der :: (a -> b) -> (a -> (a :-* b))
\vspace{1ex}
|f a + der f a epsilon| approximates |f (a + epsilon)| for small |epsilon|.
\vspace{4ex}
$$|lim(epsilon -> 0)(frac(norm (f (a+epsilon) - (f a + der f a epsilon)))(norm epsilon)) == 0|$$
\
See \href{https://archive.org/details/SpivakM.CalculusOnManifolds_201703}{\emph{Calculus on Manifolds}} by Michael Spivak.
\vspace{-8ex}
}
%format (meaning (p)) = "\llbracket" p "\rrbracket"
%format derp = "\bar{"der"}"
\framet{What is \emph{automatic} differentiation?}{\parskip4ex \mathindent20ex
Differentiation of computable functions is not computable.
Instead, differentiate \emph{recipes}: |meaning (derp p) = der (meaning p)|.
\pause\vspace{3ex}
Popular recipe forms: graphs, imperative programs, lambda calculus.
Differentiation composes messily in these forms\pause,
\emph{but tidily in language of categories!}
}
\framet{Composition}{
Sequential:
\begin{code}
(.) :: (b -> c) -> (a -> b) -> (a -> c)
(g . f) a = g (f a)
NOP
der (g . f) a == der g (f a) . der f a -- chain rule
\end{code}
\pause
Parallel:
\begin{code}
(&&&) :: (a -> c) -> (a -> d) -> (a -> c :* d)
(f &&& g) a = (f a, g a)
NOP
der (f &&& g) a == der f a &&& der g a
\end{code}
}
%format adf = "\hat{"der"}"
%format fst = exl
%format snd = exr
\framet{Linear functions}{
%if not icfp
\pause
%endif
Linear functions are their own derivatives everywhere.
% perfect linear approximations.
\vspace{2ex}
\begin{code}
der id a = id
der fst a = fst
der snd a = snd
...
\end{code}
}
\framet{Compositionality}{
Chain rule:
> der (g . f) a == der g (f a) . der f a -- non-compositional
\
%if not icfp
\pause
%endif
To fix, combine regular result with derivative:
\begin{code}
adf :: (a -> b) -> (a -> (b :* (a :-* b)))
adf f = f &&& der f -- specification
\end{code}
% adf f = (f a, der f a) -- specification
so that |der f = exr . adf f|.
%if False
Often much work in common to |f| and |der f|.
%endif
}
%% \nc\scrk[1]{_{\hspace{#1}\scriptscriptstyle{(\leadsto)\!}}}
\nc\scrk[1]{_{\hspace{#1}\scaleto{(\leadsto)\!}{4pt}}}
%format ProductCat = Cartesian
%format CoproductCat = Cocartesian "\!_\times"
%format CoproductPCat = Cocartesian "\!_\times"
%format Prod (k) a b = a "\times\scrk{-0.4ex}" b
%format Coprod (k) a b = a "+\scrk{-0.4ex}" b
%% %format Exp (k) a b = a "\Rightarrow\scrk{-0.2ex}" b
%% %format da = "\Delta a"
%% %format db = "\Delta b"
%format `k` = "\leadsto"
%format k = "(\leadsto)"
%% %format `k` = "\rightsquigarrow"
%% %format k = "(\rightsquigarrow)"
\framet{Abstract algebra for functions}{
\begin{code}
class Category k where
id :: a `k` a
(.) :: (b `k` c) -> (a `k` b) -> (a `k` c)
NOP
class Category k => ProductCat k where
exl :: (a :* b) `k` a
exr :: (a :* b) `k` b
(&&&) :: (a `k` c) -> (a `k` d) -> (a `k` (c :* d))
\end{code}
\vspace{3ex}
Plus laws and classes for arithmetic etc.
}
%% class Category k => CoproductCat k where
%% type Coprod k a b
%% inl :: a `k` (Coprod k a b)
%% inr :: b `k` (Coprod k a b)
%% (|||) :: (a `k` c) -> (b `k` c) -> ((Coprod k a b) `k` c)
%if False
\framet{Homomorphisms}{
A \emph{functor} |F| maps arrows from one category to another, preserving |Category| structure:
\begin{code}
F id == id
F (g . f) == F g . F f
\end{code}
\
\pause
A \emph{cartesian functor} |F| additionally preserves |cartesian| structure:
\begin{code}
F exl == exl
F exr == exr
F (f &&& g) == F f &&& F g
\end{code}
}
%endif
\framet{Automatic differentiation (specification)}{
\begin{code}
newtype D a b = D (a -> b :* (a :-* b))
adf :: (a -> b) -> D a b
adf f = D (f &&& der f) -- not computable
\end{code}
\pause
Specification: |adf| is a cartesian functor, i.e.,
{\setlength{\blanklineskip}{1ex}
\begin{minipage}[c]{0.49\textwidth} % \mathindent1em
\begin{code}
adf id == id
adf (g . f) == adf g . adf f
\end{code}
\end{minipage}
% \hspace{1ex}
\begin{minipage}[c]{0.49\textwidth} % \mathindent1em
\begin{code}
adf exl == exl
adf exr == exr
adf (f &&& g) == adf f &&& adf g
\end{code}
\end{minipage}
}
%\pause
\emph{The game:} solve these equations for the RHS operations.
}
\framet{Automatic differentiation (solution)}{
\mathindent-1ex
\begin{code}
newtype D a b = D (a -> b :* (a :-* b))
\end{code}
%% \pause
\vspace{-4ex}
\begin{code}
linearD f = D (\ a -> (f a, f))
instance Category D where
id = linearD id
D g . D f = D (\ a -> let { (b,f') = f a ; (c,g') = g b } in (c, g' . f'))
instance Cartesian D where
exl = linearD exl
exr = linearD exr
D f &&& D g = D (\ a -> let { (b,f') = f a ; (c,g') = g a } in ((b,c), f' &&& g'))
instance NumCat D where
negateC = linearD negateC
addC = linearD addC
mulC = D (mulC &&& (\ (a,b) -> \ (da,db) -> b*da + a*db))
\end{code}
}
%if True
\framet{Generalizing AD}{
\mathindent-1ex
\begin{code}
newtype D a b = D (a -> b :* (a :-* b))
\end{code}
%% \pause
\vspace{-4ex}
\begin{code}
linearD f = D (\ a -> (f a, f))
instance Category D where
id = linearD id
D g . D f = D (\ a -> let { (b,f') = f a ; (c,g') = g b } in (c, g' . f'))
instance Cartesian D where
exl = linearD exl
exr = linearD exr
D f &&& D g = D (\ a -> let { (b,f') = f a ; (c,g') = g a } in ((b,c), f' &&& g'))
\end{code}
\vspace{1.5ex}
%% \pause
Each |D| operation just uses corresponding |(:-*)| operation.\\[2ex]
Generalize from |(:-*)| to other cartesian categories.\\[4.1ex]
}
%% %format GD = GAD
%format (GD (k)) = D"_{"k"}"
%% %format GD (k) a b = a "\leadsto_{"k"}" b
\framet{Generalized AD}{
\mathindent-1ex
\begin{code}
newtype GD k a b = D (a -> b :* (a `k` b))
\end{code}
%% \pause
\vspace{-4ex}
\begin{code}
linearD f f' = D (\ a -> (f a, f'))
instance Category k => Category (GD k) where
id = linearD id id
D g . D f = D (\ a -> let { (b,f') = f a ; (c,g') = g b } in (c, g' . f'))
instance Cartesian k => Cartesian (GD k) where
exl = linearD exl exl
exr = linearD exr exr
D f &&& D g = D (\ a -> let { (b,f') = f a ; (c,g') = g a } in ((b,c), f' &&& g'))
\end{code}
\vspace{-5ex}
\begin{code}
instance SPC ... => NumCat D where
negateC = linearD negateC negateC
addC = linearD addC addC
mulC = ??
\end{code}
}
%endif
%format inlP = inl
%format inrP = inr
%format |||| = |||
%format ++++ = +++
\framet{Numeric operations}{
Specific to (linear) \emph{functions}:
> mulC = D (mulC &&& (\ (a,b) -> \ (da,db) -> b*da + a*db))
%if not icfp
\pause
%endif
Rephrase:
\begin{code}
scale :: Multiplicative a => a -> (a -> a)
scale u = \ v -> u * v
(||||) :: Additive c => (a -> c) -> (b -> c) -> ((a :* b) -> c)
(f |||| g) (a,b) = f a ^+^ g b
\end{code}
Now
> mulC = D (mulC &&& (\ (a,b) -> scale b |||| scale a))
}
%if not icfp
\framet{New generalized vocabulary}{
\begin{code}
class Category k => CoproductCat k where
inl :: a `k` (a :* b)
inr :: b `k` (a :* b)
(|||) :: (a `k` c) -> (b `k` c) -> ((a :* b) `k` c)
NOP
class ScalarCat k a where
scale :: a -> (a `k` a)
\end{code}
Differentiation:
\begin{code}
der (f ||| g) (a,b) == der f a ||| der g b
\end{code}
The rest are linear.
}
%endif
%format Double = R
%% %format -+> = "\mathbin{\rightarrow^{\!\!+}\!}"
%% %format -+> = "\rightarrow_{\!\!+}"
%% %format -+> = "\overset{+}{\longrightarrow}"
%% %format -+> = "\overset{{}_{+}}{\longrightarrow}"
%% %format -+> = "\rightarrow\hspace{-3ex}^{\scriptscriptstyle +}\hspace{2ex}"
%% %format -+> = "\mathbin{\longrightarrow\hspace{-3ex}{+}\hspace{0.7ex}}"
%% %format -+> = "\mathbin{\rightarrow\hspace{-2.4ex}{\tiny \circ}\hspace{0.5ex}}"
%format -+> = "\rightarrowtriangle"
%format inNew2
%format inAbst2 = inNew2
%format joinP = join
%format unjoinP = unjoin
\framet{Linear maps as functions}{
\vspace{-1.2ex}
\begin{code}
newtype a -+> b = LFun (a -> b) -- \emph{linear}
instance Category (-+>) where
id = LFun id
(.) = inNew2 (.)
instance ProductCat (-+>) where
exl = LFun exl
exr = LFun exr
(&&&) = inNew2 (&&&)
instance CoproductPCat (-+>) where
inlP = LFun (\ a -> (a,zeroV))
inrP = LFun (\ b -> (zeroV,b))
(||||) = inNew2 (\ f g (a,b) -> f a ^+^ g b)
instance Multiplicative s => ScalarCat (-+>) s where
scale s = LFun (s NOP *)
\end{code}
}
%format toV = "\Varid{to}_V"
%format unV = "\Varid{un}_V"
%format (LC (s)) = M"_{"s"}"
%format V (s) = V"_{"s"}"
%format HasV (s) = HasV"_{"s"}"
\framet{Extracting a data representation}{
\begin{itemize}
\itemsep4ex
% \parskip2ex
\item Finally, extract a matrix or gradient vector.
\item Very inefficient for gradient-based optimization!
\item Alternatively, represent as ``generalized matrices'' (|LC s a b|).\\
Then solve more homomorphisms.
\end{itemize}
}
\framet{Efficiency of composition}{
\begin{itemize}
\itemsep2ex \parskip0.5ex
\item
Composition is associative.
\item
Some associations are more efficient than others, so
\begin{itemize}\itemsep2ex
\item Associate optimally.
\item Equivalent to \emph{matrix chain multiplication} --- $O(n \log n)$.
\item Choice determined by \emph{types}, i.e., compile-time information.
\end{itemize}
\pause
\item
All right: ``forward mode AD'' (FAD).
\item
All left: ``reverse mode AD'' (RAD).
\item
RAD is \emph{much} better for gradient-based optimization.
\end{itemize}
}
%format --> = "\mapsto"
\framet{Left-associating composition (RAD)}{
CPS-like category:
\vspace{2ex}
\begin{itemize}\itemsep4ex
\item Represent |a `k` b| by |(b `k` r) -> (a `k` r)|.
\item Meaning:
%% |ab --> (\ br -> br . ab)|.
|f' --> (\ h -> h . f')|.
%% |f' --> (. NOP f')|.
%if False
\item Results in left-composition.
\item Initialize with |id :: r `k` r|.
%endif
%% \item Corresponds to a categorical pullback.
\item Construct |h . der f a| directly, without |der f a|.\\
%% Often eliminates large \& sparse matrices.
\end{itemize}
\vspace{2ex}
Old technique (Cayley 1854), vastly generalized by Yoneda.
}
%if True
\framet{Continuation category (specification)}{ %\mathindent0ex
\setlength{\blanklineskip}{1ex}
%% \vspace{-1.3ex}
%% type Ok (ContC k r) = Ok k
\begin{code}
newtype ContC k r a b = Cont ((b `k` r) -> (a `k` r))
NOP
cont :: Category k => (a `k` b) -> ContC k r a b
cont f = Cont (. NOP f)
\end{code}
\vspace{2ex}
Specification: |cont| is a cartesian functor.
%if True
\pause\vspace{3ex}
We'll use an isomorphism:
\begin{code}
join :: Cocartesian k => (c `k` a) :* (d `k` a) -> ((c :* d) `k` a)
unjoin :: Cocartesian k => ((c :* d) `k` a) -> (c `k` a) :* (d `k` a)
join (f,g) = f ||| g
unjoin h = (h . inl, h . inr)
\end{code}
%endif
}
\framet{Continuation category (solution)}{\mathindent0ex
\begin{code}
instance Category k => Category (ContC k r) where
id = Cont id
Cont g . Cont f = Cont (f . g)
instance ProductCat k => ProductCat (ContC k r) where
exl = Cont (join . inl)
exr = Cont (join . inr)
(&&&) = inNew2 (\ f g -> (f ||| g) . unjoin)
instance CoproductCat k => CoproductCat (ContC k r) where
inl = Cont (exl . unjoin)
inr = Cont (exr . unjoin)
(|||) = inNew2 (\ f g -> join . (f &&& g))
instance ScalarCat k a => ScalarCat (ContC k r) a where
scale s = Cont (scale s)
\end{code}}
%endif
\framet{Reverse-mode AD without tears}{\mathindent2in
%if full
\pause
%endif
\begin{code}
GD (ContC (LC s) r)
\end{code}
}
%if full
\framet{Left-associating composition (RAD)}{
\begin{itemize}\itemsep2ex \parskip1ex
\item CPS-like category:
\begin{itemize}\itemsep2ex
\item Represent |a `k` b| by |(b `k` r) -> (a `k` r)|.
\item Meaning:
|f --> (. NOP f)|.
\item Results in left-composition.
\item Initialize with |id :: r `k` r|.
%% \item Corresponds to a categorical pullback.
\item Construct |h . der f a| directly, without |der f a|.\\
%% Often eliminates large \& sparse matrices.
\end{itemize}
\pitem We've seen this trick before:
\begin{itemize}\itemsep2ex
\item Transforming naive |reverse| from quadratic to linear.
\item List generalizes to monoids, and monoids to categories.
\end{itemize}
\end{itemize}
}
\framet{One of my favorite papers}{
\begin{itemize}\itemsep3ex \parskip1ex
\item \href{http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.83.8567}{\emph{Continuation-Based Program Transformation Strategies}} \\ Mitch Wand, 1980, JACM.
\item Introduce a continuation argument, e.g., |[a] -> [a]|.
\item Notice the continuations that arise, e.g., |(++ NOP as)|.
\pitem Find a \emph{data} representation, e.g., |as :: [a]|
\item Identify associative operation that represents composition,\\
e.g., |(++)| , since |(++ NOP bs) . (++ NOP as) == (++ NOP (as ++ bs))|.
\end{itemize}
}
%endif
%format Du u = u "^{*}"
\framet{Duality}{
%\pause
\begin{itemize}
%if icfp
\itemsep4ex
%else
\itemsep3ex
%endif
\item Vector space dual: |Du u = u :-* s|, with |u| a vector space over |s|.
\item If |u| has finite dimension, then |Du u =~= u|.
%if full
\item For |f :: Du u|, |f == dot v| for some |v :: u|.
\item Gradients are derivatives of functions with scalar codomain.
%endif
\item Represent |a :-* b| by |Du b -> Du a| by |b -> a|.
\item \emph{Ideal} for extracting gradient vector.
Just apply to |1| (|id|).
%% \item Often don't need vector space; semi-module will do.
%% \item Semimodule suffices in place of vector space.
\end{itemize}
}
%format (DualC (k)) = Dual"_{"k"}"
%format unDot = dot"^{-1}"
\framet{Duality (specification)}{
\begin{code}
newtype DualC k a b = Dual (b `k` a)
asDual :: ContC k s a b -> DualC k a b
asDual (Cont f) = Dual (unDot . f . dot)
\end{code}
where
\begin{code}
dot :: u -> (u :-* s)
unDot :: (u :-* s) -> u
\end{code}
\vspace{2ex}
Specification: |asDual| is a cartesian functor.
%% Require |asDual| to preserve structure. Solve for methods.
}
\framet{Duality (solution)}{
\begin{code}
newtype DualC k a b = Dual (b `k` a)
instance Category k => Category (DualC k) where
id = Dual id
(.) = inNew2 (flip (.))
instance CoproductCat k => ProductCat (DualC k) where
exl = Dual inlP
exr = Dual inrP
(&&&) = inNew2 (||||)
instance ProductCat k => CoproductPCat (DualC k) where
inlP = Dual exl
inrP = Dual exr
(||||) = inNew2 (&&&)
instance ScalarCat k s => ScalarCat (DualC k) s where
scale s = Dual (scale s)
\end{code}
}
\framet{Backpropagation}{\mathindent2in
\pause
\begin{code}
GD (DualC (-+>))
\end{code}
}
\framet{Conclusions}{
\begin{itemize}\itemsep2.5ex
\item Simple AD algorithm, specializing to forward, reverse, mixed.
\item No graphs, tapes, tags, partial derivatives, or mutation.
\item Parallel-friendly and possibly low memory use.
\item Calculated
% rigorously
from simple, regular
algebra problems.
%% homomorphic specifications.
%% \item One rule per combining form: |(.)|, |(&&&)|, |(###)|.
%% \item RAD as simple as FAD but very efficient for gradient problems.
%% \item Reverse mode via simple, general constructions.
\item Generalizes to derivative categories other than linear maps.
\item Differentiate regular Haskell code (via plugin).
\item \href{http://conal.net/papers/essence-of-ad/}{ICFP 2018 paper}: pictures, proofs, incremental computation.
\end{itemize}
}
%if True
\framet{\hypertarget{Examples: derivatives as functions}{Running examples}}{
\begin{code}
sqr :: Num a => a -> a
sqr a = a * a
magSqr :: Num a => a :* a -> a
magSqr (a,b) = sqr a + sqr b
cosSinProd :: Floating a => a :* a -> a :* a
cosSinProd (x,y) = (cos z, sin z) where z = x * y
\end{code}
\pause
In categorical vocabulary:
\begin{code}
sqr = mulC . (id &&& id)
magSqr = addC . ((sqr . exl) &&& (sqr . exr))
cosSinProd = (cosC &&& sinC) . mulC
\end{code}
}
\framet{Visualizing computations}{
\begin{code}
magSqr (a,b) = sqr a + sqr b
NOP
magSqr = addC . ((sqr . exl) &&& (sqr . exr))
\end{code}
\vspace{-5ex}
\begin{center}\wpicture{4in}{magSqr}\end{center}
Auto-generated from Haskell code.
See \href{http://conal.net/papers/compiling-to-categories/}{\emph{Compiling to categories}}.
}
\framet{AD example}{
\vspace{4ex}
\begin{code}
sqr a = a * a
sqr = mulC . (id &&& id)
\end{code}
\begin{textblock}{160}[1,0](357,37)
\begin{tcolorbox}
\wpicture{2in}{sqr}
\end{tcolorbox}
\end{textblock}
\pause
\vspace{0ex}
\begin{center}\wpicture{4.8in}{sqr-adf}\end{center}
}
\framet{AD example}{
\vspace{8ex}
\begin{code}
magSqr (a,b) = sqr a + sqr b
magSqr = addC . ((sqr . exl) &&& (sqr . exr))
\end{code}
\begin{textblock}{160}[1,0](357,37)
\begin{tcolorbox}
\wpicture{2in}{magSqr}
\end{tcolorbox}
\end{textblock}
\pause
\vspace{-4ex}
\begin{center}\wpicture{4.5in}{magSqr-adf}\end{center}
}
\framet{AD example}{
\vspace{12ex}
\begin{code}
cosSinProd (x,y) = (cos z, sin z) where z = x * y
cosSinProd = (cosC &&& sinC) . mulC
\end{code}
\begin{textblock}{160}[1,0](357,37)
\begin{tcolorbox}
\wpicture{2in}{cosSinProd}
\end{tcolorbox}
\end{textblock}
\pause
\begin{center}\wpicture{4.5in}{cosSinProd-adf}\end{center}
}
\framet{\hypertarget{Examples: reverse mode}{RAD example (dual function)}}{
\begin{textblock}{160}[1,0](357,37)
\begin{tcolorbox}
\wpicture{2in}{add}
\end{tcolorbox}
\end{textblock}
\vspace{10ex}
\begin{center}\hspace{-5ex}\wpicture{4in}{add-adr}\end{center}
}
\framet{RAD example (dual vector)}{
\begin{textblock}{160}[1,0](357,37)
\begin{tcolorbox}
\wpicture{2in}{add}
\end{tcolorbox}
\end{textblock}
\vspace{12ex}
\begin{center}\hspace{-5ex}\wpicture{4in}{add-gradr}\end{center}
}
\framet{RAD example (dual function)}{
\begin{textblock}{160}[1,0](357,37)
\begin{tcolorbox}
\wpicture{2in}{dup}
\end{tcolorbox}
\end{textblock}
\vspace{13ex}
\begin{center}\hspace{-5ex}\wpicture{4.5in}{dup-adr}\end{center}
}
\framet{RAD example (vector)}{
\begin{textblock}{160}[1,0](357,37)
\begin{tcolorbox}
\wpicture{2in}{dup}
\end{tcolorbox}
\end{textblock}
\vspace{12ex}
\begin{center}\hspace{-5ex}\wpicture{2.5in}{dup-gradr}\end{center}
}
\framet{RAD example (dual function)}{
\begin{textblock}{130}[1,0](357,37)
\begin{tcolorbox}
\wpicture{1.5in}{fst}
\end{tcolorbox}
\end{textblock}
\vspace{8ex}
\begin{center}\hspace{-8ex}\wpicture{3.7in}{fst-adr}\end{center}
}
\framet{RAD example (dual vector)}{
\begin{textblock}{130}[1,0](357,37)
\begin{tcolorbox}
\wpicture{1.5in}{fst}
\end{tcolorbox}
\end{textblock}
\vspace{4ex}
\begin{center}\hspace{0ex}\wpicture{2.5in}{fst-gradr}\end{center}
}
\framet{RAD example (dual function)}{
\begin{textblock}{160}[1,0](357,37)
\begin{tcolorbox}
\wpicture{2in}{sqr}
\end{tcolorbox}
\end{textblock}
\vspace{12ex}
\begin{center}\hspace{0ex}\wpicture{4.5in}{sqr-adr}\end{center}
}
\framet{RAD example (dual vector)}{
\begin{textblock}{160}[1,0](357,37)
\begin{tcolorbox}
\wpicture{2in}{sqr}
\end{tcolorbox}
\end{textblock}
\vspace{11ex}
\begin{center}\hspace{-2ex}\wpicture{4in}{sqr-gradr}\end{center}
}
\framet{RAD example (dual function)}{
\vspace{2ex}
\begin{textblock}{160}[1,0](357,37)
\begin{tcolorbox}
\wpicture{2in}{magSqr}
\end{tcolorbox}
\end{textblock}
\vspace{4ex}
\begin{center}\hspace{-2ex}\wpicture{4.5in}{magSqr-adr}\end{center}
}
\framet{RAD example (dual vector)}{
\begin{textblock}{160}[1,0](357,37)
\begin{tcolorbox}
\wpicture{2in}{magSqr}
\end{tcolorbox}
\end{textblock}
\vspace{7ex}
\begin{center}\hspace{-4ex}\wpicture{3.8in}{magSqr-gradr}\end{center}
}
\framet{RAD example (dual function)}{
\begin{textblock}{160}[1,0](357,37)
\begin{tcolorbox}
\wpicture{2in}{cosSinProd}
\end{tcolorbox}
\end{textblock}
\vspace{10ex}
\begin{center}\hspace{-0.5ex}\wpicture{4.8in}{cosSinProd-adr}\end{center}
}
\framet{RAD example (matrix)}{
\begin{textblock}{160}[1,0](357,37)
\begin{tcolorbox}
\wpicture{2in}{cosSinProd}
\end{tcolorbox}
\end{textblock}
\vspace{10.5ex}
\begin{center}\hspace{-2ex}\wpicture{4.4in}{cosSinProd-adrl}\end{center}
}
%endif
%format ### = ||||
\framet{Reflections: recipe for success}{
\pause
Key principles:
\begin{itemize}\itemsep2.5ex
\item
Capture main concepts as first-class values.
\item
Focus on abstract notions, not specific representations.
\item
Calculate efficient implementation from simple specification.
\end{itemize}
Not previously applied to AD (afaik).
\pause\vspace{4ex}
\emph{Quandary:} Most programming languages poor for function-like things.
\pause\vspace{2ex}
\emph{Solution:} \emph{\href{http://conal.net/papers/compiling-to-categories}{Compiling to categories}}.
}
%if not icfp
\framet{Symbolic vs automatic differentiation}{
Often described as opposing techniques:
\begin{itemize}\itemsep2ex
\item \emph{Symbolic}:
\begin{itemize}\itemsep1.5ex
\item Apply differentiation rules symbolically.
\item Can duplicate much work.
\item Needs algebraic manipulation.
\end{itemize}
\item \emph{Automatic}:
\begin{itemize}\itemsep1.5ex
\item FAD: easy to implement but often inefficient.
\item RAD: efficient but tricky to implement.
\end{itemize}
\end{itemize}
\pause
\vspace{2ex}
My view: \emph{AD is SD done by a compiler.}\\[2ex]
Compilers already work symbolically and preserve sharing.
}
%endif
%if full
\nc\link[1]{\hyperlink{#1}{#1}}
\nc\litem[1]{\item \link{#1}}
\framet{}{}
\framet{Extra topics}{
\begin{itemize}\itemsep4ex
\litem{Examples: derivatives as functions}
\litem{Examples: reverse mode}
\litem{Incremental evaluation}
\litem{Compositional matrices}
%% \litem{Linear arrow (biproduct) vocabulary}
%% \litem{Symbolic vs automatic differentiation}
\end{itemize}
}
%endif
\end{document}
%% -*- latex -*-
% Presentation
%\documentclass[aspectratio=1610]{beamer} % Macbook Pro screen 16:10
%% \documentclass{beamer} % default aspect ratio 4:3
\documentclass[handout]{beamer}
% \setbeameroption{show notes} % un-comment to see the notes
%let full = not (icfp || google)
\input{macros}
%include polycode.fmt
%include forall.fmt
%include greek.fmt
%include formatting.fmt