CS231n lecture 3 Linear classification and Optimization

Linear classification

现在呢针对KNN classifier的缺点,我们提出了一个更强大的方法对于图像分类问题,这个之后会扩展到神经网络。

这个方法主要包含两部分: score function (map the raw data to class scores) 将原始数据映射为类的分数。 另一个是 loss function (quantify the agreement between the predicted scores and the ground truth labels) 量化预测的分数和实际标签之间的一致性。

然后,这个将被视为一个优化 (Optimization) 问题,我们调整 score function 的参数来使得 loss function 达到最小值。

  • linear classifier :现在举一个最简单的线性映射方程的例子:

    f(xi,W,b)=Wxi+b

    Xi 为图像像素 flatten out to a single column [D x 1],W 为 weights [K x D], b 为 bias [K x 1] (一共有K类)。原始的像素值进入方程,输出为 K 个 class scores.

    有几个点需要注意,(1)实质上,W 的每一行都是一个分类器。所以它一次性有效的平行的评估了 K 个独立的分类器。 (2) 输入的像素和标签是固定的,我们通过调整参数 W 和 b 使得分类器能够很好的和训练数据相匹配。直观上看就是,correct class has a scores that larger than the scores of incorrect class。(3) 这个方法的优点是:参数 W,b 是通过 training data 学习到的,一旦学习完成,training data 便可以废弃。新的数据可以直接使用训练好的 score function 进行分类。(4) 分类一个 test image 只需要矩阵之间的相乘和相加,比挨个和 training image 做比较速度快很多!

Foreshadowing: Convolutional Neural Networks will map image pixels to scores exactly as shown above, but the mapping ( f ) will be more complex and will contain more parameters.

  • Interpretation of linear classifiers as template matching

    对于 W 的另一种解释就是,W里的每一行都对应一个类别的模板。线性分类器是在做一个模板匹配,利用 template(this is learned) 和 图像做内积得到分数,以获得哪一个是最匹配的。另一个观点是,我们还是在做 Nearest Neighbor, 只是我们不再和成千上万的training image 做比较,而是每一类只对应一个图像(这个图像是learning得到的,并不一定是training set中的一个),使用 inner product 来替代 L1,L2 norm.

  • bias trick:

    同时追踪两个参数太烦了,于是就想把 bias vector 合并到 weight 中,在 weights 中多加一列 bias vector, 然后在image pixels 中多加一行为1. 这样就得到了简化,只有 weights 一个参数需要控制啦~

  • image data preprocessing:

    在机器学习时,我们一般都不用原始像素作为输入[0,255],而是要预先对这些像素进行预处理,it is important to center your data by subtracting the mean from every feature. 对于图像来说就是要计算出一个 mean image across training images, 然后分别减去这个 mean image. [-127,127]. 更进一步的预处理是 scale your feature to [-1,1],其中 zero mean centering 就更重要了,具体原因到梯度下降时会讲。

Loss function

loss function 直观反映除了我们是否对结果满意,如果结果很符合我们的标准,那么 loss 的结果很小,如果 we do a poor job, 那么 loss 会很大。

有很多种 loss function,下面介绍两个。

  • multiclass SVM loss

    Li=jyimax(0,sjsyi+Δ)

    SVM希望对于某个待分类的图像,正确类别分数要比错误类别分数至少高一个 margin,则认为没有损失。否则按照上面公式计算 loss。

The loss function quantifies our unhappiness with predictions on the training set.

  • regularization

    loss function 有一个bug,就是说符合条件的 W 有很多,比如一个w符合条件,那么w的整数倍都符合条件, 我们需要确定一个 W 来移除它的不确定性。我们可以用 regularization penalty R(W) 来扩展 Loss function。比较常用的有 L2 norm:

    R(W)=klW2k,l

    L2 norm that discourages large weights through an elementwise quadratic penalty over all parameters.

所以一个完整的 Multiclass SVM Loss 为:

L=1NiLidata loss+λR(W)regularization loss

N 是训练例子的数量, 这里有一个超参数 lamda, 没有简单的方法去设置它,唯有通过 cross validation.

Penalizing large weights 有一个很好的性质就是提高了通用性。避免了 ovetfitting. note 里 practical consideration 里 setting delta值得看一下,理解会更透彻。看起来 delta 和 lamda是两个不同的参数,实质上他们解决的是同一个问题,The tradeoff between the data loss and the regularization loss in the objective。 W的变化会导致 difference 的变化,这个时候 delta就会显得很无力,所以我们此时就要靠 lamda 来控制允许W增大的范围。

All we have to do now is to come up with a way to find the weights that minimize the loss.

  • softmax classifier

    除了 SVM classifier外,另一个普遍使用的就是 softmax classifer

    在softmax classifier 中 score function 并没有改变,还是

f(xi;W)=Wxi ,

但是我们对它的解释不同了,Interpret these scores as the unnormalized log probabilities for each class.

Li=log(efyijefj)

f 指的是 class scores, 完整的 Loss 还是和之前一样,对所有的值Li取平均, 并加上 regularization term. log 括号内的式子就叫做 softmax function。 在实际操作时,如果 scores 非常大,将会造成式子的不稳定,解决办法是在后面加上 logC。具体详见笔记。

SVM VS Softmax

svm vs softmax

The difference is in the interpretation of the scores in f: The SVM interprets these as class scores and its loss function encourages the correct class (class 2, in blue) to have a score higher by a margin than the other class scores. The Softmax classifier instead interprets the scores as (unnormalized) log probabilities for each class and then encourages the (normalized) log probability of the correct class to be high (equivalently the negative of it to be low). lamda 对于 Weights 的调整影响很大,Weights 的改变对于 SVM 影响不大,但是对于 softmax 的影响很大,会改变最后的概率分布。如果 weights 的值都很小,那么输出概率会接近均匀。 解释:weights 如果变小, 那么scores之间的差值将会变小,SVM只关心差值是否大于margin,而并不在乎内部scores细微的改变,而softmax并不是这样,它从不满足自己的结果,它总是想让对的class 获取更高的概率,而错误的 class 获得更低的概率。

Optimization

Optimization 的过程就是寻找参数 W 能够使得 Loss function 的值最小。

There are 3 strategies.

  • (1) A first very bad idea solution: Random search

    Our strategy will be to start with random weights and iteratively refine them over time to get lower loss.

  • (2) Random Local Search

    对于一个随机的 W, 我们产生一个 δW , 如果 loss 在 W+δW 的情况下变小了,那么我们对 W 进行更新。这种方法比第一种准确率高一些,因为第一种是在任意方向尝试,但是还是计算量很大,很浪费时间。

  • (3) Following the Gradient

    前面两种方法都是选择任意的方向然后尝试 W 看是否会减小 loss。 现在呢我们可以通过计算 Loss Function 的 gradient 来知道往哪个方向下降是最合适的,是下降最深的。一个比喻就是,这个方法能够感知到我们脚下,山的坡度,然后朝着最陡的方向向下走。 The gradient is simply the vector of partial derivatives in each dimension. 梯度指的是:每一维的偏导数组成的向量。

计算梯度

here are two ways to compute the gradient: A slow, approximate but easy way (numerical gradient), and a fast, exact but more error-prone way that requires calculus (analytic gradient).

说一下各自的优缺点。

  • 第一种数值梯度:(1)这种计算方法的复杂度是随着参数的增加而增加的,比如我们有30730个参数,那么我们就要对 loss function 评估 30730次,使得其中的一个参数更新。当神经网络特别复杂时,这个问题就会尤为严重。(计算量太大) (2) 这种方法虽然简单,但是它并不精确,只是一个估值,因为真正的梯度定义指的是 h 趋近于零的极限值。我们在计算时只是选取一个很小的h。

  • The second way to compute the gradient is analytically using Calculus, which allows us to derive a direct formula for the gradient (no approximations) that is also very fast to compute. However, unlike the numerical gradient it can be more error prone to implement, which is why in practice it is very common to compute the analytic gradient and compare it to the numerical gradient to check the correctness of your implementation.

梯度下降

Now that we can compute the gradient of the loss function, the procedure of repeatedly evaluating the gradient and then performing a parameter update is called Gradient Descent.

  • Mini-batch gradient descent

    如果对于训练集中的每一个图像都计算梯度的话太浪费计算资源了,于是我们只取其中的一部分用来调整w。原因是其中大部分都是重复的。For example, in current state of the art ConvNets, a typical batch contains 256 examples from the entire training set of 1.2 million. This batch is then used to perform a parameter update
    minibatch 的大小是一个超参数,但是很少用 cross validation的方法。It is usually based on memory constraints (if any), or set to some value, e.g. 32, 64 or 128. We use powers of 2 in practice because many vectorized operation implementations work faster when their inputs are sized in powers of 2.

具体细节和代码见官方笔记。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值