Browse Source

Translate some course to Chinese

pull/1/MERGE
bushuhui 5 years ago
parent
commit
859485aa5a
8 changed files with 166 additions and 148 deletions
  1. +9
    -9
      2_knn/knn_classification.ipynb
  2. +49
    -36
      3_kmeans/k-means.ipynb
  3. +41
    -42
      4_logistic_regression/Least_squares.ipynb
  4. +19
    -22
      4_logistic_regression/Logistic_regression.ipynb
  5. +3
    -3
      5_nn/Perceptron.ipynb
  6. +11
    -11
      5_nn/mlp_bp.ipynb
  7. +7
    -7
      5_nn/softmax_ce.ipynb
  8. +27
    -18
      6_pytorch/PyTorch_quick_intro.ipynb

+ 9
- 9
2_knn/knn_classification.ipynb
File diff suppressed because it is too large
View File


+ 49
- 36
3_kmeans/k-means.ipynb
File diff suppressed because it is too large
View File


+ 41
- 42
4_logistic_regression/Least_squares.ipynb View File

@@ -4,21 +4,29 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Least squares\n",
"# 最小二乘(Generalized Least Squares)\n",
"\n",
"A mathematical procedure for finding the best-fitting curve to a given set of points by minimizing the sum of the squares of the offsets (\"the residuals\") of the points from the curve. The sum of the squares of the offsets is used instead of the offset absolute values because this allows the residuals to be treated as a continuous differentiable quantity. However, because squares of the offsets are used, outlying points can have a disproportionate effect on the fit, a property which may or may not be desirable depending on the problem at hand. \n"
"## 1. 最小二乘的基本\n",
"\n",
"最小二乘法(generalized least squares)是一种数学优化技术,它通过最小化误差的平方和找到一组数据的最佳函数匹配。 最小二乘法通常用于曲线拟合、求解模型。很多其他的优化问题也可通过最小化能量或最大化熵用最小二乘形式表达。最小二乘原理的一般形式为:\n",
"$$\n",
"L = \\sum (V_{obv} - V_{target}(\\theta))^2\n",
"$$\n",
"其中$V_{obv}$是我们观测的多组样本值,$V_{target}$是我们假设拟合函数的输出值,$\\theta$为构造模型的参数。$L$是目标函数,如果通过调整模型参数$\\theta$,使得$L$下降到最小则表明,拟合函数与观测最为接近,也就是找到了最优的模型。\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Show the data\n"
"### 1.1 示例\n",
"\n",
"假设我们有下面的一些观测数据,我们希望找到他们内在的规律。"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"outputs": [
{
@@ -59,49 +67,49 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Theory\n",
"For $N$ observation data:\n",
"### 1.2 数学原理\n",
"有$N$个观测数据为:\n",
"$$\n",
"\\mathbf{X} = \\{x_1, x_2, ..., x_N \\} \\\\\n",
"\\mathbf{Y} = \\{y_1, y_2, ..., y_N \\}\n",
"$$\n",
"其中$\\mathbf{X}$为自变量,$\\mathbf{Y}$为因变量。\n",
"\n",
"We want to find the model which can predict the data. The simplest model is linear model, which has the form of \n",
"我们希望找到一个模型能够解释这些数据,假设我们使用最简单的线性模型来拟合数据:\n",
"$$\n",
"y = ax + b\n",
"$$\n",
"那么问题就变成求解参数$a$, $b$能够使得模型输出尽可能和观测数据有比较小的误差。\n",
"\n",
"The purpose is to find parameters $a, b$ which best fit the model to the observation data. \n",
"\n",
"We use the sum of squares to measure the differences (loss function) between the model's prediction and observation data:\n",
"如何构建函数来评估模型输出与观测数据之间的误差是一个关键问题,这里我们使用观测数据与模型输出的平方和来作为评估函数(也被称为损失函数Loss function):\n",
"$$\n",
"L = \\sum_{i=1}^{N} (y_i - a x_i - b)^2\n",
"$$\n",
"\n",
"To make the loss function minimize, we can find the parameters:\n",
"使误差函数最小,那么我们就可以求出模型的参数:\n",
"$$\n",
"\\frac{\\partial L}{\\partial a} = -2 \\sum_{i=1}^{N} (y_i - a x_i - b) x_i \\\\\n",
"\\frac{\\partial L}{\\partial b} = -2 \\sum_{i=1}^{N} (y_i - a x_i - b)\n",
"$$\n",
"When the loss is minimized, therefore the partial difference is zero, then we can get:\n",
"既当偏微分为0时,误差函数为最小,因此我们可以得到:\n",
"$$\n",
"-2 \\sum_{i=1}^{N} (y_i - a x_i - b) x_i = 0 \\\\\n",
"-2 \\sum_{i=1}^{N} (y_i - a x_i - b) = 0 \\\\\n",
"$$\n",
"\n",
"We reoder the items as:\n",
"将上式调整一下顺序可以得到:\n",
"$$\n",
"a \\sum x_i^2 + b \\sum x_i = \\sum y_i x_i \\\\\n",
"a \\sum x_i + b N = \\sum y_i\n",
"$$\n",
"By solving the linear equation we can obtain the model parameters."
"通过求解二元一次方程组,我们即可求出模型的最优参数。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Program"
"### 1.3 求解程序"
]
},
{
@@ -160,14 +168,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 如何使用迭代的方法求出模型参数\n",
"## 2. 如何使用迭代的方法求出模型参数\n",
"\n",
"当数据比较多的时候,或者模型比较复杂,无法直接使用解析的方式求出模型参数。因此更为常用的方式是,通过迭代的方式逐步逼近模型的参数。\n",
"\n",
"### 梯度下降法\n",
"### 2.1 梯度下降法\n",
"在机器学习算法中,对于很多监督学习模型,需要对原始的模型构建损失函数,接下来便是通过优化算法对损失函数进行优化,以便寻找到最优的参数。在求解机器学习参数的优化算法中,使用较多的是基于梯度下降的优化算法(Gradient Descent, GD)。\n",
"\n",
"梯度下降法有很多优点,其中,在梯度下降法的求解过程中只需求解损失函数的一阶导数,计算的代价比较小,这使得梯度下降法能在很多大规模数据集上得到应用。梯度下降法的含义是通过当前点的梯度方向寻找到新的迭代点。\n",
"梯度下降法有很多优点,其中最主要的优点是,在梯度下降法的求解过程中只需求解损失函数的一阶导数,计算的代价比较小,这使得梯度下降法能在很多大规模数据集上得到应用。梯度下降法的含义是通过当前点的梯度方向寻找到新的迭代点。\n",
"\n",
"梯度下降法的基本思想可以类比为一个下山的过程。假设这样一个场景:一个人被困在山上,需要从山上下来(i.e. 找到山的最低点,也就是山谷)。但此时山上的浓雾很大,导致可视度很低。因此,下山的路径就无法确定,他必须利用自己周围的信息去找到下山的路径。这个时候,他就可以利用梯度下降算法来帮助自己下山。具体来说就是,以他当前的所处的位置为基准,寻找这个位置最陡峭的地方,然后朝着山的高度下降的地方走,同理,如果我们的目标是上山,也就是爬到山顶,那么此时应该是朝着最陡峭的方向往上走。然后每走一段距离,都反复采用同一个方法,最后就能成功的抵达山谷。\n",
"\n",
@@ -190,16 +198,16 @@
"$$\n",
"其中$\\theta$代表了模型中的参数,例如$a$, $b$\n",
"\n",
"此公式的意义是:L是关于$\\theta$的一个函数,我们当前所处的位置为$\\theta_0$点,要从这个点走到L的最小值点,也就是山底。首先我们先确定前进的方向,也就是梯度的反向,然后走一段距离的步长,也就是$\\alpha$,走完这个段步长,就到达了$\\theta_1$这个点!\n",
"此公式的意义是:$L$是关于$\\theta$的一个函数,我们当前所处的位置为$\\theta_0$点,要从这个点走到L的最小值点,也就是山底。首先我们先确定前进的方向,也就是梯度的反向,然后走一段距离的步长,也就是$\\alpha$,走完这个段步长,就到达了$\\theta_1$这个点!\n",
"\n",
"下面就这个公式的几个常见的疑问:\n",
"\n",
"* **$\\alpha$是什么含义?**\n",
"$\\alpha$在梯度下降算法中被称作为学习率或者步长,意味着我们可以通过$\\alpha$来控制每一步走的距离,以保证不要步子跨的太大扯着蛋,哈哈,其实就是不要走太快,错过了最低点。同时也要保证不要走的太慢,导致太阳下山了,还没有走到山下。所以$\\alpha$的选择在梯度下降法中往往是很重要的!$\\alpha$不能太大也不能太小,太小的话,可能导致迟迟走不到最低点,太大的话,会导致错过最低点!\n",
"$\\alpha$在梯度下降算法中被称作为学习率或者步长,意味着我们可以通过$\\alpha$来控制每一步走的距离,以保证不要步子跨的太大,错过了最低点。同时也要保证不要走的太慢,导致太阳下山了,还没有走到山下。所以$\\alpha$的选择在梯度下降法中往往是很重要的\n",
"![gd_stepsize](images/gd_stepsize.png)\n",
"\n",
"* **为什么要梯度要乘以一个负号?**\n",
"梯度前加一个负号,就意味着朝着梯度相反的方向前进!我们在前文提到,梯度的方向实际就是函数在此点上升最快的方向而我们需要朝着下降最快的方向走,自然就是负的梯度的方向,所以此处需要加上负号\n",
"梯度前加一个负号,就意味着朝着梯度相反的方向前进!梯度的方向实际就是函数在此点上升最快的方向而我们需要朝着下降最快的方向走,自然就是负的梯度的方向,所以此处需要加上负号\n",
"\n"
]
},
@@ -207,7 +215,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Program"
"### 2.2 示例代码"
]
},
{
@@ -3294,7 +3302,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## How to show the iterative process"
"## 3. 如何可视化迭代过程"
]
},
{
@@ -4136,9 +4144,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## How to use batch update method?\n",
"## 4. 如何使用批次更新的方法?\n",
"\n",
"If some data is outliear, then only use one data can make the learning inaccuracy and slow.\n",
"如果有一些数据包含比较大的错误(异常数据),因此每次更新仅仅使用一个数据会导致不精确,同时每次仅仅使用一个数据来计算更新也导致计算效率比较低。\n",
"\n",
"\n",
"* [梯度下降方法的几种形式](https://blog.csdn.net/u010402786/article/details/51188876)"
@@ -4148,14 +4156,15 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## How to fit polynomial function?\n",
"## 5. 如何拟合多项式函数?\n",
"\n",
"If we observe a missle at some time, then how to estimate the trajectory? Acoording the physical theory, the trajectory can be formulated as:\n",
"需要设计一个弹道导弹防御系统,通过观测导弹的飞行路径,预测未来导弹的飞行轨迹,从而完成摧毁的任务。按照物理学,可以得知模型为:\n",
"$$\n",
"y = at^2 + bt + c\n",
"$$\n",
"The we need at least three data to compute the parameters $a, b, c$.\n",
"我们需要求解三个模型参数$a, b, c$。\n",
"\n",
"损失函数的定义为:\n",
"$$\n",
"L = \\sum_{i=1}^N (y_i - at^2 - bt - c)^2\n",
"$$\n"
@@ -4198,7 +4207,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### How to get the update items?\n",
"### 5.1 如何得到更新项?\n",
"\n",
"$$\n",
"L = \\sum_{i=1}^N (y_i - at^2 - bt - c)^2\n",
@@ -4215,8 +4224,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## How to use sklearn to solve linear problem?\n",
"\n"
"## 6. 如何使用sklearn求解线性问题?\n"
]
},
{
@@ -4274,7 +4282,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## How to use sklearn to fit polynomial function?"
"## 7. 如何使用sklearn拟合多项式函数?"
]
},
{
@@ -4318,7 +4326,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## How to estimate some missing value by the model?\n"
"## 8. 如何通过模型来估计缺失的值?\n"
]
},
{
@@ -4405,15 +4413,6 @@
"plt.legend()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"lines_to_next_cell": 2
},
"outputs": [],
"source": []
}
],
"metadata": {


+ 19
- 22
4_logistic_regression/Logistic_regression.ipynb View File

@@ -4,12 +4,12 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Logistic Regression\n",
"# 逻辑回归 Logistic Regression\n",
"\n",
"逻辑回归(Logistic Regression, LR)模型其实仅在线性回归的基础上,套用了一个逻辑函数,但也就由于这个逻辑函数,使得逻辑回归模型成为了机器学习领域一颗耀眼的明星,更是计算广告学的核心。本节主要详述逻辑回归模型的基础。\n",
"\n",
"\n",
"## 1 逻辑回归模型\n",
"## 1. 逻辑回归模型\n",
"回归是一种比较容易理解的模型,就相当于$y=f(x)$,表明自变量$x$与因变量$y$的关系。最常见问题有如医生治病时的望、闻、问、切,之后判定病人是否生病或生了什么病,其中的望闻问切就是获取自变量$x$,即特征数据,判断是否生病就相当于获取因变量$y$,即预测分类。\n",
"\n",
"最简单的回归是线性回归,在此借用Andrew NG的讲义,有如图所示,$X$为数据点——肿瘤的大小,$Y$为观测值——是否是恶性肿瘤。通过构建线性回归模型,如$h_\\theta(x)$所示,构建线性回归模型后,即可以根据肿瘤大小,预测是否为恶性肿瘤$h_\\theta(x)) \\ge 0.5$为恶性,$h_\\theta(x) \\lt 0.5$为良性。\n",
@@ -59,7 +59,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### 逻辑回归表达式\n",
"### 1.1 逻辑回归表达式\n",
"\n",
"这个函数称为Logistic函数(logistic function),也称为Sigmoid函数(sigmoid function)。函数公式如下:\n",
"\n",
@@ -125,7 +125,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### 逻辑回归的软分类\n",
"### 1.2 逻辑回归的软分类\n",
"\n",
"现在我们将y的取值$h_\\theta(x)$通过Logistic函数归一化到(0,1)间,$y$的取值有特殊的含义,它表示结果取1的概率,因此对于输入$x$分类结果为类别1和类别0的概率分别为:\n",
"\n",
@@ -146,7 +146,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### 梯度上升\n",
"### 1.3 梯度上升\n",
"\n",
"得到了逻辑回归的表达式,下一步跟线性回归类似,构建似然函数,然后最大似然估计,最终推导出$\\theta$的迭代更新表达式。只不过这里用的不是梯度下降,而是梯度上升,因为这里是最大化似然函数。\n",
"\n",
@@ -175,7 +175,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Program"
"### 1.4 示例程序"
]
},
{
@@ -350,7 +350,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## How to use sklearn to resolve the problem\n"
"## 2. 如何使用sklearn求解逻辑回归"
]
},
{
@@ -424,14 +424,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Multi-class recognition"
"## 3. 多类识别问题"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Load & show the data"
"### 3.1 加载显示数据"
]
},
{
@@ -474,11 +474,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Visualizing the Data\n",
"### 3.2 可视化特征\n",
"\n",
"A good first-step for many problems is to visualize the data using one of the Dimensionality Reduction techniques we saw earlier. We'll start with the most straightforward one, Principal Component Analysis (PCA).\n",
"针对机器学习的问题,一个比较好的方法是通过降维的方法将原始的高维特征降到2-3维并可视化处理,通过这样的方法可以对所要处理的数据有一个初步的认识。这里介绍最简单的降维方法主成分分析(Principal Component Analysis, PCA).\n",
"\n",
"PCA seeks orthogonal linear combinations of the features which show the greatest variance, and as such, can help give you a good idea of the structure of the data set. Here we'll use RandomizedPCA, because it's faster for large N."
"PCA寻求具有最大方差的特征的正交线性组合,因此可以更好地了解数据的结构。在这里,我们将使用Randomized PCA,因为当数据个数$N$比较大是,计算的效率更好。\n"
]
},
{
@@ -522,12 +522,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"A weakness of PCA is that it produces a linear dimensionality reduction:\n",
"this may miss some interesting relationships in the data. If we want to\n",
"see a nonlinear mapping of the data, we can use one of the several\n",
"methods in the `manifold` module. Here we'll use [Isomap](https://blog.csdn.net/VictoriaW/article/details/78497316) (a concatenation\n",
"of Isometric Mapping) which is a manifold learning method based on\n",
"graph theory:"
"PCA的一个缺点是它可能会丢失数据中一些有趣的相互关系。如果想看到非线性的降维与映射\n",
"我们可以使用几种流形模块中的方法。在这里,我们将使用[Isomap](https://blog.csdn.net/VictoriaW/article/details/78497316)(串联\n",
"等距映射)是一种基于图论的流形降维方法。"
]
},
{
@@ -571,7 +568,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Program"
"### 3.3 示例程序"
]
},
{
@@ -665,10 +662,10 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise - How to draw mis-classfied data?\n",
"## 4. 练习 - 如何画出错误分类的数据?\n",
"\n",
"1. How to obtain the mis-classified index?\n",
"2. How to draw them?"
"1. 如何得到错误分类数据的下标?\n",
"2. 如何根据下标,将这些错误的数据可视化出来?"
]
},
{


+ 3
- 3
5_nn/Perceptron.ipynb View File

@@ -41,11 +41,11 @@
"称为感知机。其中,参数w叫做权值向量,b称为偏置。w·x表示w和x的内积。sign为符号函数,即\n",
"![sign_function](images/sign.png)\n",
"\n",
"### 几何解释 \n",
"### 1.1 几何解释 \n",
"感知机模型是线性分类模型,感知机模型的假设空间是定义在特征空间中的所有线性分类模型,即函数集合{f|f(x)=w·x+b}。线性方程 w·x+b=0对应于特征空间Rn中的一个超平面S,其中w是超平面的法向量,b是超平面的截踞。这个超平面把特征空间划分为两部分。位于两侧的点分别为正负两类。超平面S称为分离超平面,如下图:\n",
"![perceptron_geometry_def](images/perceptron_geometry_def.png)\n",
"\n",
"### 生物学类比\n",
"### 1.2 生物学类比\n",
"![perceptron_2](images/perceptron_2.PNG)\n",
"\n",
"\n"
@@ -136,7 +136,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. Program\n"
"## 4. 示例程序\n"
]
},
{


+ 11
- 11
5_nn/mlp_bp.ipynb View File

@@ -11,7 +11,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 神经元\n",
"## 1. 神经元\n",
"\n",
"神经元和感知器本质上是一样的,只不过我们说感知器的时候,它的激活函数是阶跃函数;而当我们说神经元时,激活函数往往选择为sigmoid函数或tanh函数。如下图所示:\n",
"\n",
@@ -49,7 +49,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 神经网络是啥?\n",
"## 2. 神经网络是啥?\n",
"\n",
"![nn1](images/nn1.jpeg)\n",
"\n",
@@ -67,7 +67,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 计算神经网络的输出\n",
"## 3. 计算神经网络的输出\n",
"\n",
"神经网络实际上就是一个输入向量$\\vec{x}$到输出向量$\\vec{y}$的函数,即:\n",
"\n",
@@ -103,7 +103,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 神经网络的矩阵表示\n",
"## 4. 神经网络的矩阵表示\n",
"\n",
"神经网络的计算如果用矩阵来表示会很方便(当然逼格也更高),我们先来看看隐藏层的矩阵表示。\n",
"\n",
@@ -145,7 +145,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 神经网络的训练 - 反向传播算法\n",
"## 5. 神经网络的训练 - 反向传播算法\n",
"\n",
"现在,我们需要知道一个神经网络的每个连接上的权值是如何得到的。我们可以说神经网络是一个模型,那么这些权值就是模型的参数,也就是模型要学习的东西。然而,一个神经网络的连接方式、网络的层数、每层的节点数这些参数,则不是学习出来的,而是人为事先设置的。对于这些人为设置的参数,我们称之为超参数(Hyper-Parameters)。\n",
"\n",
@@ -188,7 +188,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### 输出层权值训练\n",
"### 5.1 输出层权值训练\n",
"\n",
"![nn3](images/nn3.png)\n",
"\n",
@@ -222,7 +222,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### 隐藏层权值训练\n",
"### 5.2 隐藏层权值训练\n",
"\n",
"现在我们要推导出隐藏层的$\\frac{\\partial E_d}{\\partial net_j}$。\n",
"\n",
@@ -244,7 +244,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### 具体解释\n",
"### 5.3 具体解释\n",
"\n",
"我们假设每个训练样本为$(\\vec{x}, \\vec{t})$,其中向量$\\vec{x}$是训练样本的特征,而$\\vec{t}$是样本的目标值。\n",
"\n",
@@ -297,7 +297,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Program"
"## 6. 示例程序"
]
},
{
@@ -2511,7 +2511,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 如何使用类的方法封装多层神经网络?"
"## 7. 如何使用类的方法封装多层神经网络?"
]
},
{
@@ -4730,7 +4730,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 深入分析"
"## 8. 深入分析与问题"
]
},
{


+ 7
- 7
5_nn/softmax_ce.ipynb View File

@@ -13,7 +13,7 @@
"source": [
"softmax经常被添加在分类任务的神经网络中的输出层,神经网络的反向传播中关键的步骤就是求导,从这个过程也可以更深刻地理解反向传播的过程,还可以对梯度传播的问题有更多的思考。\n",
"\n",
"## softmax 函数\n",
"## 1. softmax 函数\n",
"\n",
"softmax(柔性最大值)函数,一般在神经网络中, softmax可以作为分类任务的输出层。其实可以认为softmax输出的是几个类别选择的概率,比如我有一个分类任务,要分为三个类,softmax函数可以根据它们相对的大小,输出三个类别选取的概率,并且概率和为1。\n",
"\n",
@@ -56,7 +56,7 @@
"$a_i$代表softmax的第$i$个输出值,右侧套用了softmax函数。\n",
"\n",
"\n",
"### 损失函数 loss function\n",
"### 1.1 损失函数 loss function\n",
"\n",
"在神经网络反向传播中,要求一个损失函数,这个损失函数其实表示的是真实值与网络的估计值的误差,知道误差了,才能知道怎样去修改网络中的权重。\n",
"\n",
@@ -74,7 +74,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 推导过程\n",
"## 2. 推导过程\n",
"\n",
"首先,我们要明确一下我们要求什么,我们要求的是我们的$loss$对于神经元输出($z_i$)的梯度,即:\n",
"\n",
@@ -90,13 +90,13 @@
"\n",
"有个人可能有疑问了,这里为什么是$a_j$而不是$a_i$,这里要看一下$softmax$的公式了,因为$softmax$公式的特性,它的分母包含了所有神经元的输出,所以,对于不等于i的其他输出里面,也包含着$z_i$,所有的$a$都要纳入到计算范围中,并且后面的计算可以看到需要分为$i = j$和$i \\ne j$两种情况求导。\n",
"\n",
"### 针对$a_j$的偏导\n",
"### 2.1 针对$a_j$的偏导\n",
"\n",
"$$\n",
"\\frac{\\partial C}{\\partial a_j} = \\frac{(\\partial -\\sum_j y_j ln a_j)}{\\partial a_j} = -\\sum_j y_j \\frac{1}{a_j}\n",
"$$\n",
"\n",
"### 针对$z_i$的偏导\n",
"### 2.2 针对$z_i$的偏导\n",
"\n",
"如果 $i=j$ :\n",
"\n",
@@ -120,7 +120,7 @@
"(\\frac{u}{v})' = \\frac{u'v - uv'}{v^2} \n",
"$$\n",
"\n",
"### 整体的推导\n",
"### 2.3 整体的推导\n",
"\n",
"\\begin{eqnarray}\n",
"\\frac{\\partial C}{\\partial z_i} & = & (-\\sum_j y_j \\frac{1}{a_j} ) \\frac{\\partial a_j}{\\partial z_i} \\\\\n",
@@ -134,7 +134,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 问题\n",
"## 3. 问题\n",
"如何将本节所讲的softmax,交叉熵代价函数应用到上节所讲的BP方法中?"
]
},


+ 27
- 18
6_pytorch/PyTorch_quick_intro.ipynb View File

@@ -18,7 +18,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Tensor\n",
"## 1. Tensor\n",
"\n",
"Tensor是PyTorch中重要的数据结构,可认为是一个高维数组。它可以是一个数(标量)、一维数组(向量)、二维数组(矩阵)以及更高维的数组。Tensor和Numpy的ndarrays类似,但Tensor可以使用GPU进行加速。Tensor的使用和Numpy及Matlab的接口十分相似,下面通过几个例子来看看Tensor的基本使用。"
]
@@ -421,9 +421,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"此处可能发现GPU运算的速度并未提升太多,这是因为x和y太小且运算也较为简单,而且将数据从内存转移到显存还需要花费额外的开销。GPU的优势需在大规模数据和复杂运算下才能体现出来。\n",
"\n",
"### Autograd: 自动微分\n",
"此处可能发现GPU运算的速度并未提升太多,这是因为x和y太小且运算也较为简单,而且将数据从内存转移到显存还需要花费额外的开销。GPU的优势需在大规模数据和复杂运算下才能体现出来。\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Autograd: 自动微分\n",
"\n",
"深度学习的算法本质上是通过反向传播求导数,而PyTorch的**`Autograd`**模块则实现了此功能。在Tensor上的所有操作,Autograd都能为它们自动提供微分,避免了手动计算导数的复杂过程。\n",
" \n",
@@ -693,7 +698,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### 神经网络\n",
"## 3. 神经网络\n",
"\n",
"Autograd实现了反向传播功能,但是直接用来写深度学习的代码在很多情况下还是稍显复杂,torch.nn是专门为神经网络设计的模块化接口。nn构建于 Autograd之上,可用来定义和运行神经网络。nn.Module是nn中最重要的类,可把它看成是一个网络的封装,包含网络各层定义以及forward方法,调用forward(input)方法,可返回前向传播的结果。下面就以最早的卷积神经网络:LeNet为例,来看看如何用`nn.Module`实现。LeNet的网络结构如图2-7所示。\n",
"\n",
@@ -701,7 +706,7 @@
"\n",
"这是一个基础的前向传播(feed-forward)网络: 接收输入,经过层层传递运算,得到输出。\n",
"\n",
"#### 定义网络\n",
"### 3.1 定义网络\n",
"\n",
"定义网络时,需要继承`nn.Module`,并实现它的forward方法,把网络中具有可学习参数的层放在构造函数`__init__`中。如果某一层(如ReLU)不具有可学习的参数,则既可以放在构造函数中,也可以不放,但建议不放在其中,而在forward中使用`nn.functional`代替。"
]
@@ -865,7 +870,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 损失函数\n",
"### 3.2 损失函数\n",
"\n",
"nn实现了神经网络中大多数的损失函数,例如nn.MSELoss用来计算均方误差,nn.CrossEntropyLoss用来计算交叉熵损失。"
]
@@ -960,7 +965,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 优化器"
"### 3.3 优化器"
]
},
{
@@ -1014,14 +1019,18 @@
"source": [
"\n",
"\n",
"#### 数据加载与预处理\n",
"### 3.4 数据加载与预处理\n",
"\n",
"在深度学习中数据加载及预处理是非常复杂繁琐的,但PyTorch提供了一些可极大简化和加快数据处理流程的工具。同时,对于常用的数据集,PyTorch也提供了封装好的接口供用户快速调用,这些数据集主要保存在torchvison中。\n",
"\n",
"`torchvision`实现了常用的图像数据加载功能,例如Imagenet、CIFAR10、MNIST等,以及常用的数据转换操作,这极大地方便了数据加载,并且代码具有可重用性。\n",
"\n",
"\n",
"### 小试牛刀:CIFAR-10分类\n",
"`torchvision`实现了常用的图像数据加载功能,例如Imagenet、CIFAR10、MNIST等,以及常用的数据转换操作,这极大地方便了数据加载,并且代码具有可重用性。\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. 小试牛刀:CIFAR-10分类\n",
"\n",
"下面我们来尝试实现对CIFAR-10数据集的分类,步骤如下: \n",
"\n",
@@ -1031,7 +1040,7 @@
"4. 训练网络并更新网络参数\n",
"5. 测试网络\n",
"\n",
"#### CIFAR-10数据加载及预处理\n",
"### 4.1 CIFAR-10数据加载及预处理\n",
"\n",
"CIFAR-10[^3]是一个常用的彩色图片数据集,它有10个类别: 'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'。每张图片都是$3\\times32\\times32$,也即3-通道彩色图片,分辨率为$32\\times32$。\n",
"\n",
@@ -1187,7 +1196,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 定义网络\n",
"### 4.2 定义网络\n",
"\n",
"拷贝上面的LeNet网络,修改self.conv1第一个参数为3通道,因CIFAR-10是3通道彩图。"
]
@@ -1242,7 +1251,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 定义损失函数和优化器(loss和optimizer)"
"### 4.3 定义损失函数和优化器(loss和optimizer)"
]
},
{
@@ -1260,7 +1269,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### 训练网络\n",
"### 4.4 训练网络\n",
"\n",
"所有网络的训练流程都是类似的,不断地执行如下流程:\n",
"\n",
@@ -1430,7 +1439,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 在GPU训练\n",
"### 4.5 在GPU训练\n",
"就像之前把Tensor从CPU转到GPU一样,模型也可以类似地从CPU转到GPU。"
]
},


Loading…
Cancel
Save