Þ ¤pathÙ:/home/runner/work/LINMA2472/LINMA2472/Lectures/autodiff.jl¨metadata€¬cell_resultsÞ ªÙ$90850509-463d-44c7-88ae-4406aebd4be1Чrunning§runtimeÎ àÕ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$90850509-463d-44c7-88ae-4406aebd4be1¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyÙN
Computation graph
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAڸة”Ù²°persist_js_state·has_pluto_hook_featuresÂÙ$77a7de14-87d2-11ef-21ef-937b8239db5bЧrunning§runtimeÎ Ìé¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$77a7de14-87d2-11ef-21ef-937b8239db5b¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyÚiDifferentiation approaches
We can compute partial derivatives in different ways:
Symbolically , by fixing one of the variables and differentiating with respect to the others, either manually or using a computer.
Numerically , using the formula $f'(x) \approx (f(x + h) - f(x)) / h$ .
Algorithmically , either forward or reverse : this is what we will explore here.
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAڸة“Ùb°persist_js_state·has_pluto_hook_featuresÂÙ$e46fb3ff-b26f-4efb-aaa1-760e80017797Чrunning§runtimeÎ úø¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$e46fb3ff-b26f-4efb-aaa1-760e80017797¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyÙ@
Chain rule
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAڸة“î�°persist_js_state·has_pluto_hook_featuresÂÙ$a3ba6cd3-b2c8-42c0-9483-cb0c0c88ed8eЧrunning§runtime΢TƸdepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$a3ba6cd3-b2c8-42c0-9483-cb0c0c88ed8e¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyÚP¥
$s_0$ $f_1$ $s_1$ $\cdots$ $s_{k-1}$ $f_k$ $s_k$ $\cdots$ $s_{K-1}$ $f_K$ $s_K$ $t_0$ $\partial f_1$ $t_1$ $\cdots$ $t_{k-1}$ $\partial f_k$ $t_k$ $\cdots$ $t_{K-1}$ $\partial f_K$ $t_K$ $r_0$ $\partial f^*_1$ $r_1$ $\cdots$ $r_{k-1}$ $\partial f^*_k$ $r_k$ $\cdots$ $r_{K-1}$ $\partial f^*_K$ $r_K$ $\dfrac{\partial s_{0}}{\partial s_0}$ $\dfrac{\partial s_K}{\partial s_{0}}$ $\dfrac{\partial s_{1}}{\partial s_0}$ $\dfrac{\partial s_K}{\partial s_{1}}$ $\dfrac{\partial s_{k-1}}{\partial s_0}$ $\dfrac{\partial s_K}{\partial s_{k-1}}$ $\dfrac{\partial s_{k}}{\partial s_0}$ $\dfrac{\partial s_K}{\partial s_{k}}$ $\dfrac{\partial s_{K-1}}{\partial s_0}$ $\dfrac{\partial s_K}{\partial s_{K-1}}$ $\dfrac{\partial s_{K}}{\partial s_0}$ $\dfrac{\partial s_K}{\partial s_{K}}$
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAڸخ‚ðN°persist_js_state·has_pluto_hook_featuresÂÙ$78d8c8c9-568d-472a-9f06-a50b1cf2384bЧrunning§runtimeÀ¸depends_on_skipped_cellsµpublished_object_keys�§erroredçcell_idÙ$78d8c8c9-568d-472a-9f06-a50b1cf2384b¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyƒ£msg³InterruptException:ªstacktrace�«plain_error³InterruptException:¤mimeÙ'application/vnd.pluto.stacktrace+object¬rootassigneeÀ²last_run_timestampËAڸز|£˜°persist_js_state·has_pluto_hook_featuresÂÙ$9661a3b9-ae42-466e-967d-69230b9769b3Чrunning§runtimeÎEÏ«P¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$9661a3b9-ae42-466e-967d-69230b9769b3¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyÚé«
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAڸد¦”B°persist_js_state·has_pluto_hook_featuresÂÙ$2e8dd18a-b784-4f7d-ac7d-e1f88ba2ca56Чrunning§runtimeÎ�”¡¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$2e8dd18a-b784-4f7d-ac7d-e1f88ba2ca56¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyÚ¤mime©text/html¬rootassigneeÀ²last_run_timestampËAڸدG²°persist_js_state·has_pluto_hook_featuresÂÙ$7921c4c6-56b8-4c6f-a9be-cd1d9984680bЧrunning§runtimeÀ¸depends_on_skipped_cellsµpublished_object_keys�§erroredçcell_idÙ$7921c4c6-56b8-4c6f-a9be-cd1d9984680b¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyƒ£msg³InterruptException:ªstacktrace�«plain_error³InterruptException:¤mimeÙ'application/vnd.pluto.stacktrace+object¬rootassigneeÀ²last_run_timestampËAڸز|¢©°persist_js_state·has_pluto_hook_featuresÂÙ$54457653-f029-4368-8bc0-15d69f22bfcdЧrunning§runtimeÎ *S¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$54457653-f029-4368-8bc0-15d69f22bfcd¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyÚ0
Start with an input vector $x$ .
Apply a matrix product $y = W_1 x$ (or $y = W_1 x + b_1$ with bias).
Apply an activation function, say tanh, independently to each entry: $z_i = \tanh(y_i)$ . Using the broadcasting syntax of Julia, we can write $z = \tanh.(y)$ . Usually denoted $\sigma(y)$ .
Repeat with $W_2$ for next layer etc...
$$\hat{y} = W_\ell \tanh.(W_{\ell-1} \tanh.(\cdots W_2 \tanh.(W_1 x)))$$
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAڸة— .°persist_js_state·has_pluto_hook_featuresÂÙ$37bfd8ed-8983-4a89-9ba5-a90a35151729Чrunning§runtimeÎ ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$37bfd8ed-8983-4a89-9ba5-a90a35151729¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyÚÉLet's turn it into a chain!
Key idea: introduce $\text{dup}(x) = (x, x)$ so that $f = g_2 \circ g_1$ is a chain with $g_1 = \text{dup}$ and $g_2(x_1, x_2) = (f_1(x_1), f_2(x_2))$ .
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAڸة”Ž7°persist_js_state·has_pluto_hook_featuresÂÙ$a580ef44-234a-4ed1-b007-920651415427Чrunning§runtimeÎi.<¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$a580ef44-234a-4ed1-b007-920651415427¹depends_on_disabled_cells¦queued¤logs�¦output†¤body«28.025774f0¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAڸذ檤°persist_js_state·has_pluto_hook_featuresÂÙ$e9d6af05-63f7-4d31-85e2-b0233df23608Чrunning§runtimeÎ +4¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$e9d6af05-63f7-4d31-85e2-b0233df23608¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyÚ`$\langle\rangle_i$ , the last one should be just product of scalars to use $r_K = 1$ , the first one should be canonical to be one-hot. The inner ones can be anything. Multiplying it by 2 makes one adjoint be multiplied by 2, the other by 1/2, it just cancels out.
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAڸة˜ ɰpersist_js_state·has_pluto_hook_featuresÂÙ$edad5700-9b04-47c6-90f8-b6bac0897340Чrunning§runtimeÀ¸depends_on_skipped_cellsµpublished_object_keys�§erroredçcell_idÙ$edad5700-9b04-47c6-90f8-b6bac0897340¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyƒ£msg³InterruptException:ªstacktrace�«plain_error³InterruptException:¤mimeÙ'application/vnd.pluto.stacktrace+object¬rootassigneeÀ²last_run_timestampËAڸز|¢Â°persist_js_state·has_pluto_hook_featuresÂÙ$11eadcc1-68bf-4ca7-9603-ef46468b4779Чrunning§runtimeÀ¸depends_on_skipped_cellsµpublished_object_keys�§erroredçcell_idÙ$11eadcc1-68bf-4ca7-9603-ef46468b4779¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyƒ£msg³InterruptException:ªstacktrace�«plain_error³InterruptException:¤mimeÙ'application/vnd.pluto.stacktrace+object¬rootassigneeÀ²last_run_timestampËAڸز|¢°persist_js_state·has_pluto_hook_featuresÂÙ$dc4feb58-d2cf-4a97-aaed-7f4593fc9732Чrunning§runtimeÎ ÂŽ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$dc4feb58-d2cf-4a97-aaed-7f4593fc9732¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyÙF
Discontinuity
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAڸة–·°persist_js_state·has_pluto_hook_featuresÂÙ$cbfc0129-9361-4edb-a467-1456a1f3aeaeЧrunning§runtimeÎWe¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$cbfc0129-9361-4edb-a467-1456a1f3aeae¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyÙ%img (generic function with 3 methods)¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAڸدø—°persist_js_state·has_pluto_hook_featuresÂÙ$b92d17a9-8481-458a-bc0a-efb7333cbc6eЧrunning§runtimeÎç²À¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$b92d17a9-8481-458a-bc0a-efb7333cbc6e¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyƒªattributes‚¥classÀ¥styleÙ#display: flex; flex-direction: row;£tag£div¨children“’ƒªattributes‚¥classÀ¥style°flex: 0 1 49.0%;£tag£div¨children‘’ÚðRight-to-left
$$\begin{align*}
S_{0} & = X\\
S_{2k-1} & = W_k S_{2k-2} + b_{k} \mathbf{1}^\top\\
S_{2k} & = \sigma(S_{2k-1})\\
S_{2H+1} & = W_{k+1} S_{2H}\\
S_{2H+2} & = \ell(S_{2H+1}; Y)\\
\end{align*}$$
©text/htmlÙ,application/vnd.pluto.reactdomelement+object’ƒªattributes‚¥classÀ¥styleflex: 0 0 2%;£tag£div¨children�Ù,application/vnd.pluto.reactdomelement+object’ƒªattributes‚¥classÀ¥style°flex: 0 1 49.0%;£tag£div¨children‘’ÚðLeft-to-right
$$\begin{align*}
S_{0} & = X\\
S_{2k-1} & = S_{2k-2} W_k + \mathbf{1} b_{k}^\top\\
S_{2k} & = \sigma(S_{2k-1})\\
S_{2H+1} & = S_{2H} W_{k+1}\\
S_{2H+2} & = \ell(S_{2H+1}; Y)\\
\end{align*}$$
©text/htmlÙ,application/vnd.pluto.reactdomelement+object¤mimeÙ,application/vnd.pluto.reactdomelement+object¬rootassigneeÀ²last_run_timestampËAڸ؋Ϟ°persist_js_state·has_pluto_hook_featuresÂÙ$ee3299e2-3367-4c68-a050-c26650a97c8cЧrunning§runtimeÎ5D’™¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$ee3299e2-3367-4c68-a050-c26650a97c8c¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyÛ é
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAڸدYÖ¿°persist_js_state·has_pluto_hook_featuresÂÙ$db7aaa4c-5568-4619-a9c4-00c7916c321aЧrunning§runtimeÍn¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$db7aaa4c-5568-4619-a9c4-00c7916c321a¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyÚ13×178 Matrix{Float64}:
14.23 13.2 13.16 14.37 … 13.71 13.4 13.27 13.17 14.13
1.71 1.78 2.36 1.95 5.65 3.91 4.28 2.59 4.1
2.43 2.14 2.67 2.5 2.45 2.48 2.26 2.37 2.74
15.6 11.2 18.6 16.8 20.5 23.0 20.0 20.0 24.5
127.0 100.0 101.0 113.0 95.0 102.0 120.0 120.0 96.0
2.8 2.65 2.8 3.85 … 1.68 1.8 1.59 1.65 2.05
3.06 2.76 3.24 3.49 0.61 0.75 0.69 0.68 0.76
0.28 0.26 0.3 0.24 0.52 0.43 0.43 0.53 0.56
2.29 1.28 2.81 2.18 1.06 1.41 1.35 1.46 1.35
5.64 4.38 5.68 7.8 7.7 7.3 10.2 9.3 9.2
1.04 1.05 1.03 0.86 … 0.64 0.7 0.59 0.6 0.61
3.92 3.4 3.17 3.45 1.74 1.56 1.56 1.62 1.6
1065.0 1050.0 1185.0 1480.0 740.0 750.0 835.0 840.0 560.0¤mimeªtext/plain¬rootassignee¨features²last_run_timestampËAڸذd*°persist_js_state·has_pluto_hook_featuresÂÙ$068ef840-04cb-4de4-9512-721f57e5750eЧrunning§runtimeÎ ¾¥¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$068ef840-04cb-4de4-9512-721f57e5750e¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyÙ:
Adjoint
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAڸة—“¼°persist_js_state·has_pluto_hook_featuresÂÙ$95eb9960-89f0-4edc-8943-77a75bce2b80Чrunning§runtimeÀ¸depends_on_skipped_cellsµpublished_object_keys�§erroredçcell_idÙ$95eb9960-89f0-4edc-8943-77a75bce2b80¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyƒ£msg³InterruptException:ªstacktrace�«plain_error³InterruptException:¤mimeÙ'application/vnd.pluto.stacktrace+object¬rootassigneeÀ²last_run_timestampËAڸز|£°persist_js_state·has_pluto_hook_featuresÂÙ$fa5dba01-a3f7-452c-877e-352d578ecf51Чrunning§runtimeÎ!5¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$fa5dba01-a3f7-452c-877e-352d578ecf51¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyƒªattributes‚¥classÀ¥styleÙ#display: flex; flex-direction: row;£tag£div¨children•’ÚÏForward
$$\begin{align}
t_3 & = \partial_1 f_3 \cdot t_1 + \partial_2 f_3 \cdot t_2\\
& =
\begin{bmatrix}
\partial_1 f_3 & \partial_2 f_3
\end{bmatrix} \cdot
\begin{bmatrix}
t_1\\
t_2
\end{bmatrix}\\
& =
\partial f_3 \cdot
\begin{bmatrix}
t_1\\
t_2
\end{bmatrix}
\end{align}$$
©text/html’ƒªattributes‚¥classÀ¥styleflex-grow: 1;£tag£div¨children‘’¡ ©text/htmlÙ,application/vnd.pluto.reactdomelement+object’Ú¨Reverse
$$\begin{align}
\begin{bmatrix}
r_1 &
r_2
\end{bmatrix}
& \mathrel{\raise{0.19ex}{\scriptstyle+}} = r_3 \cdot \partial f_3\\
& \mathrel{\raise{0.19ex}{\scriptstyle+}} = r_3 \cdot
\begin{bmatrix}
\partial_1 f_3 & \partial_2 f_3
\end{bmatrix}\\
& \mathrel{\raise{0.19ex}{\scriptstyle+}} =
\begin{bmatrix}
r_3 \cdot\partial_1 f_3 & r_3 \cdot\partial_2 f_3
\end{bmatrix}
\end{align}$$
©text/html’ƒªattributes‚¥classÀ¥styleflex-grow: 1;£tag£div¨children‘’¡ ©text/htmlÙ,application/vnd.pluto.reactdomelement+object’Ú£Reverse*
$$\begin{align}
\begin{bmatrix}
r_1\\
r_2
\end{bmatrix} & \mathrel{\raise{0.19ex}{\scriptstyle+}} = \partial f_3^* \cdot r_3\\
& \mathrel{\raise{0.19ex}{\scriptstyle+}} =
\begin{bmatrix}
\partial_1 f_3\\ \partial_2 f_3
\end{bmatrix} \cdot r_3\\
& \mathrel{\raise{0.19ex}{\scriptstyle+}} =
\begin{bmatrix}
\partial_1 f_3 \cdot r_3 \\ \partial_2 f_3 \cdot r_3
\end{bmatrix}
\end{align}$$
©text/html¤mimeÙ,application/vnd.pluto.reactdomelement+object¬rootassigneeÀ²last_run_timestampËAÚ¸ØeÈ—°persist_js_state·has_pluto_hook_featuresÂÙ$5aff8e66-787d-4dc5-a9b1-0fdec25ce0f0Чrunning§runtimeÎ ¼B¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$5aff8e66-787d-4dc5-a9b1-0fdec25ce0f0¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyÙL
Reverse tangents
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAڸة”ÉŠ°persist_js_state·has_pluto_hook_featuresÂÙ$69c08fab-c317-462c-817c-3f841a8a0941Чrunning§runtimeÎ Ea¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$69c08fab-c317-462c-817c-3f841a8a0941¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyÙáWhen using automatic differentiation, don't forget that we must always evaluate the derivatives. For the following example we choose to evaluate it in $x=3$
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAڸة”`g°persist_js_state·has_pluto_hook_featuresÂÙ$e8b1c40d-27a6-4f39-b95e-6d3a0f81c72bЧrunning§runtimeΣM¸¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$e8b1c40d-27a6-4f39-b95e-6d3a0f81c72b¹depends_on_disabled_cells¦queued¤logs�¦output†¤body ¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAڸخœÜ#°persist_js_state·has_pluto_hook_featuresÂÙ$d18fb5c2-6e47-4a90-b3d1-90c7af4e2b16Чrunning§runtimeÎR˜Š¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$d18fb5c2-6e47-4a90-b3d1-90c7af4e2b16¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyÚ‡
1
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAڸدÛÙ°persist_js_state·has_pluto_hook_featuresÂÙ$2fcf25d2-fd51-4c13-b57c-86236aceead2Чrunning§runtimeÎ#%ý¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$2fcf25d2-fd51-4c13-b57c-86236aceead2¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyÙn1×178 Matrix{Float32}:
-1.0 -1.0 -1.0 -1.0 -1.0 -1.0 -1.0 … 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0¤mimeªtext/plain¬rootassignee¡y²last_run_timestampËAڸذ¤EF°persist_js_state·has_pluto_hook_featuresÂÙ$1c44706b-fd7d-4826-84a3-73e2db5adaddЧrunning§runtimeÎ /æ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$1c44706b-fd7d-4826-84a3-73e2db5adadd¹depends_on_disabled_cells¦queued¤logs�¦output†¤bodyÙH
Implementation
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAڸة”([°persist_js_state·has_pluto_hook_featuresÂÙ$e50f8f52-a73f-4186-af5e-b4ca2c021142Чrunning§runtimeÍ8ö¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$e50f8f52-a73f-4186-af5e-b4ca2c021142¹depends_on_disabled_cells¦queued¤logs�¦output†¤body…¦prefix«Dual{Int64}¨elements’’¥value’¡0ªtext/plain’ªderivative’¡1ªtext/plain¤type¦struct¬prefix_short¤Dual¨objectid°86128cc9be2ded2d¤mimeÙ!application/vnd.pluto.tree+object¬rootassigneeÀ²last_run_timestampËAÚ¸Øt