Show code
#collapse
!pip install d2lApril 25, 2021
“The fourth notebook in a series to be posted aiming to solve and understand exercises from d2l.ai curriculum on deep learning”
#collapse
def use_svg_display():
"""Use the svg format to display a plot in Jupyter."""
display.set_matplotlib_formats('svg')
def set_figsize(figsize=(3.5, 2.5)):
"""Set the figure size for matplotlib."""
use_svg_display()
d2l.plt.rcParams['figure.figsize'] = figsize
def set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend):
"""Set the axes for matplotlib."""
axes.set_xlabel(xlabel)
axes.set_ylabel(ylabel)
axes.set_xscale(xscale)
axes.set_yscale(yscale)
axes.set_xlim(xlim)
axes.set_ylim(ylim)
if legend:
axes.legend(legend)
axes.grid()
def plot(X, Y=None, xlabel=None, ylabel=None, legend=None, xlim=None,
ylim=None, xscale='linear', yscale='linear',
fmts=('-', 'm--', 'g-.', 'r:'), figsize=(3.5, 2.5), axes=None):
"""Plot data points."""
if legend is None:
legend = []
set_figsize(figsize)
axes = axes if axes else d2l.plt.gca()
# Return True if `X` (tensor or list) has 1 axis
def has_one_axis(X):
return (hasattr(X, "ndim") and X.ndim == 1 or
isinstance(X, list) and not hasattr(X[0], "__len__"))
if has_one_axis(X):
X = [X]
if Y is None:
X, Y = [[]] * len(X), X
elif has_one_axis(Y):
Y = [Y]
if len(X) != len(Y):
X = X * len(Y)
axes.cla()
for x, y, fmt in zip(X, Y, fmts):
if len(x):
axes.plot(x, y, fmt)
else:
axes.plot(y, fmt)
set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend)The derivative of \(f(x)\) here is \(f'(x) = 3x^2 + 1/x^2\) and by setting \(x=1\), we get 4, let’s try to simulate like they did.
h=0.1000000000, numerical limit=4.2190909091
h=0.0100000000, numerical limit=4.0201990099
h=0.0010000000, numerical limit=4.0020019990
h=0.0001000000, numerical limit=4.0002000200
h=0.0000100000, numerical limit=4.0000200002
h=0.0000010000, numerical limit=4.0000019997
h=0.0000001000, numerical limit=4.0000002022
h=0.0000000100, numerical limit=3.9999999868
h=0.0000000010, numerical limit=4.0000003310
h=0.0000000001, numerical limit=4.0000003310
We do see that the value approaches to 4. So \(y=4\) when \(x=1\), to find the equation of the tangent line to \(x^3−(1/x)\) at \(x=1\)
We already have the slope of the tangent line by substituting \(x=1\) on \(f'(x)\) which is \(4\), since a tangent line shares atleast one point with the original equation(\(f(x)\)), substituting the \(x\) in \(f(x)\) we get \(0\).
So the common(shared) point between \(f(x)\) and its tangent at \(x=1\) is \((1,0)\), we can use the slope \(4\) and the point \((1,0)\) to find the equation of the tangent line at \(x=1\) with the formula:
\[y - y_1 = m(x-x_1)\]
where the \(m\) is the slope and \(x_1\) and \(y_1\) are coordinates from the point which we found. Plugging in the values which we have into the equation we get the equation
\[ y = 4x - 4 \]
This is a good reference for another example of this method.
Let’s see if it is the tangent line by visualising it
For gradient we need to calculate the partial derivatives of the function given with respect to its variables. (ie) \(\frac{\partial y}{\partial x_1}\) and \(\frac{\partial y}{\partial x_2}\)
\[\frac{\partial y}{\partial x_1} = 6x_1 \]
\[\frac{\partial y}{\partial x_2} = 5e^{x_2}\]
so the gradient will be \[\nabla_xf(x) = [6x_1, 5e^{x_2}]\]
For this function, it can be split into \(\sqrt{x^Tx}\).
Let’s consider \(u = x^Tx\) then our function would become $f(x) = $ or \(u^{1/2}\)
So now we have two variables \(u\) and \(x\), the gradient of the function would be
\[\frac{\partial y} {\partial u} . \frac{\partial y} {\partial x}\]
\[\frac{\partial y} {\partial u} = (1/2)*(u)^{-1/2} = \frac{1}{2 \sqrt u} = \frac{1}{2 ||x||_2}\]
\[\frac{\partial y} {\partial x} = \frac {\partial x^Tx} {\partial x} = 2x\]
\[\nabla_xf(x) = \frac{\partial y} {\partial u} . \frac{\partial y} {\partial x}\]
\[ = \frac{1}{2 ||x||_2} . 2x = \frac {x}{||x||_2}\]
We need to treat \(u\) as a function with variables \(x\), \(y\) and \(z\) and each of \(x,y,z\) as functions with variables \(a\) and \(b\)
so by applying chain rule we get
\[ \frac{\partial u}{\partial a} = \frac{\partial u}{\partial x}\frac{\partial x}{\partial a}+\frac{\partial u}{\partial y}\frac{\partial y}{\partial a}+\frac{\partial u}{\partial z}\frac{\partial z}{\partial a}\]
\[ \frac{\partial u}{\partial a} = \frac{\partial u}{\partial x}\frac{\partial x}{\partial b} + \frac{\partial u}{\partial y}\frac{\partial y}{\partial b}+\frac{\partial u}{\partial z}\frac{\partial z}{\partial b}\]