Skip to content

Commit c56d5b7

Browse files
author
Mu Li
committed
Uploaded by d2lbook
1 parent 704e12c commit c56d5b7

File tree

153 files changed

+64990
-210097
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

153 files changed

+64990
-210097
lines changed

chapter_appendix-mathematics-for-deep-learning/distributions.ipynb

+1,871-1,836
Large diffs are not rendered by default.

chapter_appendix-mathematics-for-deep-learning/eigendecomposition.ipynb

+144-130
Large diffs are not rendered by default.

chapter_appendix-mathematics-for-deep-learning/geometry-linear-algebraic-ops.ipynb

+30,336-63
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -1,60 +0,0 @@
1-
{
2-
"cells": [
3-
{
4-
"cell_type": "markdown",
5-
"metadata": {
6-
"origin_pos": 0
7-
},
8-
"source": [
9-
"# Appendix: Mathematics for Deep Learning\n",
10-
":label:`chap_appendix_math`\n",
11-
"\n",
12-
"**Brent Werness** (*Amazon*), **Rachel Hu** (*Amazon*), and authors of this book\n",
13-
"\n",
14-
"\n",
15-
"One of the wonderful parts of modern deep learning is the fact that much of it can be understood and used without a full understanding of the mathematics below it. This is a sign that the field is maturing. Just as most software developers no longer need to worry about the theory of computable functions, neither should deep learning practitioners need to worry about the theoretical foundations of maximum likelihood learning.\n",
16-
"\n",
17-
"But, we are not quite there yet.\n",
18-
"\n",
19-
"In practice, you will sometimes need to understand how architectural choices influence gradient flow, or the implicit assumptions you make by training with a certain loss function. You might need to know what in the world entropy measures, and how it can help you understand exactly what bits-per-character means in your model. These all require deeper mathematical understanding.\n",
20-
"\n",
21-
"This appendix aims to provide you the mathematical background you need to understand the core theory of modern deep learning, but it is not exhaustive. We will begin with examining linear algebra in greater depth. We develop a geometric understanding of all the common linear algebraic objects and operations that will enable us to visualize the effects of various transformations on our data. A key element is the development of the basics of eigen-decompositions.\n",
22-
"\n",
23-
"We next develop the theory of differential calculus to the point that we can fully understand why the gradient is the direction of steepest descent, and why back-propagation takes the form it does. Integral calculus is then discussed to the degree needed to support our next topic, probability theory.\n",
24-
"\n",
25-
"Problems encountered in practice frequently are not certain, and thus we need a language to speak about uncertain things. We review the theory of random variables and the most commonly encountered distributions so we may discuss models probabilistically. This provides the foundation for the naive Bayes classifier, a probabilistic classification technique.\n",
26-
"\n",
27-
"Closely related to probability theory is the study of statistics. While statistics is far too large a field to do justice in a short section, we will introduce fundamental concepts that all machine learning practitioners should be aware of, in particular: evaluating and comparing estimators, conducting hypothesis tests, and constructing confidence intervals.\n",
28-
"\n",
29-
"Last, we turn to the topic of information theory, which is the mathematical study of information storage and transmission. This provides the core language by which we may discuss quantitatively how much information a model holds on a domain of discourse.\n",
30-
"\n",
31-
"Taken together, these form the core of the mathematical concepts needed to begin down the path towards a deep understanding of deep learning.\n",
32-
"\n",
33-
":begin_tab:toc\n",
34-
" - [geometry-linear-algebraic-ops](geometry-linear-algebraic-ops.ipynb)\n",
35-
" - [eigendecomposition](eigendecomposition.ipynb)\n",
36-
" - [single-variable-calculus](single-variable-calculus.ipynb)\n",
37-
" - [multivariable-calculus](multivariable-calculus.ipynb)\n",
38-
" - [integral-calculus](integral-calculus.ipynb)\n",
39-
" - [random-variables](random-variables.ipynb)\n",
40-
" - [maximum-likelihood](maximum-likelihood.ipynb)\n",
41-
" - [distributions](distributions.ipynb)\n",
42-
" - [naive-bayes](naive-bayes.ipynb)\n",
43-
" - [statistics](statistics.ipynb)\n",
44-
" - [information-theory](information-theory.ipynb)\n",
45-
":end_tab:\n"
46-
]
47-
}
48-
],
49-
"metadata": {
50-
"kernelspec": {
51-
"display_name": "Python 3",
52-
"name": "python3"
53-
},
54-
"language_info": {
55-
"name": "python"
56-
}
57-
},
58-
"nbformat": 4,
59-
"nbformat_minor": 4
60-
}

chapter_appendix-mathematics-for-deep-learning/information-theory.ipynb

+46-27
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@
5555
"execution_count": 1,
5656
"metadata": {
5757
"origin_pos": 2,
58-
"tab": "pytorch"
58+
"tab": [
59+
"pytorch"
60+
]
5961
},
6062
"outputs": [
6163
{
@@ -80,7 +82,7 @@
8082
"def self_information(p):\n",
8183
" return -torch.log2(torch.tensor(p)).item()\n",
8284
"\n",
83-
"self_information(1/64)"
85+
"self_information(1 / 64)"
8486
]
8587
},
8688
{
@@ -125,7 +127,9 @@
125127
"execution_count": 2,
126128
"metadata": {
127129
"origin_pos": 5,
128-
"tab": "pytorch"
130+
"tab": [
131+
"pytorch"
132+
]
129133
},
130134
"outputs": [
131135
{
@@ -142,7 +146,7 @@
142146
"source": [
143147
"def entropy(p):\n",
144148
" entropy = - p * torch.log2(p)\n",
145-
" # nansum will sum up the non-nan number\n",
149+
" # Operator nansum will sum up the non-nan number\n",
146150
" out = nansum(entropy)\n",
147151
" return out\n",
148152
"\n",
@@ -216,7 +220,9 @@
216220
"execution_count": 3,
217221
"metadata": {
218222
"origin_pos": 8,
219-
"tab": "pytorch"
223+
"tab": [
224+
"pytorch"
225+
]
220226
},
221227
"outputs": [
222228
{
@@ -281,7 +287,9 @@
281287
"execution_count": 4,
282288
"metadata": {
283289
"origin_pos": 11,
284-
"tab": "pytorch"
290+
"tab": [
291+
"pytorch"
292+
]
285293
},
286294
"outputs": [
287295
{
@@ -349,7 +357,9 @@
349357
"execution_count": 5,
350358
"metadata": {
351359
"origin_pos": 14,
352-
"tab": "pytorch"
360+
"tab": [
361+
"pytorch"
362+
]
353363
},
354364
"outputs": [
355365
{
@@ -367,13 +377,12 @@
367377
"def mutual_information(p_xy, p_x, p_y):\n",
368378
" p = p_xy / (p_x * p_y)\n",
369379
" mutual = p_xy * torch.log2(p)\n",
370-
" # nansum will sum up the non-nan number\n",
380+
" # Operator nansum will sum up the non-nan number\n",
371381
" out = nansum(mutual)\n",
372382
" return out\n",
373383
"\n",
374384
"mutual_information(torch.tensor([[0.1, 0.5], [0.1, 0.3]]),\n",
375-
" torch.tensor([0.2, 0.8]),\n",
376-
" torch.tensor([[0.75, 0.25]]))"
385+
" torch.tensor([0.2, 0.8]), torch.tensor([[0.75, 0.25]]))"
377386
]
378387
},
379388
{
@@ -431,7 +440,9 @@
431440
"execution_count": 6,
432441
"metadata": {
433442
"origin_pos": 17,
434-
"tab": "pytorch"
443+
"tab": [
444+
"pytorch"
445+
]
435446
},
436447
"outputs": [],
437448
"source": [
@@ -474,7 +485,9 @@
474485
"execution_count": 7,
475486
"metadata": {
476487
"origin_pos": 20,
477-
"tab": "pytorch"
488+
"tab": [
489+
"pytorch"
490+
]
478491
},
479492
"outputs": [],
480493
"source": [
@@ -504,7 +517,9 @@
504517
"execution_count": 8,
505518
"metadata": {
506519
"origin_pos": 23,
507-
"tab": "pytorch"
520+
"tab": [
521+
"pytorch"
522+
]
508523
},
509524
"outputs": [
510525
{
@@ -540,7 +555,9 @@
540555
"execution_count": 9,
541556
"metadata": {
542557
"origin_pos": 26,
543-
"tab": "pytorch"
558+
"tab": [
559+
"pytorch"
560+
]
544561
},
545562
"outputs": [
546563
{
@@ -606,7 +623,9 @@
606623
"execution_count": 10,
607624
"metadata": {
608625
"origin_pos": 29,
609-
"tab": "pytorch"
626+
"tab": [
627+
"pytorch"
628+
]
610629
},
611630
"outputs": [],
612631
"source": [
@@ -629,7 +648,9 @@
629648
"execution_count": 11,
630649
"metadata": {
631650
"origin_pos": 32,
632-
"tab": "pytorch"
651+
"tab": [
652+
"pytorch"
653+
]
633654
},
634655
"outputs": [
635656
{
@@ -716,7 +737,9 @@
716737
"execution_count": 12,
717738
"metadata": {
718739
"origin_pos": 35,
719-
"tab": "pytorch"
740+
"tab": [
741+
"pytorch"
742+
]
720743
},
721744
"outputs": [
722745
{
@@ -731,8 +754,8 @@
731754
}
732755
],
733756
"source": [
734-
"# Implementation of CrossEntropy loss in pytorch \n",
735-
"# combines nn.LogSoftmax() and nn.NLLLoss().\n",
757+
"# Implementation of CrossEntropy loss in pytorch combines nn.LogSoftmax() and\n",
758+
"# nn.NLLLoss()\n",
736759
"nll_loss = NLLLoss()\n",
737760
"loss = nll_loss(torch.log(preds), labels)\n",
738761
"loss"
@@ -755,17 +778,13 @@
755778
"## Exercises\n",
756779
"\n",
757780
"1. Verify that the card examples from the first section indeed have the claimed entropy.\n",
758-
"2. Let us compute the entropy from a few data sources:\n",
781+
"1. Show that the KL divergence $D(p\\|q)$ is nonnegative for all distributions $p$ and $q$. Hint: use Jensen's inequality, i.e., use the fact that $-\\log x$ is a convex function.\n",
782+
"1. Let us compute the entropy from a few data sources:\n",
759783
" * Assume that you are watching the output generated by a monkey at a typewriter. The monkey presses any of the $44$ keys of the typewriter at random (you can assume that it has not discovered any special keys or the shift key yet). How many bits of randomness per character do you observe?\n",
760784
" * Being unhappy with the monkey, you replaced it by a drunk typesetter. It is able to generate words, albeit not coherently. Instead, it picks a random word out of a vocabulary of $2,000$ words. Moreover, assume that the average length of a word is $4.5$ letters in English. How many bits of randomness do you observe now?\n",
761785
" * Still being unhappy with the result, you replace the typesetter by a high quality language model. These can currently obtain perplexity numbers as low as $15$ points per character. The perplexity is defined as a length normalized probability, i.e., $$PPL(x) = \\left[p(x)\\right]^{1 / \\text{length(x)} }.$$ How many bits of randomness do you observe now?\n",
762-
"3. Explain intuitively why $I(X, Y) = H(X) - H(X|Y)$. Then, show this is true by expressing both sides as an expectation with respect to the joint distribution.\n",
763-
"4. What is the KL Divergence between the two Gaussian distributions $\\mathcal{N}(\\mu_1, \\sigma_1^2)$ and $\\mathcal{N}(\\mu_2, \\sigma_2^2)$?\n",
764-
"\n",
765-
"\n",
766-
"## [Discussions](https://discuss.mxnet.io/t/5157)\n",
767-
"\n",
768-
"![](../img/qr_information-theory.svg)\n"
786+
"1. Explain intuitively why $I(X, Y) = H(X) - H(X|Y)$. Then, show this is true by expressing both sides as an expectation with respect to the joint distribution.\n",
787+
"1. What is the KL Divergence between the two Gaussian distributions $\\mathcal{N}(\\mu_1, \\sigma_1^2)$ and $\\mathcal{N}(\\mu_2, \\sigma_2^2)$?\n"
769788
]
770789
}
771790
],

0 commit comments

Comments
 (0)