forked from nouiz/ccw_tutorial_theano
-
Notifications
You must be signed in to change notification settings - Fork 34
/
presentation.tex
728 lines (649 loc) · 18.4 KB
/
presentation.tex
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
\documentclass[utf8x,xcolor=pdftex,dvipsnames,table]{beamer}
\usetheme{Malmoe} % Now it's a beamer presentation with the lisa theme!
\setbeamertemplate{footline}[page number]
\usecolortheme{beaver}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage[utf8x]{inputenc}
%\logo{\includegraphics[width=.8in]{UdeM_NoirBleu_logo_Marie_crop}}
\usepackage{listings}
\newcommand{\superscript}[1]{\ensuremath{^{\textrm{#1}}}}
\mode<presentation>
\title{Introduction to Theano}
\author{%
\footnotesize
Frédéric Bastien \newline
(slides highly copied from previous tutorial by Ian G.) \newline
}
\date{September 25, 2014}
\setbeamertemplate{navigation symbols}{}
\begin{document}
\begin{frame}[plain]
\titlepage
\vspace{-5em}
% \includegraphics[width=1in]{../hpcs2011_tutorial/pics/lisabook_logo_text_3.png}
\hfill
% \includegraphics[width=.8in]{../hpcs2011_tutorial/pics/UdeM_NoirBleu_logo_Marie_crop}
\end{frame}
\section{Outline}
\begin{frame}{High level}\setcounter{page}{1}
\begin{itemize}
\item Overview of library (3 min)
\item Building expressions (30 min)
\item Compiling and running expressions (30 min)
\item Modifying expressions (25 min)
\item Debugging (30 min)
\item Citing Theano (2 min)
\end{itemize}
\end{frame}
\begin{frame}{Overview of Library}
Theano is many things
\begin{itemize}
\item Language
\item Compiler
\item Python library
\end{itemize}
\end{frame}
\begin{frame}{Overview}
Theano language:
\begin{itemize}
\item Operations on scalar, vector, matrix, tensor, and sparse variables
\item Linear algebra
\item Element-wise nonlinearities
\item Convolution
\item Extensible
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Overview}
Using Theano:
\begin{itemize}
\item define expression $f(x,y) = x + y$
\item compile expression
\lstset{language=Python,
commentstyle=\itshape\color{blue},
stringstyle=\color{violet},
}
\begin{lstlisting}
int f(int x, int y){
return x + y;
}
\end{lstlisting}
\item execute expression
\lstset{language=Python,
commentstyle=\itshape\color{blue},
stringstyle=\color{violet},
}
\begin{lstlisting}
>>> f(1, 2)
3
\end{lstlisting}
\end{itemize}
\end{frame}
\section{Building}
\begin{frame}{Building expressions}
\begin{itemize}
\item Scalars
\item Vectors
\item Matrices
\item Tensors
\item Reduction
\item Dimshuffle
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Scalar math}
Using Theano:
\begin{itemize}
\item define expression $f(x,y) = x + y$
\item compile expression
\end{itemize}
\lstset{language=Python,
commentstyle=\itshape\color{blue},
stringstyle=\color{violet},
}
\begin{lstlisting}
from theano import tensor as T
x = T.scalar()
y = T.scalar()
z = x+y
w = z*x
a = T.sqrt(w)
b = T.exp(a)
c = a ** b
d = T.log(c)
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Vector math}
\lstset{language=Python,
commentstyle=\itshape\color{blue},
stringstyle=\color{violet},
}
\begin{lstlisting}
from theano import tensor as T
x = T.vector()
y = T.vector()
# Scalar math applied elementwise
a = x * y
# Vector dot product
b = T.dot(x, y)
# Broadcasting
c = a + b
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Matrix math}
\lstset{language=Python,
commentstyle=\itshape\color{blue},
stringstyle=\color{violet},
}
\begin{lstlisting}
from theano import tensor as T
x = T.matrix()
y = T.matrix()
a = T.vector()
# Matrix-matrix product
b = T.dot(x, y)
# Matrix-vector product
c = T.dot(x, a)
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Tensors}
Using Theano:
\begin{itemize}
\item define expression $f(x,y) = x + y$
\item compile expression
\begin{itemize}
\item Dimensionality defined by length of ``broadcastable'' argument
\item Can add (or do other elemwise op) on two
tensors with same dimensionality
\item Duplicate tensors along broadcastable axes to
make size match
\end{itemize}
\end{itemize}
\lstset{language=Python,
commentstyle=\itshape\color{blue},
stringstyle=\color{violet},
}
\begin{lstlisting}
from theano import tensor as T
tensor3 = T.TensorType(
broadcastable=(False, False, False),
dtype='float32')
x = tensor3()
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Reductions}
Using Theano:
\begin{itemize}
\item define expression $f(x,y) = x + y$
\item compile expression
\end{itemize}
\lstset{language=Python,
commentstyle=\itshape\color{blue},
stringstyle=\color{violet},
}
\begin{lstlisting}
from theano import tensor as T
tensor3 = T.TensorType(
broadcastable=(False, False, False),
dtype='float32')
x = tensor3()
total = x.sum()
marginals = x.sum(axis=(0, 2))
mx = x.max(axis=1)
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Dimshuffle}
\lstset{language=Python,
commentstyle=\itshape\color{blue},
stringstyle=\color{violet},
}
\begin{lstlisting}
from theano import tensor as T
tensor3 = T.TensorType(broadcastable=(False, False, False), dtype=''float32'')
x = tensor3()
y = x.dimshuffle((2, 1, 0))
a = T.matrix()
b = a.T
# Same as b
c = a.dimshuffle((0, 1))
# Adding to larger tensor
d = a.dimshuffle((0, 1, ``x''))
e = a + d
\end{lstlisting}
\end{frame}
\begin{frame}{Exercices}
Work through the ``01\_buildbing\_expressions'' directory now.
Available at ``git~clone~https://github.com/nouiz/ccw\_tutorial\_theano.git''.
\end{frame}
\section{Compiling/Running}
\begin{frame}{Compiling and running expression}
\begin{itemize}
\item theano.function
\item shared variables and updates
\item compilation modes
\item compilation for GPU
\item optimizations
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{theano.function}
\lstset{language=Python,
commentstyle=\itshape\color{blue},
stringstyle=\color{violet},
}
\begin{lstlisting}
>>> from theano import tensor as T
>>> x = T.scalar()
>>> y = T.scalar()
>>> from theano import function
>>> # first arg is list of SYMBOLIC inputs
>>> # second arg is SYMBOLIC output
>>> f = function([x, y], x + y)
>>> # Call it with NUMERICAL values
>>> # Get a NUMERICAL output
>>> f(1., 2.)
array(3.0)
\end{lstlisting}
\end{frame}
\begin{frame}{Shared variables}
\begin{itemize}
\item It’s hard to do much with purely functional programming
\item ``shared variables'' add just a little bit of imperative programming
\item A “shared variable” is a buffer that stores a numerical value for a Theano variable
\item Can write to as many shared variables as you want, once each, at the end of the function
\item Modify outside Theano function with get\_value() and set\_value() methods.
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Shared variable example}
\lstset{language=Python,
commentstyle=\itshape\color{blue},
stringstyle=\color{violet},
}
\begin{lstlisting}
>>> from theano import shared
>>> x = shared(0.)
>>> from theano.compat.python2x import OrderedDict
>>> updates = OrderedDict()
>>> updates[x] = x + 1
>>> f = function([], updates=updates)
>>> f()
>>> x.get\_value()
1.0
>>> x.set\_value(100.)
>>> f()
>>> x.get\_value()
101.0
\end{lstlisting}
\end{frame}
\begin{frame}{Which dict?}
\begin{itemize}
\item Use theano.compat.python2x.OrderedDict
\item Not collections.OrderedDict
\begin{itemize}
\item This isn’t available in older versions of python,
and will limit the portability of your code
\end{itemize}
\item Not \{\} aka dict
\begin{itemize}
\item The iteration order of this built-in class is not
deterministic (thanks, Python!) so if Theano
accepted this, the same script could compile
different C programs each time you run it
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}{Compilation modes}
\begin{itemize}
\item Can compile in different modes to get different kinds of programs
\item Can specify these modes very precisely with arguments to theano.function
\item Can use a few quick presets with environment variable flags
\end{itemize}
\end{frame}
\begin{frame}{Example preset compilation modes}
\begin{itemize}
\item FAST\_RUN: default. Spends a lot of time on
compilation to get an executable that runs
fast.
\item FAST\_COMPILE: Doesn’t spend much time
compiling. Executable usually uses python
instead of compiled C code. Runs slow.
\item DEBUG\_MODE: Adds lots of checks.
Raises error messages in situations other
modes regard as fine.
\end{itemize}
\end{frame}
\begin{frame}{Compilation for GPU}
\begin{itemize}
\item Theano current back-end only supports 32 bit on GPU
\item CUDA supports 64 bit, but is slow in gamer card
\item T.fscalar, T.fvector, T.fmatrix are all 32 bit
\item T.scalar, T.vector, T.matrix resolve to 32 bit or 64 bit depending on theano’s floatX flag
\item floatX is float64 by default, set it to float32
\item Set device flag to gpu (or a specific gpu, like gpu0)
\end{itemize}
\end{frame}
\begin{frame}{Optimizations}
\begin{itemize}
\item Theano changes the symbolic expressions
you write before converting them to C code
\item It makes them faster
\begin{itemize}
\item (x+y)+(x+y) -> 2 (x + y)
\end{itemize}
\item It makes them more stable
\begin{itemize}
\item exp(a)/exp(a).sum()->softmax(a)
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Optimizations}
\begin{itemize}
\item Sometimes optimizations discard error
checking and produce incorrect output
rather than an exception
\end{itemize}
\lstset{language=Python,
commentstyle=\itshape\color{blue},
stringstyle=\color{violet},
}
\begin{lstlisting}
>>> x = T.scalar()
>>> f = function([x], x/x)
>>> f(0.)
array(1.0)
\end{lstlisting}
\end{frame}
\begin{frame}{Exercises}
Work through the ``02\_compiling\_and\_running'' directory now
\end{frame}
\section{Modifying expressions}
\begin{frame}{Modifying expressions}
\begin{itemize}
\item The grad method
\item Variable nodes
\item Types
\item Ops
\item Apply nodes
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{The grad method}
\lstset{language=Python,
commentstyle=\itshape\color{blue},
stringstyle=\color{violet},
}
\begin{lstlisting}
>>> x = T.scalar('x')
>>> y = 2. * x
>>> g = T.grad(y, x)
>>> from theano.printing import min_informative_str
>>> print min_informative_str(g)
A. Elemwise{mul}
B. Elemwise{second,no_inplace}
C. Elemwise{mul,no_inplace}
D. TensorConstant{2.0}
E. x
F. TensorConstant{1.0}
<D>
\end{lstlisting}
\end{frame}
\begin{frame}{Theano Variables}
\begin{itemize}
\item A Variable is a theano expression
\item Can come from T.scalar, T.matrix, etc.
\item Can come from doing operations on other Variables
\item Every Variable has a type field, identifying its Type \newline
e.g. TensorType((True, False), ‘float32’)
\item Variables can be thought of as nodes in a graph
\end{itemize}
\end{frame}
\begin{frame}{Ops}
\begin{itemize}
\item An Op is any class that describes a
mathematical function of some variables
\item Can call the op on some variables to get a
new variable or variables
\item An Op class can supply other forms of
information about the function, such as its
derivatives
\end{itemize}
\end{frame}
\begin{frame}{Apply nodes}
\begin{itemize}
\item The Apply class is a specific instance of an application of an Op
\item Notable fields:
\begin{itemize}
\item op: The Op to be applied
\item inputs: The Variables to be used as input
\item outputs: The Variables produced
\end{itemize}
\item Variable.owner identifies the Apply that created the variable
\item Variable and Apply instances are nodes and owner/
inputs/outputs identify edges in a Theano graph
\end{itemize}
\end{frame}
\begin{frame}{Exercises}
Work through the ``03\_modifying'' directory now
\end{frame}
\section{Debugging}
\begin{frame}{Debugging}
\begin{itemize}
\item DEBUG\_MODE
\item Error message
\item theano.printing.debugprint
\item min\_informative\_str
\item compute\_test\_value
\item Accessing the FunctionGraph
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Error message: code}
\lstset{language=Python,
commentstyle=\itshape\color{blue},
stringstyle=\color{violet},
}
\begin{lstlisting}
import numpy as np
import theano
import theano.tensor as T
x = T.vector()
y = T.vector()
z = x + x
z = z + y
f = theano.function([x, y], z)
f(np.ones((2,)), np.ones((3,)))
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Error message: 1st part}
\lstset{language=Python,
commentstyle=\itshape\color{blue},
stringstyle=\color{violet},
}
\begin{lstlisting}
Traceback (most recent call last):
[...]
ValueError: Input dimension mis-match.
(input[0].shape[0] = 3, input[1].shape[0] = 2)
Apply node that caused the error:
Elemwise{add,no_inplace}(<TensorType(float64, vector)>,
<TensorType(float64, vector)>,
<TensorType(float64, vector)>)
Inputs types: [TensorType(float64, vector),
TensorType(float64, vector),
TensorType(float64, vector)]
Inputs shapes: [(3,), (2,), (2,)]
Inputs strides: [(8,), (8,), (8,)]
Inputs scalar values: ['not scalar', 'not scalar', 'not scalar']
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Error message: 2st part}
\lstset{language=Python,
commentstyle=\itshape\color{blue},
stringstyle=\color{violet},
}
\begin{lstlisting}
HINT: Re-running with most Theano optimization
disabled could give you a back-traces when this
node was created. This can be done with by setting
the Theano flags optimizer=fast_compile
HINT: Use the Theano flag 'exception_verbosity=high'
for a debugprint of this apply node.
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Error message: exception\_verbosity=high}
\lstset{language=Python,
commentstyle=\itshape\color{blue},
stringstyle=\color{violet},
}
\begin{lstlisting}
Debugprint of the apply node:
Elemwise{add,no_inplace} [@A] <TensorType(float64, vector)> ''
|<TensorType(float64, vector)> [@B] <TensorType(float64, vector)>
|<TensorType(float64, vector)> [@C] <TensorType(float64, vector)>
|<TensorType(float64, vector)> [@C] <TensorType(float64, vector)>
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Error message: optimizer=fast\_compile}
\lstset{language=Python,
commentstyle=\itshape\color{blue},
stringstyle=\color{violet},
}
\begin{lstlisting}
Backtrace when the node is created:
File "test.py", line 7, in <module>
z = z + y
File "/home/nouiz/src/Theano/theano/tensor/var.py", line 122, in __add__
return theano.tensor.basic.add(self, other)
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Error message: Traceback}
\lstset{language=Python,
commentstyle=\itshape\color{blue},
stringstyle=\color{violet},
}
\begin{lstlisting}
Traceback (most recent call last):
File "test.py", line 9, in <module>
f(np.ones((2,)), np.ones((3,)))
File "/u/bastienf/repos/theano/compile/function_module.py",
line 589, in __call__
self.fn.thunks[self.fn.position_of_error])
File "/u/bastienf/repos/theano/compile/function_module.py",
line 579, in __call__
outputs = self.fn()
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{debugprint}
\lstset{language=Python,
commentstyle=\itshape\color{blue},
stringstyle=\color{violet},
}
\begin{lstlisting}
>>> from theano.printing import debugprint
>>> debugprint(a)
Elemwise{mul,no_inplace} [@A] ''
|TensorConstant{2.0} [@B]
|Elemwise{add,no_inplace} [@C] 'z'
|<TensorType(float64, scalar)> [@D]
|<TensorType(float64, scalar)> [@E]
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{min\_informative\_str}
\lstset{language=Python,
commentstyle=\itshape\color{blue},
stringstyle=\color{violet},
}
\begin{lstlisting}
>>> x = T.scalar()
>>> y = T.scalar()
>>> z = x + y
>>> z.name = 'z'
>>> a = 2. * z
>>> from theano.printing import min_informative_str
>>> print min_informative_str(a)
A. Elemwise{mul,no_inplace}
B. TensorConstant{2.0}
C. z
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{compute\_test\_value}
\lstset{language=Python,
commentstyle=\itshape\color{blue},
stringstyle=\color{violet},
}
\begin{lstlisting}
>>> from theano import config
>>> config.compute_test_value = 'raise'
>>> x = T.vector()
>>> import numpy as np
>>> x.tag.test_value = np.ones((2,))
>>> y = T.vector()
>>> y.tag.test_value = np.ones((3,))
>>> x + y
...
ValueError: Input dimension mis-match.
(input[0].shape[0] = 2, input[1].shape[0] = 3)
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Accessing a function’s fgraph}
\lstset{language=Python,
commentstyle=\itshape\color{blue},
stringstyle=\color{violet},
}
\begin{lstlisting}
>>> x = T.scalar()
>>> y = x / x
>>> f = function([x], y)
>>> debugprint(f.maker.fgraph.outputs[0])
DeepCopyOp [@A] ''
|TensorConstant{1.0} [@B]
\end{lstlisting}
\end{frame}
\begin{frame}{Exercises}
Work through the ``04\_debugging'' directory now
\end{frame}
\section{Citing}
\begin{frame}{Citing Theano}
\begin{itemize}
\item Please cite both of the following papers in
all work that uses Theano:
\item Bastien, Frédéric, Lamblin, Pascal, Pascanu, Razvan, Bergstra, James, Goodfellow, Ian, Bergeron, Arnaud, Bouchard, Nicolas, and
Bengio,Yoshua. Theano: new features and speed improvements. Deep Learning and Unsupervised Feature Learning NIPS 2012
Workshop, 2012.
\item Bergstra, James, Breuleux, Olivier, Bastien, Frédéric, Lamblin, Pascal, Pascanu, Razvan, Desjardins, Guillaume, Turian, Joseph, Warde-
Farley, David, and Bengio,Yoshua. Theano: a CPU and GPU math expression compiler. In Proceedings of the Python for Scientific
Computing Conference (SciPy), June 2010. Oral Presentation.
\end{itemize}
\end{frame}
\begin{frame}{Example acknowledgments}
We would like to thank the developers of
Theano \\citep\{bergstra+al:2010-scipy,Bastien-Theano-2012\},
Pylearn2 \\citep\{pylearn2\_arxiv\_2013\}. We would also like
to thank NSERC, Compute Canada, and Calcul Qu\'ebec
for providing computational resources.
\end{frame}
\begin{frame}
\begin{center}
\bibliography{strings,strings-short,ml,aigaion-shorter}
\Huge
Questions?
\end{center}
\end{frame}
\end{document}