forked from sdrangan/introml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupplementary_neural.tex
1437 lines (1288 loc) · 50 KB
/
supplementary_neural.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
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
\documentclass[11pt]{article}
\usepackage{fullpage}
\usepackage{amsmath, amssymb, bm, cite, epsfig, psfrag}
\usepackage{graphicx}
\usepackage{float}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{listings}
\usepackage{cite}
\usepackage{hyperref}
\usepackage{tikz}
\usepackage{enumerate}
\usepackage{listings}
\usepackage{mathtools}
\usepackage{bbm}
\usepackage{array}
\lstloadlanguages{Python}
\usetikzlibrary{shapes,arrows}
%\usetikzlibrary{dsp,chains}
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{9} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{9} % for normal
% Defining colors
\usepackage{color}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
%\restylefloat{figure}
%\theoremstyle{plain} \newtheorem{theorem}{Theorem}
%\theoremstyle{definition} \newtheorem{definition}{Definition}
\def\del{\partial}
\def\ds{\displaystyle}
\def\ts{\textstyle}
\def\beq{\begin{equation}}
\def\eeq{\end{equation}}
\def\beqa{\begin{eqnarray}}
\def\eeqa{\end{eqnarray}}
\def\beqan{\begin{eqnarray*}}
\def\eeqan{\end{eqnarray*}}
\def\nn{\nonumber}
\def\binomial{\mathop{\mathrm{binomial}}}
\def\half{{\ts\frac{1}{2}}}
\def\Half{{\frac{1}{2}}}
\def\N{{\mathbb{N}}}
\def\Z{{\mathbb{Z}}}
\def\Q{{\mathbb{Q}}}
\def\R{{\mathbb{R}}}
\def\C{{\mathbb{C}}}
\def\argmin{\mathop{\mathrm{arg\,min}}}
\def\argmax{\mathop{\mathrm{arg\,max}}}
%\def\span{\mathop{\mathrm{span}}}
\def\diag{\mathop{\mathrm{diag}}}
\def\x{\times}
\def\limn{\lim_{n \rightarrow \infty}}
\def\liminfn{\liminf_{n \rightarrow \infty}}
\def\limsupn{\limsup_{n \rightarrow \infty}}
\def\MID{\,|\,}
\def\MIDD{\,;\,}
\newcommand{\specialcell}[2][c]{%
\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}}
\newtheorem{proposition}{Proposition}
\newtheorem{definition}{Definition}
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}
\newtheorem{corollary}{Corollary}
\newtheorem{assumption}{Assumption}
\newtheorem{claim}{Claim}
\def\qed{\mbox{} \hfill $\Box$}
\setlength{\unitlength}{1mm}
\def\bhat{\widehat{b}}
\def\ehat{\widehat{e}}
\def\phat{\widehat{p}}
\def\qhat{\widehat{q}}
\def\rhat{\widehat{r}}
\def\shat{\widehat{s}}
\def\uhat{\widehat{u}}
\def\ubar{\overline{u}}
\def\vhat{\widehat{v}}
\def\xhat{\widehat{x}}
\def\xbar{\overline{x}}
\def\zhat{\widehat{z}}
\def\zbar{\overline{z}}
\def\la{\leftarrow}
\def\ra{\rightarrow}
\def\MSE{\mbox{\small \sffamily MSE}}
\def\SNR{\mbox{\small \sffamily SNR}}
\def\SINR{\mbox{\small \sffamily SINR}}
\def\arr{\rightarrow}
\def\Exp{\mathbb{E}}
\def\var{\mbox{var}}
\def\Tr{\mbox{Tr}}
\def\tm1{t\! - \! 1}
\def\tp1{t\! + \! 1}
\def\Tm1{T\! - \! 1}
\def\Tp1{T\! + \! 1}
\def\Xset{{\cal X}}
\newcommand{\one}{\mathbf{1}}
\newcommand{\abf}{\mathbf{a}}
\newcommand{\bbf}{\mathbf{b}}
\newcommand{\dbf}{\mathbf{d}}
\newcommand{\ebf}{\mathbf{e}}
\newcommand{\gbf}{\mathbf{g}}
\newcommand{\hbf}{\mathbf{h}}
\newcommand{\pbf}{\mathbf{p}}
\newcommand{\pbfhat}{\widehat{\mathbf{p}}}
\newcommand{\qbf}{\mathbf{q}}
\newcommand{\qbfhat}{\widehat{\mathbf{q}}}
\newcommand{\rbf}{\mathbf{r}}
\newcommand{\rbfhat}{\widehat{\mathbf{r}}}
\newcommand{\sbf}{\mathbf{s}}
\newcommand{\sbfhat}{\widehat{\mathbf{s}}}
\newcommand{\ubf}{\mathbf{u}}
\newcommand{\ubfhat}{\widehat{\mathbf{u}}}
\newcommand{\utildebf}{\tilde{\mathbf{u}}}
\newcommand{\vbf}{\mathbf{v}}
\newcommand{\vbfhat}{\widehat{\mathbf{v}}}
\newcommand{\wbf}{\mathbf{w}}
\newcommand{\wbfhat}{\widehat{\mathbf{w}}}
\newcommand{\xbf}{\mathbf{x}}
\newcommand{\xbfhat}{\widehat{\mathbf{x}}}
\newcommand{\xbfbar}{\overline{\mathbf{x}}}
\newcommand{\ybf}{\mathbf{y}}
\newcommand{\zbf}{\mathbf{z}}
\newcommand{\zbfbar}{\overline{\mathbf{z}}}
\newcommand{\zbfhat}{\widehat{\mathbf{z}}}
\newcommand{\Ahat}{\widehat{A}}
\newcommand{\Abf}{\mathbf{A}}
\newcommand{\Bbf}{\mathbf{B}}
\newcommand{\Cbf}{\mathbf{C}}
\newcommand{\Bbfhat}{\widehat{\mathbf{B}}}
\newcommand{\Dbf}{\mathbf{D}}
\newcommand{\Gbf}{\mathbf{G}}
\newcommand{\Hbf}{\mathbf{H}}
\newcommand{\Ibf}{\mathbf{I}}
\newcommand{\Kbf}{\mathbf{K}}
\newcommand{\Pbf}{\mathbf{P}}
\newcommand{\Phat}{\widehat{P}}
\newcommand{\Qbf}{\mathbf{Q}}
\newcommand{\Rbf}{\mathbf{R}}
\newcommand{\Rhat}{\widehat{R}}
\newcommand{\Sbf}{\mathbf{S}}
\newcommand{\Ubf}{\mathbf{U}}
\newcommand{\Vbf}{\mathbf{V}}
\newcommand{\Wbf}{\mathbf{W}}
\newcommand{\Xhat}{\widehat{X}}
\newcommand{\Xbf}{\mathbf{X}}
\newcommand{\Ybf}{\mathbf{Y}}
\newcommand{\Zbf}{\mathbf{Z}}
\newcommand{\Zhat}{\widehat{Z}}
\newcommand{\Zbfhat}{\widehat{\mathbf{Z}}}
\def\alphabf{{\boldsymbol \alpha}}
\def\betabf{{\boldsymbol \beta}}
\def\betabfhat{{\widehat{\bm{\beta}}}}
\def\epsilonbf{{\boldsymbol \epsilon}}
\def\mubf{{\boldsymbol \mu}}
\def\lambdabf{{\boldsymbol \lambda}}
\def\etabf{{\boldsymbol \eta}}
\def\xibf{{\boldsymbol \xi}}
\def\taubf{{\boldsymbol \tau}}
\def\sigmahat{{\widehat{\sigma}}}
\def\thetabf{{\bm{\theta}}}
\def\thetabfhat{{\widehat{\bm{\theta}}}}
\def\thetahat{{\widehat{\theta}}}
\def\mubar{\overline{\mu}}
\def\muavg{\mu}
\def\sigbf{\bm{\sigma}}
\def\etal{\emph{et al.}}
\def\Ggothic{\mathfrak{G}}
\def\Pset{{\mathcal P}}
\newcommand{\bigCond}[2]{\bigl({#1} \!\bigm\vert\! {#2} \bigr)}
\newcommand{\BigCond}[2]{\Bigl({#1} \!\Bigm\vert\! {#2} \Bigr)}
\newcommand{\tran}{^{\text{\sf T}}}
\newcommand{\herm}{^{\text{\sf H}}}
\newcommand{\bkt}[1]{{\langle #1 \rangle}}
\def\Norm{{\mathcal N}}
\newcommand{\vmult}{.}
\newcommand{\vdiv}{./}
\newcommand{\indic}[1]{\mathbbm{1}_{ \{ {#1} \} }}
\def\hid{\textsc{\tiny H}}
\def\out{\textsc{\tiny O}}
\def\zhid{z^\hid}
\def\uhid{u^\hid}
\def\zout{z^\out}
\def\uout{u^\out}
% Python style for highlighting
\newcommand\pythonstyle{\lstset{
language=Python,
backgroundcolor=\color{backcolour},
commentstyle=\color{deepgreen},
basicstyle=\ttm,
otherkeywords={self}, % Add keywords here
keywordstyle=\ttb\color{deepblue},
emph={MyClass,__init__}, % Custom highlighting
emphstyle=\ttb\color{deepred}, % Custom highlighting style
stringstyle=\color{deepgreen},
%frame=tb, % Any extra options here
showstringspaces=false %
}}
% Python environment
\lstnewenvironment{python}[1][]
{
\pythonstyle
\lstset{#1}
}
{}
% Python for external files
\newcommand\pythonexternal[2][]{{
\pythonstyle
\lstinputlisting[#1]{#2}}}
% Python for inline
\newcommand\pycode[1]{{\pythonstyle\lstinline!#1!}}
\begin{document}
\title{Introduction to Machine Learning\\
Neural Network Notes with Solved Problems}
\author{Prof. Sundeep Rangan}
\date{}
\maketitle
\section{Description of Neural Networks}
\begin{itemize}
\item Neural networks refer to a general class of models for making predictions on
data. The key feature of neural networks is that the input data is processed in
multiple stages, called \emph{layers}. The parameters in each layer are \emph{trained}
or \emph{learned} from examples. This multi-layer processing
with trainable parameters in each layer is loosely inspired by biological neural systems.
\item To describe the neural network, let $\xbf$ denote the input vector and
let $y$ denote the target variable that we wish to predict from $\xbf$.
\item We consider networks for both \textbf{regression} or \textbf{classification} problems:
\begin{itemize}
\item For regression problems, $y$ is a scalar or vector and is typically continuous-valued.
In this case, the neural network produces a prediction $\hat{y}$ of $y$ of the same dimension as $y$.
\item For classification problems, the target variable $y \in \{1,\ldots,K\}$ and the neural network
typically provides a soft prediction of the class label.
Specifically, the networks makes a prediction of the probability $P(y=k|\xbf)$ for each class label $k$
given the input $\xbf$.
\end{itemize}
\item For both regression and classification problems,
we assume the input $\xbf$ is a vector of dimension $N_i$ so that
\[
\xbf = (x_1,\ldots,x_{N_i}).
\]
\item In this note, we look at neural networks with one hidden layer.
In such a network, the neural network mapping is performed in two steps -- each step is called a \emph{layer}:
\begin{itemize}
\item \emph{Hidden layer} produces outputs $\zbf^\hid$ and $\ubf^\hid$ of dimension $N_h$.
\item \emph{Output layer} produces outputs $\zbf^\out$ and $\ubf^\out$ of dimension $N_o$.
\end{itemize}
The dimensions $N_h$ and $N_o$ will be discussed below.
\item The equations for the two layers with a single input $\xbf$ are:
\begin{subequations} \label{eq:nn}
\begin{align}
&\mbox{Hidden layer:} \quad
z_{j}^\hid = \sum_{k=1}^{N_i} W^\hid_{jk}x_{k} + b^\hid_j, \quad
u_{j}^\hid = g_{\rm act}(z_{j}^\hid), \quad j=1,\ldots,N_h
\label{eq:nnhidden} \\
&\mbox{Output layer:} \quad
z_{j}^\out = \sum_{k=1}^{N_h} W^\out_{jk}u^\hid_{k} + b^\out_j,
\quad
u^\out = g_{\rm out}(\zbf^\out).
\quad j=1,\ldots,N_o.
\label{eq:nnout}
\end{align}
\end{subequations}
\item In the hidden layer, the function $g_{\rm act}(z)$ is called the \emph{activation function}.
There are three common choices:
\begin{itemize}
\item Hard threshold:
\beq \label{eq:gact_ht}
g_{\rm act}(z) = \begin{cases}
1, & \mbox{if } z \geq 0 \\
0, & \mbox{if } z < 0.
\end{cases}
\eeq
\item Sigmoid: $g_{\rm act}(z) = 1/(1+e^{-z})$.
\item Rectified linear unit (ReLU): $g(z) = \max\{0,z\}$.
\end{itemize}
\item The output map $g_{\rm out}(z)$ and dimension $N_o$
depends on the type of prediction problem we are using the neural
network for:
\begin{itemize}
\item Binary classification: In this case, the response
is $y=\{0,1\}$. To predict the response, we typically take $N_o = 1$ and
the output $u^\out$ is the class probability:
\beq \label{eq:gout_binary_soft}
P(y=1|\xbf) = u^\out = g_{\rm out}(z^\out) = \frac{1}{1 + e^{-\zout}}.
\eeq
The variable $\zout$ is called the \emph{logit} and the output mapping \eqref{eq:gout_binary_soft}
is a sigmoid.
We can also use $\zout$ to make a hard decision by selecting the most likely class:
\beq \label{eq:gout_thresh}
\hat{y} = \begin{cases}
1 & z^\out \geq 0 \\
0 & z^\out < 0.
\end{cases}
\eeq
\item $K$-class classification: In this case,
the target is a class label $y=1,\ldots,K$. We typically take $N_o = K$ and use the
soft-max function for the class probability:
\beq \label{eq:gout_mc_soft}
P(y=k|\xbf) = u^\out_k = g_{\rm out,k}(z^\out) = \frac{e^{z^\out_k}}{\sum_{\ell=1}^K e^{z^\out_\ell}}.
\eeq
Again, we can make a hard decision on the class label by selecting the highest logit score:
\beq \label{eq:gout_argmax}
\hat{y} = \argmax_{k=1,\ldots,K} z^\out_k.
\eeq
\item Regression: In this case, one is trying to predict $\ybf \in \R^d$,
where $d$ is the number of variables to predict and each component $y_j$
is typically continuous-valued. For this problem, we take $N_o=d$ and $\ubf^\out$ is the prediction of $\ybf$,
\beq \label{eq:gout_id}
\hat{\ybf} = \ubf^\out = g_{\rm out}(\zbf^\out) = \zbf^\out.
\eeq
In \eqref{eq:gout_id}, we will either say there is no activation or an \emph{identity} activation.
\end{itemize}
\item The number, $N_h$ of hidden variables (also called the number of \emph{hidden units})
represents the model complexity. Higher values of $N_h$ can express more complex mappings,
but will need more data to train.
\item When $N_o=1$ (single output unit), we drop the subscript $j$ in
\eqref{eq:nnout} and write
\beq
\mbox{Output layer:} \quad
z^\out = \sum_{k=1}^{N_h} W^\hid_{k}u^\hid_k + b^\out, \quad
\hat{y} = g_{\rm out}(z^\out).
\label{eq:nnout_scalar}
\eeq
\item Matrix form of the neural network with single sample input $\xbf$:
\begin{align*}
\zbf^\hid &= \Wbf^\hid\xbf + \bbf^\hid, \quad
\ubf^\hid = g_{\rm act}(\zbf^\hid), \\
\zbf^\out &= \Wbf^\out\ubf^\hid + \bbf^\out, \quad
\ubf^\out = g_{\rm out}(\zbf^\out).
\end{align*}
\item Batch form: Often we have a batch of input-output samples,
$(\xbf_i,y_i)$, $i=1,\ldots,N$ (e.g.\ a mini-batch in training or test).
In this case, we need to add an index $i$
to each sample and rewrite \eqref{eq:nn} as:
\begin{subequations} \label{eq:nnmulti}
\begin{align}
&\mbox{Hidden layer:} \quad
z_{ij}^\hid = \sum_{k=1}^{N_i} W^\hid_{jk}x_{ik} + b^\hid_j, \quad
u_{ij}^\hid = g_{\rm act}(z_{ij}^\hid), \quad j=1,\ldots,N_h
\label{eq:nnhidden_multi} \\
&\mbox{Output layer:} \quad
z_{ij}^\out = \sum_{k=1}^{N_h} W^\out_{jk}u^\hid_{ik} + b^\out_j,
\quad
\ubf_j^\out = g_{\rm out}(\zbf_i^\out),
\quad j=1,\ldots,N_o.
\label{eq:nnout_multi}
\end{align}
\end{subequations}
\item Matrix form of batch processing in neural networks:
Define the matrices,
\[
\Xbf = \left[ \begin{array}{ccc}
\xbf_1\tran \\
\vdots \\
\xbf_N\tran
\end{array} \right]
= \left[ \begin{array}{ccc}
x_{11} & \cdots & x_{1,N_i} \\
\vdots & \vdots & \vdots \\
x_{N1} & \cdots & x_{N,N_i}
\end{array}
\right],
\quad
\Zbf^\hid
= \left[ \begin{array}{c}
(\zbf_1^\hid)\tran \\
\vdots \\
(\zbf_N^\hid)\tran
\end{array} \right]
= \left[ \begin{array}{ccc}
z^\hid_{11} & \cdots & z^\hid_{1,N_h} \\
\vdots & \vdots & \vdots \\
z^\hid_{N1} & \cdots & z^\hid_{N,N_h}
\end{array}
\right],
\]
Similarly define
\[
\Ubf^\hid
= \left[ \begin{array}{c}
(\ubf_1^\hid)\tran \\
\vdots \\
(\ubf_N^\hid)\tran
\end{array} \right]
=
\left[ \begin{array}{ccc}
u^\hid_{11} & \cdots & u^\hid_{1,N_h} \\
\vdots & \vdots & \vdots \\
u^\hid_{N1} & \cdots & u^\hid_{N,N_h}
\end{array}
\right].
\quad
\Ubf^\out
= \left[ \begin{array}{c}
(\ubf_1^\out)\tran \\
\vdots \\
(\ubf_N^\out)\tran
\end{array} \right]
=
\left[ \begin{array}{ccc}
u^\out_{11} & \cdots & u^\hid_{1,N_o} \\
\vdots & \vdots & \vdots \\
u^\out_{N1} & \cdots & u^\hid_{N,N_o}
\end{array}
\right].
\]
Hence each row contains all the components for the $i$-th sample.
Then, we can write
\begin{subequations} \label{eq:nn_ms_matrix}
\begin{align}
\Zbf^\hid &= \Xbf\left[ \Wbf^\hid \right]\tran
+ \Bbf^\hid,
\quad \Ubf^\hid = g_{\rm act}(\Zbf^\hid)
\label{eq:nn_ms_mat_hid} \\
\Zbf^\out &= \Ubf^\hid \left[ \Wbf^\out \right]\tran
+ \Bbf^\out
\quad
\Ubf^\out = g_{\rm out}\left( \Zbf^\out \right),
\label{eq:nn_ms_mat_out}
\end{align}
\end{subequations}
where $\Bbf^\hid$ and $\Bbf^\out$
repeat the bias vectors on the $N$ rows
\beq \label{eq:brep}
\Bbf^\hid
= \left[ \begin{array}{c}
(\bbf^\hid)\tran \\
\vdots \\
(\bbf^\hid)\tran
\end{array} \right] \quad
\Bbf^\out
= \left[ \begin{array}{c}
(\bbf^\out)\tran \\
\vdots \\
(\bbf^\out)\tran
\end{array} \right] \quad
\eeq
\end{itemize}
\paragraph*{Problems}
\begin{enumerate}
\item Consider the neural network \eqref{eq:nn} with a scalar input $x$ and parameters
\[
W^\hid = \left[ \begin{array}{c} 1 \\ 1 \end{array} \right], \quad
b^\hid = \left[ \begin{array}{c} -1 \\ -3 \end{array} \right] \quad
W^\out = [-1, 2], \quad b^\out = 0.5,
\]
using a hard threshold activation function \eqref{eq:gact_ht} and
threshold output function \eqref{eq:gout_thresh}.
\begin{enumerate}[(a)]
\item What is $N_h$, the number of hidden units? What is $N_o$, the number of output units?
\item Write $\zbf^\hid$ in terms of $x$. Draw the functions for each component $z_j^\hid$.
\item Write $\ubf^\hid$ in terms of $x$. Draw the functions for each component $u_j^\hid$.
\item Write $z^\out$ in terms of $x$.
\item What is $\hat{y}$ in terms of $x$?
\end{enumerate}
\item Consider the data set for four points with
scalar features $x_i$ and binary class labels $y_i=0,1$.
\begin{center}
\begin{tabular}{|c|c|c|c|c|c|} \hline
$x_i$ & 0 & 1 & 3 & 5 \\ \hline
$y_i$ & 0 & 0 & 1 & 0 \\ \hline
\end{tabular}
\end{center}
\begin{enumerate}[(a)]
\item Find a neural network with $N_h=2$ units, $N_o=1$ output units
such that $\hat{y}_i=y_i$ on all four data points.
Use a hard threshold activation function \eqref{eq:gact_ht} and
threshold output function \eqref{eq:gout_thresh}.
State the weights and biases used in each layer.
\item Compute the values of $\hat{y}_i$ and all
the intermediate variables $\zbf_i^\hid$,
$\ubf_i^\hid$ and $z^\out_i$ for each sample $x=x_i$.
\item Now suppose we are given a new sample, $x=3.5$. What does the network
predict as $\hat{y}$?
\end{enumerate}
\item \emph{Two-dimensional example:} Consider a neural network
on a 2-dimensional input $\xbf=(x_1,x_2)$ with weights and biases:
\[
W^\hid = \left[ \begin{array}{cc} 1 & 0 \\ 0 & 1 \\ 1 & 1 \end{array} \right], \quad
b^\hid = \left[ \begin{array}{c} 0 \\ 0 \\ -1 \end{array} \right] \quad
W^\out = [1, 1, -1], \quad b^\out = -1.5.
\]
Assume the network uses hard a
threshold activation function \eqref{eq:gact_ht} and
threshold output function \eqref{eq:gout_thresh}.
\begin{enumerate}[(a)]
\item Write the components of $\zbf^\hid$ and $\ubf^\hid$ as a function
of $(x_1,x_2)$. For each component $j$, indicate where in the $(x_1,x_2)$
plane $u^\hid_j=1$.
\item Write $z^\out$ as a function of $(x_1,x_2)$. In what region is
$\hat{y}=1$?
\end{enumerate}
\item \emph{Architecture choices for different problems:}
For each problem, state possible selections for the dimensions
$N_i$, $N_h$, $N_o$ and the functions $g_{\rm act}(\cdot)$ and
$g_{\rm out}(\cdot)$. Indicate which parameters are free to choose.
\begin{enumerate}[(a)]
\item One wants a neural network to take as an input a $20 \x 20$
gray scale image and determine which letter (`a' to `z')
the image is of.
\item One extracts 120 features of a sample of a
speech recording (like the MFCCs).
Based on the audio samples, the network is to determine if the speech
is male or female.
\item One wants a neural network to predict the
stock price based on the average stock price of the last five days.
\end{enumerate}
\item \emph{Implementation in python:}
Write python code for implementing the following steps for a batch of samples:
\begin{enumerate}[(a)]
\item The hidden layer step \eqref{eq:nn_ms_mat_hid}.
\item The output layer step \eqref{eq:nn_ms_mat_out}
for binary classification with a sigmoid output \eqref{eq:gout_binary_soft}.
\item The output layer step \eqref{eq:nn_ms_mat_out}
for $K$-class classification with a softmax output \eqref{eq:gout_mc_soft}.
\end{enumerate}
For all examples, try to avoid for-loops.
\end{enumerate}
\paragraph*{Solutions}
\begin{enumerate}
\item
\begin{enumerate}[(a)]
\item Since $\Wbf^\hid$ has 2 ouptuts, $N_h=2$.
Since $W^\out$ has 1 output, $N_o=1$.
\item The components of the linear variables $\zbf^\hid$ in the hidden layer are:
\[
\zbf^\hid = \Wbf^\hid\xbf + \bbf^\hid =
\left[ \begin{array}{c} 1 \\ 1 \end{array} \right]x +
\left[ \begin{array}{c} -1 \\ -3 \end{array} \right] =
\left[ \begin{array}{c} x-1 \\ x-3 \end{array} \right].
\]
\item The activation outputs in the hidden layer are
\[
\ubf^\hid = g_{\rm act}(\zbf^\hid) =
\left[ \begin{array}{c} g_{\rm act}(x-1) \\ g_{\rm act}(x-3) \end{array} \right]
=
\left[ \begin{array}{c} \indic{x \geq 1} \\ \indic{x \geq 3} \end{array} \right].
\]
\item The linear variable in the output layer is
\begin{align*}
z^\out &= W^\out \ubf^\hid + b^\out =
[-1, 2]\left[ \begin{array}{c} \indic{x \geq 1} \\ \indic{x \geq 3} \end{array} \right]
+ 0.5 \\
&= -\indic{x \geq 1} +2\indic{x \geq 3} + 0.5
= \begin{cases}
0.5 & \mbox{when } x < 1 \\
-0.5 & \mbox{when } x \in [1,3) \\
1.5 & \mbox{when } x \geq 3
\end{cases}
\end{align*}
\item The predicted output $\hat{y}$ is
\[
\hat{y} = \indic{z^\out \geq 0} =
\begin{cases}
0 & \mbox{if } x \in [1,3) \\
1 & \mbox{else}
\end{cases}
\]
\end{enumerate}
\item \label{prob:nncreate}
\begin{enumerate}[(a)]
\item
We can use a network similar in structure to the previous problem.
In the hidden layer, we want to find features that can distinguish between
$x=3$ which belongs to class $y=1$, and $x=\{0,1,5\}$ which belong to class $y=0$.
There are lots of choices, but we will extract two features: whether $x \geq 2$
and $x \geq 4$. We can do this with the linear transforms:
\[
\Wbf^\hid = \left[ \begin{array}{c} 1 \\ 1 \end{array} \right], \quad
b^\hid = \left[ \begin{array}{c} -2 \\ -4 \end{array} \right],
\]
so that
\[
\zbf^\hid = \Wbf^\hid\xbf + \bbf^\hid =
\left[ \begin{array}{c} 1 \\ 1 \end{array} \right]x +
\left[ \begin{array}{c} -2 \\ -4 \end{array} \right] =
\left[ \begin{array}{c} x-2 \\ x-4 \end{array} \right].
\]
Then, the activation outputs in the hidden layer are
\[
\ubf^\hid = g_{\rm act}(\zbf^\hid) =
\left[ \begin{array}{c} g_{\rm act}(x-2) \\ g_{\rm act}(x-4) \end{array} \right]
=
\left[ \begin{array}{c} \indic{x \geq 2} \\ \indic{x \geq 4} \end{array} \right].
\]
Now we need to combine these functions in a way that they are negative
for samples $x_i$ when $y_i=0$ and positive on sample $x_i$ when $y_i=1$.
Similar to the previous problem, take
\[
W^\out = [1, -2], \quad b^\out = -0.5.
\]
Then,
\begin{align*}
z^\out &= W^\out \ubf^\hid + b^\out =
[1, -2]\left[ \begin{array}{c} \indic{x \geq 2} \\ \indic{x \geq 4} \end{array} \right]
- 0.5 \\
&= \indic{x \geq 2} -2\indic{x \geq 4} - 0.5
= \begin{cases}
-0.5 & \mbox{when } x < 2 \\
0.5 & \mbox{when } x \in [2,4) \\
-1.5 & \mbox{when } x \geq 4.
\end{cases}
\end{align*}
The predicted output $\hat{y}$ is
\[
\hat{y} = \indic{z^\out \geq 0} =
\begin{cases}
1 & \mbox{if } x \in [2,4) \\
0 & \mbox{else}
\end{cases}
\]
This matches the training data.
\item Table~\ref{tbl:nncreate} computes the values for all intermediate variables
for the four training
data points. We can see that $\hat{y}_i=y_i$ for all four points.
\begin{table}
\begin{center}
\begin{tabular}{|l|c|c|c|c|c|} \hline
& \multicolumn{4}{|c|}{Training} & Test \\ \hline
$x_i$ & 0 & 1 & 3 & 5 & 3.5 \\ \hline
$z^\hid_{i1}=x-2$ & -2 & -1 & 1 & 3 & 1.5\\ \hline
$z^\hid_{i2}=x-2$ & -4 & -3 & -1 & 1 & 0.5 \\ \hline
$u^\hid_{i1}=g_{\rm act}(z^\hid_{i1})$ & 0 & 0 & 1 & 1 & 1 \\ \hline
$u^\hid_{i1}=g_{\rm act}(z^\hid_{i2})$ & 0 & 0 & 0 & 1 & 0 \\ \hline
$z^\out_{i}=u^\hid_{i1}-2u^\hid_{i1}-0.5$ & -0.5 & -0.5 & 0.5 & -1.5 & 0.5\\ \hline
$\hat{y}_i = g_{\rm out}(z^\out_i)$ & 0 & 0 & 1 & 0 & 1\\ \hline
$y_i$ & 0 & 0 & 1 & 0 & \\ \hline
\end{tabular}
\end{center}
\caption{Computations for the neural network in Problem \ref{prob:nncreate}}
\label{tbl:nncreate}
\end{table}
\item The value for $x=3.5$ is shown in the final column of
Table~\ref{tbl:nncreate}. For this value of $x=3.5$, we get $\hat{y}=1$.
\end{enumerate}
\item \label{prob:nntwo}
\begin{enumerate}[(a)]
\item
The linear functions in the hidden layer are:
\[
\zbf^\hid = \Wbf^\hid\xbf + \bbf^\hid =
\left[ \begin{array}{cc} 1 & 0 \\ 0 & 1 \\ 1 & 1 \end{array} \right]
\left[ \begin{array}{c} x_1 \\ x_2 \end{array} \right] +
\left[ \begin{array}{c} 0 \\ 0 \\ -1 \end{array} \right]
= \left[ \begin{array}{c} x_1 \\ x_2 \\ x_1 + x_2 -1 \end{array} \right]
\]
Hence, the activation functions are
\[
\ubf^\hid = g_{\rm act}(\zbf^\hid) =
\left[ \begin{array}{c} g_{\rm act}(x_1) \\ g_{\rm act}(x_2)
\\ g_{\rm act}(x_1+x_2-1) \end{array} \right]
=
\left[ \begin{array}{c} \indic{x_1 \geq 0} \\ \indic{x_2 \geq 0}
\\ \indic{x_1+x_2 \geq 1} \end{array} \right].
\]
\item The output $z^\out$ is
\begin{align*}
z^\out &= W^\out \ubf^\hid + b^\out =
[1, 1, -1]
\left[ \begin{array}{c} \indic{x_1 \geq 0} \\ \indic{x_2 \geq 0}
\\ \indic{x_1+x_2 \geq 1} \end{array} \right]
- 1.5 \\
&= \indic{x_1 \geq 0} + \indic{x_2 \geq 0} -\indic{x_1 + x_2 \geq 1} - 1.5.
\end{align*}
It is best to draw this. If you do that (I will add the figures later),
you will see that in the triangle defined by the lines
\beq \label{eq:ztriangle}
x_1 \geq 0, \quad x_2 \geq 0, \quad x_1 + x_2 < 1,
\eeq
we have
\[
z^o = (1) + (1) -(0)-1.5 = 0.5.
\]
Outside this region, $z^\out < 0$. Hence, $z^O \geq 0$ only in the region
\eqref{eq:ztriangle}. Therefore,
\[
\hat{y} = \indic{z^\out \geq 0} =
\begin{cases}
1 & \mbox{if } x_1 \geq 0, x_2 \geq 0, x_1 + x_2 < 1 \\
0 & \mbox{else}
\end{cases}
\]
\end{enumerate}
\item
\begin{enumerate}[(a)]
\item Assuming we represent the image pixels as a vector,
the input size is $N_i=20\x20=400$. Since this is a multi-class classification problem
with 26 classes, one would generally take an output size of $N_o=26$ and use a soft-max for
the output map $g_{\rm out}(\cdot)$. The number of hidden units $N_h$
and the activation $g_{\rm act}(\cdot)$ are free to choose
\item The input size is $N_i=120$. Since this is a binary classification problem,
we would generally take $N_o=1$ and use a sigmoid output for
$g_{\rm out}(\cdot)$. The number of hidden units $N_h$
and the activation $g_{\rm act}(\cdot)$ are free to choose.
\item We would take $N_i=5$ for the stock prices for the last five days.
Since this is a regression problem predicting a scalar value, we would take
$N_o=1$ and use an identity activation, $g_{\rm out}(z)=z$ (i.e.\ there is
no activation).
Again, the number of hidden units $N_h$
and the activation $g_{\rm act}(\cdot)$ are free to choose.
\end{enumerate}
\item
\begin{enumerate}[(a)]
\item
The operations in the hidden layer in \eqref{eq:nn_ms_matrix} can
be implemented as:
\begin{python}
Zh = X.dot(Wh.T) + bh[None,:]
Uh = 1/(1+np.exp(-Zh))
\end{python}
where \pycode{Wh} and \pycode{bh} are the weight matrix and bias vector.
Note that we have used python broadcasting on the bias vector \pycode{bh}
\item For a binary classification problem, $N_o=1$ and
we can represent $\Zbf^\out$ as an $N$-dimensional vector, \pycode{Zo}.
Also, we can represent the weight $\Wbf^\hid$ as an $N_h$-dimensional vector
\pycode{Wh} and the bias $b^\hid$ as a scalar \pycode{bh}.
In this case,
\begin{python}
Zo = Uh.dot(Wo) + bo
Uo = 1/(1+np.exp(-Zo))
\end{python}
\item For $K$-class classification, we perform
\begin{python}
Zo = Uh.dot(Wo.T) + bo[None,:]
expZo = np.exp(Zo)
Uo = expZo / np.sum(expZo,axis=1)[:,None]
\end{python}
Note the method for normalizing the outputs in a softmax classifier.
\end{enumerate}
\end{enumerate}
\section{Multivariate Gradients and Chain Rule}
\begin{itemize}
\item The parameter learning for a neural network is based on gradient-descent.
Before looking at the learning rules, we need to review some concepts from
multivariable calculus.
\item \emph{Tensors:} A tensor is a multi-dimensional array,
\[
X_{i_1,i_2, \ldots, i_d}, \quad 0 \leq i_1 < N_1,\ldots,
0 \leq i_d < N_d.
\]
\begin{itemize}
\item The number of dimensions $d$ is called the \emph{order}. It is also
sometimes called the \emph{rank} (esp.\ in Tensorflow documentation), but this
can be confusing since rank has other meanings in linear algebra.
\item The elements are accessed by a \emph{multi-index}, $(i_1,\ldots,i_d)$.
\item The \emph{shape} or \emph{size} of the tensor is $(N_1,\ldots,N_d)$.
\end{itemize}
\item It will be sometimes more convenient to index the elements of a tensor with brackets:
$X[i_1,\ldots,i_d]$ instead of $X_{i_1,\ldots,i_d}$.
\item \emph{Examples:}
\begin{itemize}
\item A vector is an order 1 tensor.
\item A matrix is an order 2 tensor.
\item A color image can be represented as an order 3 tensor (height, width, color).
For example, a $512 \times 512$ with three color channels (RGB) would have shape
$(512,512,3)$.
\item A batch of color images can be represented as an order 4 tensor
(batch size, height, width, color). For example, a mini-batch of 32 color images
as above would have shape $(32,512,512,3)$.
\end{itemize}
\item \emph{Gradient tensors:} Suppose that $\Ybf = f(\Xbf)$ where:
\begin{itemize}
\item The input $\Xbf$ is a tensor of size $(N_1,\ldots,N_r)$, and
\item The output $\Ybf$ is a tensor of size $(M_1,\ldots,M_s)$.
\end{itemize}
The \emph{gradient} $\partial \Ybf / \partial \Xbf$
is a tensor of shape $(M_1,\ldots,M_s,N_1,\ldots,N_r)$
with components,
\[
\left[ \frac{\partial \Ybf}{\partial \Xbf} \right]_{i_1,\ldots, i_s,j_1,\ldots,j_r} =
\frac{\partial Y_{i_1,\ldots,i_r}}{\partial X_{j_1,\ldots,j_s}}.
\]
\item \emph{Jacobian}: Consider the special case that $\ybf = f(\xbf)$ where
$\xbf$ is a vector of dimension $N$ and $\ybf$ is a vector of dimension $M$.
The gradient tensor $\partial \ybf/\partial \xbf$ is a matrix of shape $M \times N$.
This is called the \emph{Jacobian}.
\item \emph{Gradients for scalar-valued functions:} Suppose $J = f(\Xbf)$,
where the output $J$ is a scalar and the input $\Xbf$ has shape $(N_1,\ldots,N_r)$.
In this case, the gradient tensor $\partial J/\partial \Xbf$ will have the shape
$(1,N_1,\ldots,N_r)$. It is sometimes convenient to regard $\partial J/\partial \Xbf$ has a
$(N_1,\ldots,N_r)$ tensor and drop the index over the single output components. That is,
$\partial J/\partial \Xbf$ has the same shape as $\Xbf$ with components,
\[
\frac{\partial J}{\partial X_{1_1,\ldots,1_r}}.
\]
\item \emph{Tensor chain rule:}
Consider the sequence $\Xbf \mapsto \Ybf \mapsto \Zbf$ where
\begin{itemize}
\item The input $\Xbf$ is a tensor of size $(K_1,\ldots,K_t)$;
\item The intermediate variable $\Ybf=g(\Xbf)$ is a tensor of size $(N_1,\ldots,N_r)$;
\item The output $\Zbf=h(\Ybf)$ is a tensor of size $(M_1,\ldots,M_s)$.
\end{itemize}
Then, the components of the gradient tensor $\partial \Zbf/\partial \Xbf$ are given by:
\[
\frac{\partial Z_{i_1,\ldots,i_r}}{\partial X_{k_1,\ldots,k_t}}
= \sum_{j_1=0}^{N_1-1}\cdots \sum_{j_r=0}^{N_r-1} \frac{\partial Z_{i_1,\ldots,i_r}}{\partial Y_{j_1,\ldots,j_s}}
\frac{\partial Y_{j_1,\ldots,j_s}}{\partial X_{k_1,\ldots,k_t}}.
\]
That is, one sums over all the intermediate variables. We will often use the shorthand,
\[
\frac{\partial \Zbf}{\partial \Xbf} = \frac{\partial \Zbf}{\partial \Ybf}\cdot \frac{\partial \Ybf}{\partial \Xbf},
\]
where $\cdot$ is called the \emph{tensor dot-product}.
\item \emph{Gradients of linear functions:} In neural networks, we often
have linear layers where
\[
\zbf = \Wbf \xbf + \bbf,
\]
meaning that $z_i = \sum_j W_{ij}x_j + b_i$.
Therefore, the components of the gradients of $\partial \zbf/\partial \Wbf$ and $\partial \zbf/\partial \bbf$ and
$\partial \zbf/\partial \xbf$ are:
\beq \label{eq:gradlinvec}
\frac{\partial z_i}{\partial W_{ij}}=x_j, \quad
\frac{\partial z_i}{\partial b_i}=1, \quad
\frac{\partial z_i}{\partial x_j}=W_{ij}.
\eeq
This can often be combined with chain rule.
\end{itemize}
\paragraph*{Problems}
\begin{enumerate}
\item Compute the gradient $\partial f(\xbf)/\partial \xbf$ of the function,
\[
f(\xbf) = (x_1+x_2x_3,x_1^2+3x_2).
\]
\item Consider the logistic loss function,
\[
J = \sum_{i=1}^N\left[ \ln(1+e^{z_i}) - y_iz_i \right], \quad z_i = \sum_{j=1}^p x_{ij}w_j + b.
\]
\begin{enumerate}[(a)]
\item What is the gradient $\partial J/\partial \zbf$? That is, find the components $\partial J/\partial z_i$.
\item What are the gradient $\partial \zbf/\partial \wbf$ and $\partial \zbf/\partial b$?
\item Use the chain rule to compute the gradients $\partial J/\partial \wbf$ and $\partial J/\partial b$.
\end{enumerate}
\item Suppose that
\[
\ubf = f(\zbf) = (f(z_1),\ldots,f(z_N)), \quad f(z_i) = \frac{1}{1+e^{-z_i}},
\]
so that both $\ubf$ and $\zbf$ are $N$-dimensional vectors.
\begin{enumerate}[(a)]
\item What is the gradient $\partial \ubf/\partial \zbf$?
\item If $\zbf = \Wbf\xbf + \bbf$, what are the gradients $\partial \ubf/\partial \Wbf$ and
$\partial \ubf/\partial \bbf$?
\item In part (b), what is $\partial \ubf/\partial \xbf$?
\end{enumerate}
\item Let
\[
L = \|\ybf -\zbf\|^2, \quad \zbf = \Wbf\xbf+\bbf,
\]
\begin{enumerate}[(a)]
\item What is the gradient $\partial L/\partial \zbf$?
\item What are the gradients $\partial L/\partial \Wbf$ and $\partial L/\partial \bbf$?
\item What is the gradient $\partial L/\partial \xbf$?
\end{enumerate}
\end{enumerate}
\paragraph*{Solutions}
\begin{enumerate}
\item This function has two outputs and three inputs. Therefore, the gradient can be represented as
$2 \x 3$ matrix with the partial derivatives:
\[
\frac{\partial f(\xbf)}{\partial \xbf}
= \left[ \begin{array}{ccc}
1 & x_3 & x_2 \\
2x_1 & 3 & 0
\end{array}
\right]
\]
\item
\begin{enumerate}[(a)]
\item The components of the gradient $dJ/\partial \zbf$ are given by the partial derivatives,
\[
\frac{\partial J}{\partial z_i} = -y_i + \frac{e^{z_i}}{1+e^{z_i}}.
\]
\item The components of the gradient $\partial \zbf/\partial \wbf$ and $\partial \zbf/\partial b$ are
\[
\frac{\partial z_i}{\partial w_j} = x_{ij}, \quad \frac{\partial z_i}{\partial b} = 1.
\]
\item Using chain rule,
\begin{align*}
\frac{\partial J}{\partial w_j} &= \sum_i \frac{\partial J}{\partial z_i}\frac{\partial z_i}{\partial w_j} = \sum_i
\frac{\partial J}{\partial z_i}x_{ij}, \\
\frac{\partial J}{\partial b} &= \sum_i \frac{\partial J}{\partial z_i}\frac{\partial z_i}{\partial b} =
\sum_i \frac{\partial J}{\partial z_i},
\end{align*}
where $\partial J/\partial z_i$ is given in part (a).
\end{enumerate}
\item
\begin{enumerate}[(a)]
\item Since $u_i$ only depends on $z_i$, we have that
\[
\frac{\partial u_i}{\partial z_j} = 0 \mbox{ for } i \neq j.
\]
For $i=j$,
\[
\frac{\partial u_i}{\partial z_i} = f'(z_i) = \frac{e^{-z_i}}{(1+e^{-z_i})^2}.
\]
Therefore, the gradient is the diagonal matrix,
\[
\frac{\partial \ubf}{\partial \zbf} = \left[
\begin{array}{cccc}
f'(z_1) & 0 & \cdots & 0 \\
0 & f'(z_2) & \cdots & 0 \\
\vdots & \vdots & \ddots & \vdots \\
0 & 0 & \cdots & f'(z_N)
\end{array} \right] = \mathrm{diag}(f'(z_1),f'(z_2),\cdots,f'(z_N)).
\]