update on jupyter-book
@@ -77,3 +77,13 @@ found info about 5 exercises
|
||||
|
||||
*** warning: latex envir \begin{bmatrix} does not work well in Markdown. Stick to \[ ... \], equation, equation*, align, or align* environments in math environments.
|
||||
output in chapter1.ipynb
|
||||
Translating doconce text in chapter1.do.txt to ipynb
|
||||
*** replacing \bm{...} by \boldsymbol{...} (\bm is not supported by MathJax)
|
||||
found info about 5 exercises
|
||||
|
||||
*** warning: latex envir \begin{bmatrix} does not work well in Markdown. Stick to \[ ... \], equation, equation*, align, or align* environments in math environments.
|
||||
|
||||
*** warning: latex envir \begin{bmatrix} does not work well in Markdown. Stick to \[ ... \], equation, equation*, align, or align* environments in math environments.
|
||||
|
||||
*** warning: latex envir \begin{bmatrix} does not work well in Markdown. Stick to \[ ... \], equation, equation*, align, or align* environments in math environments.
|
||||
output in chapter1.ipynb
|
||||
|
||||
@@ -660,7 +660,7 @@ import os
|
||||
# Where to save the figures and data files
|
||||
PROJECT_ROOT_DIR = "Results"
|
||||
FIGURE_ID = "Results/FigureFiles"
|
||||
DATA_ID = "datafiles/"
|
||||
DATA_ID = "DataFiles/"
|
||||
|
||||
if not os.path.exists(PROJECT_ROOT_DIR):
|
||||
os.mkdir(PROJECT_ROOT_DIR)
|
||||
@@ -1102,7 +1102,7 @@ import os
|
||||
# Where to save the figures and data files
|
||||
PROJECT_ROOT_DIR = "Results"
|
||||
FIGURE_ID = "Results/FigureFiles"
|
||||
DATA_ID = "datafiles/"
|
||||
DATA_ID = "DataFiles/"
|
||||
|
||||
if not os.path.exists(PROJECT_ROOT_DIR):
|
||||
os.mkdir(PROJECT_ROOT_DIR)
|
||||
@@ -1607,7 +1607,7 @@ from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error
|
||||
# Where to save the figures and data files
|
||||
PROJECT_ROOT_DIR = "Results"
|
||||
FIGURE_ID = "Results/FigureFiles"
|
||||
DATA_ID = "datafiles/"
|
||||
DATA_ID = "DataFiles/"
|
||||
|
||||
if not os.path.exists(PROJECT_ROOT_DIR):
|
||||
os.mkdir(PROJECT_ROOT_DIR)
|
||||
@@ -1771,7 +1771,7 @@ from sklearn.model_selection import train_test_split
|
||||
# Where to save the figures and data files
|
||||
PROJECT_ROOT_DIR = "Results"
|
||||
FIGURE_ID = "Results/FigureFiles"
|
||||
DATA_ID = "datafiles/"
|
||||
DATA_ID = "DataFiles/"
|
||||
|
||||
if not os.path.exists(PROJECT_ROOT_DIR):
|
||||
os.mkdir(PROJECT_ROOT_DIR)
|
||||
|
||||
@@ -300,25 +300,22 @@ ensures convexity of a function $f$. We write $D_f$ to denote the
|
||||
domain of $f$, i.e the subset of $R^n$ where $f$ is defined. For more
|
||||
details and proofs we refer to: "S. Boyd and L. Vandenberghe. Convex Optimization. Cambridge University Press":"http://stanford.edu/boyd/cvxbook/, 2004".
|
||||
|
||||
!bblock First order condition
|
||||
Suppose $f$ is differentiable (i.e $\nabla f(x)$ is well defined for
|
||||
_First order condition_: Suppose $f$ is differentiable (i.e $\nabla f(x)$ is well defined for
|
||||
all $x$ in the domain of $f$). Then $f$ is convex if and only if $D_f$
|
||||
is a convex set and $$f(y) \geq f(x) + \nabla f(x)^T (y-x) $$ holds
|
||||
is a convex set and $f(y) \geq f(x) + \nabla f(x)^T (y-x)$ holds
|
||||
for all $x,y \in D_f$. This condition means that for a convex function
|
||||
the first order Taylor expansion (right hand side above) at any point
|
||||
is a global under estimator of the function. To convince yourself you can
|
||||
make a drawing of $f(x) = x^2+1$ and draw the tangent line to $f(x)$ and
|
||||
note that it is always below the graph.
|
||||
!eblock
|
||||
|
||||
!bblock Second order condition
|
||||
Assume that $f$ is twice
|
||||
|
||||
_Second order condition_: Assume that $f$ is twice
|
||||
differentiable, i.e the Hessian matrix exists at each point in
|
||||
$D_f$. Then $f$ is convex if and only if $D_f$ is a convex set and its
|
||||
Hessian is positive semi-definite for all $x\in D_f$. For a
|
||||
single-variable function this reduces to $f''(x) \geq 0$. Geometrically this means that $f$ has nonnegative curvature
|
||||
everywhere.
|
||||
!eblock
|
||||
|
||||
This condition is particularly useful since it gives us an procedure for determining if the function under consideration is convex, apart from using the definition.
|
||||
|
||||
@@ -1705,6 +1702,7 @@ Autograd supports many features. However, there are some functions that is not s
|
||||
|
||||
Assigning a value to the variable being differentiated with respect to
|
||||
!bc pycod
|
||||
"""
|
||||
import autograd.numpy as np
|
||||
from autograd import grad
|
||||
def f8(x): # Assume x is an array
|
||||
@@ -1716,6 +1714,7 @@ f8_grad = grad(f8)
|
||||
x = 8.4
|
||||
|
||||
print("The derivative of f8 is:",f8_grad(x))
|
||||
"""
|
||||
!ec
|
||||
Here, Autograd tells us that an 'ArrayBox' does not support item assignment. The item assignment is done when the program tries to assign x[2] to the value 3. However, Autograd has implemented the computation of the derivative such that this assignment is not possible.
|
||||
|
||||
|
||||
|
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 13 KiB |
|
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 18 KiB |
|
Before Width: | Height: | Size: 25 KiB After Width: | Height: | Size: 26 KiB |
|
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 18 KiB |
|
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 23 KiB |
|
Before Width: | Height: | Size: 21 KiB After Width: | Height: | Size: 22 KiB |
|
Before Width: | Height: | Size: 21 KiB After Width: | Height: | Size: 22 KiB |
@@ -957,13 +957,13 @@ example of the functionality of <strong>Scikit-Learn</strong>.</p>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>The intercept alpha:
|
||||
[2.03523311]
|
||||
[1.72261919]
|
||||
Coefficient beta :
|
||||
[[4.99498108]]
|
||||
Mean squared error: 0.27
|
||||
Variance score: 0.87
|
||||
[[5.39493843]]
|
||||
Mean squared error: 0.23
|
||||
Variance score: 0.91
|
||||
Mean squared log error: 0.01
|
||||
Mean absolute error: 0.41
|
||||
Mean absolute error: 0.39
|
||||
</pre></div>
|
||||
</div>
|
||||
<img alt="_images/chapter1_19_1.png" src="_images/chapter1_19_1.png" />
|
||||
@@ -1063,7 +1063,7 @@ a linear <span class="math notranslate nohighlight">\(x\)</span>-dependence we s
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<img alt="_images/chapter1_33_0.png" src="_images/chapter1_33_0.png" />
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>0.00499999999999999
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>0.0050000000000000044
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -1148,7 +1148,7 @@ After having downloaded this file to our own computer, we are now ready to read
|
||||
<span class="c1"># Where to save the figures and data files</span>
|
||||
<span class="n">PROJECT_ROOT_DIR</span> <span class="o">=</span> <span class="s2">"Results"</span>
|
||||
<span class="n">FIGURE_ID</span> <span class="o">=</span> <span class="s2">"Results/FigureFiles"</span>
|
||||
<span class="n">DATA_ID</span> <span class="o">=</span> <span class="s2">"datafiles/"</span>
|
||||
<span class="n">DATA_ID</span> <span class="o">=</span> <span class="s2">"DataFiles/"</span>
|
||||
|
||||
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">PROJECT_ROOT_DIR</span><span class="p">):</span>
|
||||
<span class="n">os</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">PROJECT_ROOT_DIR</span><span class="p">)</span>
|
||||
@@ -1611,7 +1611,7 @@ our matrix as <span class="math notranslate nohighlight">\(\boldsymbol{X}\in {\m
|
||||
<span class="c1"># Where to save the figures and data files</span>
|
||||
<span class="n">PROJECT_ROOT_DIR</span> <span class="o">=</span> <span class="s2">"Results"</span>
|
||||
<span class="n">FIGURE_ID</span> <span class="o">=</span> <span class="s2">"Results/FigureFiles"</span>
|
||||
<span class="n">DATA_ID</span> <span class="o">=</span> <span class="s2">"datafiles/"</span>
|
||||
<span class="n">DATA_ID</span> <span class="o">=</span> <span class="s2">"DataFiles/"</span>
|
||||
|
||||
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">PROJECT_ROOT_DIR</span><span class="p">):</span>
|
||||
<span class="n">os</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">PROJECT_ROOT_DIR</span><span class="p">)</span>
|
||||
@@ -2048,7 +2048,7 @@ instead of our own matrix inversion implementation.</p>
|
||||
<span class="c1"># Where to save the figures and data files</span>
|
||||
<span class="n">PROJECT_ROOT_DIR</span> <span class="o">=</span> <span class="s2">"Results"</span>
|
||||
<span class="n">FIGURE_ID</span> <span class="o">=</span> <span class="s2">"Results/FigureFiles"</span>
|
||||
<span class="n">DATA_ID</span> <span class="o">=</span> <span class="s2">"datafiles/"</span>
|
||||
<span class="n">DATA_ID</span> <span class="o">=</span> <span class="s2">"DataFiles/"</span>
|
||||
|
||||
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">PROJECT_ROOT_DIR</span><span class="p">):</span>
|
||||
<span class="n">os</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">PROJECT_ROOT_DIR</span><span class="p">)</span>
|
||||
@@ -2213,7 +2213,7 @@ but now splitting the data into a training set and a test set.</p>
|
||||
<span class="c1"># Where to save the figures and data files</span>
|
||||
<span class="n">PROJECT_ROOT_DIR</span> <span class="o">=</span> <span class="s2">"Results"</span>
|
||||
<span class="n">FIGURE_ID</span> <span class="o">=</span> <span class="s2">"Results/FigureFiles"</span>
|
||||
<span class="n">DATA_ID</span> <span class="o">=</span> <span class="s2">"datafiles/"</span>
|
||||
<span class="n">DATA_ID</span> <span class="o">=</span> <span class="s2">"DataFiles/"</span>
|
||||
|
||||
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">PROJECT_ROOT_DIR</span><span class="p">):</span>
|
||||
<span class="n">os</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">PROJECT_ROOT_DIR</span><span class="p">)</span>
|
||||
|
||||
@@ -781,17 +781,15 @@ line as discussed below.</p>
|
||||
ensures convexity of a function <span class="math notranslate nohighlight">\(f\)</span>. We write <span class="math notranslate nohighlight">\(D_f\)</span> to denote the
|
||||
domain of <span class="math notranslate nohighlight">\(f\)</span>, i.e the subset of <span class="math notranslate nohighlight">\(R^n\)</span> where <span class="math notranslate nohighlight">\(f\)</span> is defined. For more
|
||||
details and proofs we refer to: [S. Boyd and L. Vandenberghe. Convex Optimization. Cambridge University Press](<a class="reference external" href="http://stanford.edu/boyd/cvxbook/">http://stanford.edu/boyd/cvxbook/</a>, 2004).</p>
|
||||
<p><strong>First order condition.</strong></p>
|
||||
<p>Suppose <span class="math notranslate nohighlight">\(f\)</span> is differentiable (i.e <span class="math notranslate nohighlight">\(\nabla f(x)\)</span> is well defined for
|
||||
<p><strong>First order condition</strong>: Suppose <span class="math notranslate nohighlight">\(f\)</span> is differentiable (i.e <span class="math notranslate nohighlight">\(\nabla f(x)\)</span> is well defined for
|
||||
all <span class="math notranslate nohighlight">\(x\)</span> in the domain of <span class="math notranslate nohighlight">\(f\)</span>). Then <span class="math notranslate nohighlight">\(f\)</span> is convex if and only if <span class="math notranslate nohighlight">\(D_f\)</span>
|
||||
is a convex set and $<span class="math notranslate nohighlight">\(f(y) \geq f(x) + \nabla f(x)^T (y-x) \)</span><span class="math notranslate nohighlight">\( holds
|
||||
for all \)</span>x,y \in D_f<span class="math notranslate nohighlight">\(. This condition means that for a convex function
|
||||
is a convex set and <span class="math notranslate nohighlight">\(f(y) \geq f(x) + \nabla f(x)^T (y-x)\)</span> holds
|
||||
for all <span class="math notranslate nohighlight">\(x,y \in D_f\)</span>. This condition means that for a convex function
|
||||
the first order Taylor expansion (right hand side above) at any point
|
||||
is a global under estimator of the function. To convince yourself you can
|
||||
make a drawing of \)</span>f(x) = x^2+1<span class="math notranslate nohighlight">\( and draw the tangent line to \)</span>f(x)$ and
|
||||
make a drawing of <span class="math notranslate nohighlight">\(f(x) = x^2+1\)</span> and draw the tangent line to <span class="math notranslate nohighlight">\(f(x)\)</span> and
|
||||
note that it is always below the graph.</p>
|
||||
<p><strong>Second order condition.</strong></p>
|
||||
<p>Assume that <span class="math notranslate nohighlight">\(f\)</span> is twice
|
||||
<p><strong>Second order condition</strong>: Assume that <span class="math notranslate nohighlight">\(f\)</span> is twice
|
||||
differentiable, i.e the Hessian matrix exists at each point in
|
||||
<span class="math notranslate nohighlight">\(D_f\)</span>. Then <span class="math notranslate nohighlight">\(f\)</span> is convex if and only if <span class="math notranslate nohighlight">\(D_f\)</span> is a convex set and its
|
||||
Hessian is positive semi-definite for all <span class="math notranslate nohighlight">\(x\in D_f\)</span>. For a
|
||||
@@ -940,11 +938,11 @@ which equals</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_96694/483257001.py:18: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().
|
||||
<div class="output stderr highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_9414/483257001.py:18: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().
|
||||
ax = fig.gca(projection="3d")
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span><mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x11db14850>
|
||||
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span><mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x13b240850>
|
||||
</pre></div>
|
||||
</div>
|
||||
<img alt="_images/chapteroptimization_61_2.png" src="_images/chapteroptimization_61_2.png" />
|
||||
@@ -1002,7 +1000,7 @@ which equals</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[<matplotlib.lines.Line2D at 0x11e09b370>]
|
||||
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[<matplotlib.lines.Line2D at 0x13b8c72e0>]
|
||||
</pre></div>
|
||||
</div>
|
||||
<img alt="_images/chapteroptimization_69_1.png" src="_images/chapteroptimization_69_1.png" />
|
||||
@@ -1259,11 +1257,11 @@ when <span class="math notranslate nohighlight">\(||\nabla_\beta C(\beta_k) || \
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[0.2831603 4.55553537]
|
||||
[[3.91511388]
|
||||
[3.13030182]]
|
||||
[[3.91511388]
|
||||
[3.13030182]]
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[0.25881631 4.66111673]
|
||||
[[4.01840062]
|
||||
[2.89545727]]
|
||||
[[4.01840062]
|
||||
[2.89545727]]
|
||||
</pre></div>
|
||||
</div>
|
||||
<img alt="_images/chapteroptimization_123_1.png" src="_images/chapteroptimization_123_1.png" />
|
||||
@@ -1292,9 +1290,9 @@ when <span class="math notranslate nohighlight">\(||\nabla_\beta C(\beta_k) || \
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[[4.1509778 ]
|
||||
[2.92461411]]
|
||||
[4.13288373] [2.92817032]
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[[3.79441434]
|
||||
[3.07608141]]
|
||||
[3.80994952] [3.12302855]
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -1365,10 +1363,10 @@ C_{\text{ridge}}(\beta) = \frac{1}{n}||X\beta -\mathbf{y}||^2 + \lambda ||\beta|
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[[4.0795449 ]
|
||||
[2.86893619]]
|
||||
[[4.04785727]
|
||||
[2.89298533]]
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[[3.78596961]
|
||||
[3.12387751]]
|
||||
[[3.71441535]
|
||||
[3.17942122]]
|
||||
</pre></div>
|
||||
</div>
|
||||
<img alt="_images/chapteroptimization_132_1.png" src="_images/chapteroptimization_132_1.png" />
|
||||
@@ -1618,15 +1616,15 @@ function.</p>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Own inversion
|
||||
[[4.41170104]
|
||||
[2.6431453 ]]
|
||||
Eigenvalues of Hessian Matrix:[0.31228042 4.55571665]
|
||||
[[4.42130182]
|
||||
[2.83757843]]
|
||||
Eigenvalues of Hessian Matrix:[0.27660123 4.17938393]
|
||||
theta from own gd
|
||||
[[4.41170104]
|
||||
[2.6431453 ]]
|
||||
[[4.42130182]
|
||||
[2.83757843]]
|
||||
theta from own sdg
|
||||
[[4.39272691]
|
||||
[2.63430285]]
|
||||
[[4.44198566]
|
||||
[2.79512696]]
|
||||
</pre></div>
|
||||
</div>
|
||||
<img alt="_images/chapteroptimization_148_1.png" src="_images/chapteroptimization_148_1.png" />
|
||||
@@ -2221,27 +2219,52 @@ The analytical derivative of f7 at n = 2 is: 1
|
||||
<p>Assigning a value to the variable being differentiated with respect to</p>
|
||||
<div class="cell docutils container">
|
||||
<div class="cell_input docutils container">
|
||||
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="sd">"""</span>
|
||||
<span class="sd">import autograd.numpy as np</span>
|
||||
<span class="sd">from autograd import grad</span>
|
||||
<span class="sd">def f8(x): # Assume x is an array</span>
|
||||
<span class="sd"> x[2] = 3</span>
|
||||
<span class="sd"> return x*2</span>
|
||||
|
||||
<span class="sd">f8_grad = grad(f8)</span>
|
||||
|
||||
<span class="sd">x = 8.4</span>
|
||||
|
||||
<span class="sd">print("The derivative of f8 is:",f8_grad(x))</span>
|
||||
<span class="sd">"""</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>'\nimport autograd.numpy as np\nfrom autograd import grad\ndef f8(x): # Assume x is an array\n x[2] = 3\n return x*2\n\nf8_grad = grad(f8)\n\nx = 8.4\n\nprint("The derivative of f8 is:",f8_grad(x))\n'
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p>Here, Autograd tells us that an ‘ArrayBox’ does not support item assignment. The item assignment is done when the program tries to assign x[2] to the value 3. However, Autograd has implemented the computation of the derivative such that this assignment is not possible.</p>
|
||||
<div class="cell docutils container">
|
||||
<div class="cell_input docutils container">
|
||||
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">autograd.numpy</span> <span class="k">as</span> <span class="nn">np</span>
|
||||
<span class="kn">from</span> <span class="nn">autograd</span> <span class="kn">import</span> <span class="n">grad</span>
|
||||
<span class="k">def</span> <span class="nf">f8</span><span class="p">(</span><span class="n">x</span><span class="p">):</span> <span class="c1"># Assume x is an array</span>
|
||||
<span class="n">x</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="mi">3</span>
|
||||
<span class="k">return</span> <span class="n">x</span><span class="o">*</span><span class="mi">2</span>
|
||||
<span class="k">def</span> <span class="nf">f9</span><span class="p">(</span><span class="n">a</span><span class="p">):</span> <span class="c1"># Assume a is an array with 2 elements</span>
|
||||
<span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">1.0</span><span class="p">,</span><span class="mf">2.0</span><span class="p">])</span>
|
||||
<span class="k">return</span> <span class="n">a</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
|
||||
|
||||
<span class="n">f8_grad</span> <span class="o">=</span> <span class="n">grad</span><span class="p">(</span><span class="n">f8</span><span class="p">)</span>
|
||||
<span class="n">f9_grad</span> <span class="o">=</span> <span class="n">grad</span><span class="p">(</span><span class="n">f9</span><span class="p">)</span>
|
||||
|
||||
<span class="n">x</span> <span class="o">=</span> <span class="mf">8.4</span>
|
||||
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">1.0</span><span class="p">,</span><span class="mf">0.0</span><span class="p">])</span>
|
||||
|
||||
<span class="nb">print</span><span class="p">(</span><span class="s2">"The derivative of f8 is:"</span><span class="p">,</span><span class="n">f8_grad</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
|
||||
<span class="nb">print</span><span class="p">(</span><span class="s2">"The derivative of f9 is:"</span><span class="p">,</span><span class="n">f9_grad</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="cell_output docutils container">
|
||||
<div class="output traceback highlight-ipythontb notranslate"><div class="highlight"><pre><span></span><span class="gt">---------------------------------------------------------------------------</span>
|
||||
<span class="ne">TypeError</span><span class="g g-Whitespace"> </span>Traceback (most recent call last)
|
||||
<span class="nn">Input In [22],</span> in <span class="ni"><cell line: 11></span><span class="nt">()</span>
|
||||
<span class="g g-Whitespace"> </span><span class="mi">7</span> <span class="n">f8_grad</span> <span class="o">=</span> <span class="n">grad</span><span class="p">(</span><span class="n">f8</span><span class="p">)</span>
|
||||
<span class="g g-Whitespace"> </span><span class="mi">9</span> <span class="n">x</span> <span class="o">=</span> <span class="mf">8.4</span>
|
||||
<span class="ne">---> </span><span class="mi">11</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"The derivative of f8 is:"</span><span class="p">,</span><span class="n">f8_grad</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
|
||||
<span class="ne">AttributeError</span><span class="g g-Whitespace"> </span>Traceback (most recent call last)
|
||||
<span class="nn">Input In [23],</span> in <span class="ni"><cell line: 11></span><span class="nt">()</span>
|
||||
<span class="g g-Whitespace"> </span><span class="mi">7</span> <span class="n">f9_grad</span> <span class="o">=</span> <span class="n">grad</span><span class="p">(</span><span class="n">f9</span><span class="p">)</span>
|
||||
<span class="g g-Whitespace"> </span><span class="mi">9</span> <span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">1.0</span><span class="p">,</span><span class="mf">0.0</span><span class="p">])</span>
|
||||
<span class="ne">---> </span><span class="mi">11</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"The derivative of f9 is:"</span><span class="p">,</span><span class="n">f9_grad</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
|
||||
|
||||
<span class="nn">File ~/miniforge3/envs/myenv/lib/python3.9/site-packages/autograd/wrap_util.py:20,</span> in <span class="ni">unary_to_nary.<locals>.nary_operator.<locals>.nary_f</span><span class="nt">(*args, **kwargs)</span>
|
||||
<span class="g g-Whitespace"> </span><span class="mi">18</span> <span class="k">else</span><span class="p">:</span>
|
||||
@@ -2280,30 +2303,12 @@ The analytical derivative of f7 at n = 2 is: 1
|
||||
<span class="g g-Whitespace"> </span><span class="mi">14</span> <span class="n">subargs</span> <span class="o">=</span> <span class="n">subvals</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="nb">zip</span><span class="p">(</span><span class="n">argnum</span><span class="p">,</span> <span class="n">x</span><span class="p">))</span>
|
||||
<span class="ne">---> </span><span class="mi">15</span> <span class="k">return</span> <span class="n">fun</span><span class="p">(</span><span class="o">*</span><span class="n">subargs</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
||||
|
||||
<span class="nn">Input In [22],</span> in <span class="ni">f8</span><span class="nt">(x)</span>
|
||||
<span class="g g-Whitespace"> </span><span class="mi">3</span> <span class="k">def</span> <span class="nf">f8</span><span class="p">(</span><span class="n">x</span><span class="p">):</span> <span class="c1"># Assume x is an array</span>
|
||||
<span class="ne">----> </span><span class="mi">4</span> <span class="n">x</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="mi">3</span>
|
||||
<span class="g g-Whitespace"> </span><span class="mi">5</span> <span class="k">return</span> <span class="n">x</span><span class="o">*</span><span class="mi">2</span>
|
||||
<span class="nn">Input In [23],</span> in <span class="ni">f9</span><span class="nt">(a)</span>
|
||||
<span class="g g-Whitespace"> </span><span class="mi">3</span> <span class="k">def</span> <span class="nf">f9</span><span class="p">(</span><span class="n">a</span><span class="p">):</span> <span class="c1"># Assume a is an array with 2 elements</span>
|
||||
<span class="g g-Whitespace"> </span><span class="mi">4</span> <span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">1.0</span><span class="p">,</span><span class="mf">2.0</span><span class="p">])</span>
|
||||
<span class="ne">----> </span><span class="mi">5</span> <span class="k">return</span> <span class="n">a</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
|
||||
|
||||
<span class="ne">TypeError</span>: 'ArrayBox' object does not support item assignment
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p>Here, Autograd tells us that an ‘ArrayBox’ does not support item assignment. The item assignment is done when the program tries to assign x[2] to the value 3. However, Autograd has implemented the computation of the derivative such that this assignment is not possible.</p>
|
||||
<div class="cell docutils container">
|
||||
<div class="cell_input docutils container">
|
||||
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">autograd.numpy</span> <span class="k">as</span> <span class="nn">np</span>
|
||||
<span class="kn">from</span> <span class="nn">autograd</span> <span class="kn">import</span> <span class="n">grad</span>
|
||||
<span class="k">def</span> <span class="nf">f9</span><span class="p">(</span><span class="n">a</span><span class="p">):</span> <span class="c1"># Assume a is an array with 2 elements</span>
|
||||
<span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">1.0</span><span class="p">,</span><span class="mf">2.0</span><span class="p">])</span>
|
||||
<span class="k">return</span> <span class="n">a</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
|
||||
|
||||
<span class="n">f9_grad</span> <span class="o">=</span> <span class="n">grad</span><span class="p">(</span><span class="n">f9</span><span class="p">)</span>
|
||||
|
||||
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">1.0</span><span class="p">,</span><span class="mf">0.0</span><span class="p">])</span>
|
||||
|
||||
<span class="nb">print</span><span class="p">(</span><span class="s2">"The derivative of f9 is:"</span><span class="p">,</span><span class="n">f9_grad</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
|
||||
<span class="ne">AttributeError</span>: 'ArrayBox' object has no attribute 'dot'
|
||||
</pre></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -625,7 +625,7 @@ import os
|
||||
# Where to save the figures and data files
|
||||
PROJECT_ROOT_DIR = "Results"
|
||||
FIGURE_ID = "Results/FigureFiles"
|
||||
DATA_ID = "datafiles/"
|
||||
DATA_ID = "DataFiles/"
|
||||
|
||||
if not os.path.exists(PROJECT_ROOT_DIR):
|
||||
os.mkdir(PROJECT_ROOT_DIR)
|
||||
@@ -1089,7 +1089,7 @@ import os
|
||||
# Where to save the figures and data files
|
||||
PROJECT_ROOT_DIR = "Results"
|
||||
FIGURE_ID = "Results/FigureFiles"
|
||||
DATA_ID = "datafiles/"
|
||||
DATA_ID = "DataFiles/"
|
||||
|
||||
if not os.path.exists(PROJECT_ROOT_DIR):
|
||||
os.mkdir(PROJECT_ROOT_DIR)
|
||||
@@ -1579,7 +1579,7 @@ from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error
|
||||
# Where to save the figures and data files
|
||||
PROJECT_ROOT_DIR = "Results"
|
||||
FIGURE_ID = "Results/FigureFiles"
|
||||
DATA_ID = "datafiles/"
|
||||
DATA_ID = "DataFiles/"
|
||||
|
||||
if not os.path.exists(PROJECT_ROOT_DIR):
|
||||
os.mkdir(PROJECT_ROOT_DIR)
|
||||
@@ -1745,7 +1745,7 @@ from sklearn.model_selection import train_test_split
|
||||
# Where to save the figures and data files
|
||||
PROJECT_ROOT_DIR = "Results"
|
||||
FIGURE_ID = "Results/FigureFiles"
|
||||
DATA_ID = "datafiles/"
|
||||
DATA_ID = "DataFiles/"
|
||||
|
||||
if not os.path.exists(PROJECT_ROOT_DIR):
|
||||
os.mkdir(PROJECT_ROOT_DIR)
|
||||
|
||||
|
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 13 KiB |
|
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 18 KiB |
|
Before Width: | Height: | Size: 25 KiB After Width: | Height: | Size: 26 KiB |
|
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 18 KiB |
@@ -269,20 +269,16 @@
|
||||
# domain of $f$, i.e the subset of $R^n$ where $f$ is defined. For more
|
||||
# details and proofs we refer to: [S. Boyd and L. Vandenberghe. Convex Optimization. Cambridge University Press](http://stanford.edu/boyd/cvxbook/, 2004).
|
||||
#
|
||||
# **First order condition.**
|
||||
#
|
||||
# Suppose $f$ is differentiable (i.e $\nabla f(x)$ is well defined for
|
||||
# **First order condition**: Suppose $f$ is differentiable (i.e $\nabla f(x)$ is well defined for
|
||||
# all $x$ in the domain of $f$). Then $f$ is convex if and only if $D_f$
|
||||
# is a convex set and $$f(y) \geq f(x) + \nabla f(x)^T (y-x) $$ holds
|
||||
# is a convex set and $f(y) \geq f(x) + \nabla f(x)^T (y-x)$ holds
|
||||
# for all $x,y \in D_f$. This condition means that for a convex function
|
||||
# the first order Taylor expansion (right hand side above) at any point
|
||||
# is a global under estimator of the function. To convince yourself you can
|
||||
# make a drawing of $f(x) = x^2+1$ and draw the tangent line to $f(x)$ and
|
||||
# note that it is always below the graph.
|
||||
# note that it is always below the graph.
|
||||
#
|
||||
# **Second order condition.**
|
||||
#
|
||||
# Assume that $f$ is twice
|
||||
# **Second order condition**: Assume that $f$ is twice
|
||||
# differentiable, i.e the Hessian matrix exists at each point in
|
||||
# $D_f$. Then $f$ is convex if and only if $D_f$ is a convex set and its
|
||||
# Hessian is positive semi-definite for all $x\in D_f$. For a
|
||||
@@ -1689,6 +1685,7 @@ print("The analytical derivative of f7 at n = %d is: %g"%(n,f7_grad_analytical))
|
||||
# In[22]:
|
||||
|
||||
|
||||
"""
|
||||
import autograd.numpy as np
|
||||
from autograd import grad
|
||||
def f8(x): # Assume x is an array
|
||||
@@ -1700,6 +1697,7 @@ f8_grad = grad(f8)
|
||||
x = 8.4
|
||||
|
||||
print("The derivative of f8 is:",f8_grad(x))
|
||||
"""
|
||||
|
||||
|
||||
# Here, Autograd tells us that an 'ArrayBox' does not support item assignment. The item assignment is done when the program tries to assign x[2] to the value 3. However, Autograd has implemented the computation of the derivative such that this assignment is not possible.
|
||||
|
||||
|
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 23 KiB |
|
Before Width: | Height: | Size: 21 KiB After Width: | Height: | Size: 22 KiB |
|
Before Width: | Height: | Size: 21 KiB After Width: | Height: | Size: 22 KiB |