一个向量的范数可以告诉我吗一个向量有多大。这里考虑的大小(size)概念不涉及维度,而是分量的大小。
L
2
范
数
的
计
算
L_2范数的计算
L2范数的计算
里面的元素的平方和再开根号
x=torch.tensor([-3.0,4.0]) print(torch.norm(x))
tensor(5.)
L 1 范 数 的 计 算 L_1范数的计算 L1范数的计算
表示为向量元素的绝对值之和
print(torch.abs(x).sum())
tensor(7.)
L 1 范 数 和 L 2 范 数 都 是 更 一 般 的 L p 范 数 的 特 例 L_1范数和L_2范数都是更一般的L_p范数的特例 L1范数和L2范数都是更一般的Lp范数的特例
弗罗贝尼乌斯范数(Frobenius norn)是矩阵元素的平方根,类似于L2范数。
y=torch.ones((4,9)) print(y) print(torch.norm(y)) #求弗罗贝尼乌斯范数
tensor([[1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1.]])
tensor(6.)
范数一般用于解决优化问题,最大化分配给观测数据的概念;最小化预测和真实观测之间的距离。用向量表示实际的的东西(图片,价格),求出预测值和实际值的偏离(距离).
2.4微分用微分解决问题的思想,把一个比较大(或者无法用常规方法解决)的问题无限分割,直到问题缩小到能够比较简单的解决。深度学习中,我们训练模型,不断更新他们,使他们的效果变得更好。衡量模型有多好的标准,是损失函数。
2.4.1 导数与微分关于导数的例子:
import matplotlib.pyplot as plt
import numpy as np
from IPython import display
from d2l import torch as d2l
def f(x): # 定义函数
return 3 * x ** 2 - 4 * x
def numberical_lim(f, x, h):
return (f(x + h) - f(x)) / h
h = 0.1
for i in range(5):
print(f'h={h:.5f},numberical lim={numberical_lim(f, 2, h):.5f}') # 当x=2时,计算其导数
h *= 0.1
def use_svg_display(): # 使用svg格式在调用显示绘图
display.set_matplotlib_formats('svg')
def set_figsize(figzize=(3.5, 2.5)): # 设置matplotlib图表的大小。
use_svg_display()
d2l.plt.rcParams['figure.figsize'] = figzize
def set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend): # 设置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:'), figsixe=(3.5, 2.5), axes=None):
if legend is None:
legend = []
set_figsize(figsixe)
axes = axes if axes else d2l.plt.gca()
# 如果 “X”有一个轴,则输出Ture
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)
x = np.arange(0, 3, 0.1)
plot(x, [f(x), 2 * x - 3], 'x', 'f(x)', legend=['f(x)', 'Tangent line (x=1)'])
plt.show()
2.4.2 偏导数
将导数的概念扩展到多元函数上,就有了偏导数的概念。
2.4.3 梯度我们可以连结一个多元函数对其所有变量的偏导数,以得到该函数的梯度向量。梯度在深度学习模型训练的作用是提供一个正确的方向,梯度的方向即变化率最大的方向,更够更加快的找到目标,实现模型的优化。
2.2.4 链式法则在很多时候,多元函数都是复合的,链式法则能使我们能够微分复合函数,即复合函数求导.



