机器学习算法、无监督学习与概率

Machine Learning Algorithms, Unsupervised Learning, and Probability

齐科·科尔特 Zico Kolter · 机器学习公开课 · 2014-07-09 · 约 62 分钟 · 原视频 ↗

打开互动全文版(中英对照 + 朗读 + 问答)→

本期速览 · Overview

对多种机器学习算法、无监督学习以及概率在假设类、损失函数和优化框架中作用的高层次概述。

A high-level overview of various machine learning algorithms, unsupervised learning, and the role of probability in the hypothesis class, loss function, and optimization framework.

要点 · TL;DR

核心观点 · Key points

反共识 · Contrarian takes

本期章节 · Chapters(共 21)

全文 · Full transcript(中英对照)

0. 引言与概述 Introduction and Overview

Host

好的,大家好,欢迎回到上午的课程。考虑到现在才早上 9 点,我估计大家会陆续进来。这节课我将讨论一系列主题,这些主题建立在上一节课的基础上,但也会讲得比较快、比较宏观。具体来说,我会讲到:昨天我们讨论过,你们也在后续课程中看到了一系列不同的机器学习算法。我讲了支持向量机、线性回归和逻辑回归。我给出了损失函数,并更明确地写出了这些算法的公式。今天,我想重点介绍你们在学习机器学习过程中可能会遇到的其他几种算法。这次讲解会更加宏观;其中一些算法会在后续课程中深入讲解,有些则不会,但它们都很常见,所以你们应该了解它们是什么。目的是强调许多不同的算法都可以归结为假设类、损失函数和优化算法这个相同的框架。然后我会非常简要地讨论另一种学习方式,叫做无监督学习。今天,我相信还会有一节关于强化学习的课,这是另一种机器学习方法,但无监督学习也很常见,所以我们会讨论几种解释它的方式以及几种不同的算法。最后,我会讲到概率。到目前为止,我很少涉及概率;我几乎没提过。你们可能在后续课程中看到了一点,但我想至少强调一下概率如何融入假设类、损失函数和优化算法这个框架,因为这两者之间有着非常紧密的联系。至少从高层次理解概率如何进入这个方程是很好的,当然概率模型是一个更大的主题,有专门的课程,所以这里只是简单介绍。最后,我会用两张幻灯片讲机器学习算法的评估,主要借鉴 Alex 说过的一些内容:人们在评估方法时经常会犯一些错误,我想强调其中几点,不过我觉得我在这里要讲的内容现在越来越少见——人们可能犯的错误。

All right, hello everyone, welcome back to the morning session. I assume people will kind of filter in as we go, given that it is only 9 a.m. In this lecture, I'm going to talk about a variety of topics that build upon the lecture from last time, but which are also going to be fairly fast and fairly high level. In particular, I'm going to cover: we talked yesterday and you saw in later lectures about a bunch of different machine learning algorithms. I talked about support vector machines, linear regression, and logistic regression. I gave the loss function and wrote down the formulation more explicitly for these algorithms. Today, what I want to do is just highlight a few other algorithms that you may run into as you study machine learning. This is going to be even higher level; some of them will be covered in great depth by further lectures, some will not, but they are quite common, so you should know what these are. The goal is to highlight that many different algorithms can be cast in the same framework of hypothesis class, loss function, and optimization algorithm. I'll then talk very briefly about another kind of learning called unsupervised learning. Today, I believe there will also be a lecture on reinforcement learning, which is yet another type of machine learning approach, but unsupervised learning is quite common, so we'll talk about a few ways of interpreting it and a few different algorithms. Finally, I'll get to probability. So far, I've really been light on probability; I haven't mentioned it at all. You've seen a little bit maybe in subsequent lectures, but I want to at least highlight how probability can enter into this framework of hypothesis class, loss function, and optimization algorithm, because there are very close and tight-knit connections between these two. It's good to at least understand at a high level how probability can enter into the equation here, though of course probabilistic models are a much bigger topic and there are entire courses on these things, so this will be a very light treatment. Finally, I'll have two slides on evaluating machine learning algorithms, mainly taking some similar to what Alex said: there are certain things that people do wrong when it comes to evaluating methods, and I want to highlight a couple of these, though I think what I'm going to talk about here I see less and less these days—the mistakes people can make.

1. 框架回顾:假设类、损失函数、优化 Framework Recap: Hypothesis Class, Loss Function, Optimization

Host

那么,我们先从其他几种机器学习算法开始。还记得我们昨天的框架吗?一个机器学习算法有三个组成部分。首先,假设类,我们称之为 H_theta。其次,损失函数,我们称之为 L,这取决于我们讨论的是回归问题还是分类问题,但回归和分类都有不同的可能损失函数,这会导致不同的机器学习算法。最后,还有一个优化方法。我昨天讲到的主要是,对于大多数问题,梯度下降类算法,你们以后会看到更多——梯度方法和随机梯度方法。我还讲了最小二乘法的精确解。周五的课上我会更详细地讲优化。但现在,我只想强调一点:这些不同的机器学习算法都可以在这个框架中看待,只要考虑到它们也可以使用不同的优化算法。

So let's start with a couple of other machine learning algorithms. Remember the framework we had yesterday: there were three ingredients to a machine learning algorithm. First, the hypothesis class, which we called H_theta. Second, the loss function, which we called L, and this was different based on whether we were talking about a regression problem or a classification problem, but there were also different possible loss functions for regression and different possible loss functions for classification, which lead to different algorithms in machine learning. And then finally, there was an optimization method. The main ones I talked about yesterday were, for most problems, gradient descent type algorithms, and you'll see a lot more of that going forward—gradient type methods and stochastic gradient methods. I also talked about exact solutions for least squares. I'll talk more about optimization in general on Friday's lecture. But for now, I just want to highlight the fact that these algorithms—different machine learning algorithms—can be viewed in this framework, as long as you consider that they can also use different optimization algorithms.

2. 核方法概述 Kernel Methods Overview

Host

那么,我们直接开始。这里有一系列机器学习算法类别。有些你们听说过,有些可能没听说过,但它们都很适合这个框架。第一个是非常广泛的类别,叫做核方法。下周你们会听到更多关于核方法的内容;Andrew Wilson 会讲两节课。虽然这里是非常简化的处理,而且这些方法有更深的层次,但从某种意义上说,核方法可以看作是一种线性预测器。它们实际上非常相似。具体来说,对于核方法,假设类如下:它是一个线性函数,很像我们的线性回归情况或任何线性假设类:从 i=1 到 m 的 theta_i 乘以函数 k(x, x_i)。这里需要注意几点。首先,这个函数 k 被称为核函数,它直观地评估 x 与 x_i 之间的相似度——这两个参数有多相似;值越大表示越相似。需要注意:首先,这个假设类在参数 theta 上仍然是线性的,所以你可以使用几乎相同的损失函数和优化方法。这些实际上用于如何优化这些东西,你可以使用与线性分类器或线性回归方法相同的各种损失函数和优化方法。这导致了针对不同损失函数的不同算法类别。几个要点:首先,我在这里把很多复杂性包裹在这个 k 函数里,这实际上是对核的一个非常不精确和高层次的介绍。你们会看到更多。有很多方法可以解释这个核函数到底在做什么,包括将数据嵌入到更高维度,使用非常高维的输入向量,而无需显式构造这些向量——这是一种解释。另一种解释是更理论化的内积空间。但你们会详细了解。另一件要注意的事情:与之前不同,之前我们的输入向量是一个 n 维向量,我们有 m 个不同的样本,这里我实际上是从 i=1 到 m 求和,所以我必须计算我的输入 x 与训练集中所有样本之间的这个核函数。这里的 i 求和不仅索引参数,还索引训练集中的所有样本。这意味着两件事。

So let's just jump right in. Here are a bunch of classes of machine learning algorithms. Some you have heard of, some you might not have heard of, but they all fall very nicely into this framework. The first one is a very broad class called kernel methods. You'll hear a lot more about kernel methods next week; Andrew Wilson is going to be giving two lectures on these. While this is a very simplified treatment here, and there's much more depth to these, in some sense kernel methods can be thought of like a linear predictor. They're actually very similar. In particular, for a kernel method, the hypothesis class is the following: it's a linear function, much like our linear regression case or any linear hypothesis class: sum from i=1 to m of theta_i times this function k of x and x_i. A couple of things to note here. First, this function k is called a kernel function, and it essentially, intuitively, evaluates some kind of similarity between how similar x is to x_i—how similar these two arguments are; bigger means more similar. A couple of things to note: first, this hypothesis class is still linear in the parameters theta, so you can still use almost all the same loss functions and optimization methods. These are actually used for how you optimize these things, and you can use all the same different sorts of loss functions and optimization methods as we were doing with, say, a linear classifier or a linear regression method. That leads to a different class of algorithms for these different loss functions. A couple of take-home points: first, I'm wrapping a lot of complexity around this k function here, and really this is a very inexact and high-level treatment of kernels. You'll see much more. There are a lot of ways to interpret exactly what this kernel function is doing, including embedding the data into a higher dimension with very high-dimensional input vectors without ever actually constructing these things explicitly—that's one interpretation. Another interpretation is a more theoretical one about inner product spaces. But you'll get that in detail. The other thing to notice: unlike before, where our input vector was a vector with n dimensions and we have m different examples, here I'm actually taking a sum from i=1 to m, so I have to compute this kernel function between my input x and all the examples in my training set. The sum over i here is indexing not just over the parameters but also over all the examples in the training set. This means two things.

3. 核方法与正则化 Kernel Methods and Regularization

Zico

首先,昨天有一些关于参数数量和样本数量相对大小的讨论。这里它们实际上是相同的:我们有与样本数量相同的参数 theta,所以 M 等于 N。这意味着过拟合等问题不能敷衍了事;你必须处理它们,通常通过正则化。在损失函数中加入正则化会导致我们通常所说的核方法。

First of all, there was some discussion yesterday about the relative size of the parameters and the number of examples. Here they are actually the same: we have the same number of parameters theta as examples, so M equals N. This means issues like overfitting cannot be swept under the rug; you have to deal with them, typically via regularization. Adding regularization to the loss function leads to what we think of as kernel methods.

Zico

第二点是,为了计算对新样本的假设或预测,我必须计算该样本与所有训练数据的某个函数。这意味着我必须保留所有训练数据。没有优化的话,每次预测都要遍历所有数据。对于大问题,这变得非常低效。使核方法可扩展的激动人心的工作涉及近似计算,通常利用核函数的其他解释。通过这样做,你可以在不保留所有数据的情况下进行紧密近似,但朴素的核方法需要存储所有数据。

The second point is that to compute my hypothesis or prediction on a new example, I have to compute some function of that example and all my training data. This means I have to keep all my training data around. Without optimizations, every time I want to make a prediction, I do a complete pass over all my data. For big problems, this becomes very inefficient. Exciting work on making kernels scalable involves approximating this, often exploiting other interpretations of the kernel function. By doing so, you can approximate closely without keeping all data, but naive kernel methods require storing all data.

4. 最近邻方法 Nearest Neighbor Methods

Zico

另一类方法是最近邻方法。它们非常简单:要进行预测,你在数据集中寻找最相似的样本,并预测相同的标签,无论是回归还是分类。这里,H_theta(X) 等于 y_i,我们选择最接近输入点的样本。我们最小化 X 和 X_i 之间的平方二范数距离。这是衡量两个向量相距多远的度量。我们也可以做 k 近邻,取 k 个最近邻的平均值。

Another class of methods is nearest neighbor methods. They are very simple: to make a prediction, you look for the most similar example in your dataset and predict the same label, for both regression or classification. Here, H_theta of X equals y_i, where we pick the example closest to our input point. We minimize the squared 2-norm distance between X and X_i. This is a measure of how far apart two vectors are. We can also do k-nearest neighbor, taking the average of the k closest ones.

Zico

注意,最近邻方法不需要真正的训练;没有需要拟合的参数。与核方法一样,这意味着我们必须保留所有数据来进行预测,因为我们需要搜索所有数据以找到最近邻。这些被称为非参数方法。非参数并不意味着没有参数;它意味着参数数量随数据规模增长。最近邻是一个经典的非参数例子,因为实际上没有参数。

Note that nearest neighbor methods require no real training; there are no parameters to fit. Like kernel methods, this means we have to keep all data around to make predictions, because we need to search through all data to find the closest one. These are called nonparametric methods. Nonparametric does not mean no parameters; it means the number of parameters grows with the size of the data. Nearest neighbor is a classic nonparametric example because there are essentially no parameters.

5. 神经网络 Neural Networks

Zico

神经网络是具有非线性激活函数(如 sigmoid)的非线性分类器。最终输出通过组合许多这样的非线性函数得到。损失函数通常是输出和输入之间的平方误差,优化使用梯度下降或随机梯度下降。与其他方法不同,不能保证找到最优参数集,但通常可以找到好的参数。神经网络近年来取得了重大成功,是一个非常令人兴奋的话题。

Neural networks are nonlinear classifiers with nonlinear activation functions like sigmoid. The final output is obtained by composing many such nonlinear functions. The loss function is usually squared error between output and input, and optimization uses gradient descent or stochastic gradient descent. Unlike other methods, there is no guarantee of finding the optimal set of parameters, but typically good ones can be found. Neural networks have seen major successes in recent years and are a very exciting topic.

6. 决策树 Decision Trees

Zico

另一种分类器是决策树,这是最早获得关注的机器学习算法之一。它们通过查看单个变量并对其进行分支来将输入空间划分为区域。例如,对于二维输入的二元分类,树首先检查第二个坐标是否大于 2。如果是,预测 -1。如果不是,检查第一个坐标是否大于 -3。如果是,预测 +1;如果不是,预测 -1。

Another type of classifier is decision trees, one of the first machine learning algorithms to gain traction. They partition the input space into regions by looking at a single variable and branching on it. For example, with a two-dimensional input for binary classification, the tree first checks if the second coordinate is greater than 2. If yes, predict -1. If not, check if the first coordinate is greater than -3. If yes, predict +1; if not, predict -1.

7. 决策树与假设类 Decision Trees and Hypothesis Class

Zico

不是我们预测正一。所以本质上,它们只是以树的形式遍历一系列规则,然后做出最终预测。所以我们的假设类实际上是一个树结构,它将数据集划分成不同的区域。在每个叶节点,你可以只做一个单一预测,比如如果到了这里,我就总是预测正一。但你也可以扩展,让叶节点有线性回归函数,甚至更复杂的函数。实际上,昨天那位演讲者提到的一个函数就是这样的:你划分空间,然后在每个叶节点用一个高斯过程来确定函数。所以这些可以非常强大。所以在这种情况下,我们的假设类就是空间的划分。

Not we predict positive one. So essentially they just go through a bit of this list of rules in a tree form and make the final prediction. So our hypothesis class here actually ends up being a kind of tree structure that will partition the data set into different regions. Now at each final node here, you could either just make a single prediction, just say this is going to be if I ever get here, I'm just always going to predict plus one. But you can also extend this to actually have linear regression functions at the roots, sorry, at the leaves of the tree, or even more complicated functions. And I think actually one of the functions that the nano talked about yesterday was kind of like that: you had partitioned the space and then at each leaf you would have a Gaussian process that determines your function. So these can be very, very powerful here. So our hypothesis class in this case is these partitioning of the space.

8. 决策树中的损失函数与优化 Loss Function and Optimization in Decision Trees

Zico

典型决策树的损失函数稍微复杂一些,但你可以把标准训练方法看作是在最小化某种类似于逻辑损失的东西。实际上它并不完全是,但它是某种概率型损失,我稍后会讲到。这就是优化的损失函数。但关键点是,与之前看到的许多类不同(除了神经网络),我们无法精确优化所有决策树的类。我们不能像线性回归那样直接找到最优解、最优树。所以我们做的是贪婪地分割数据,朝着最小化损失函数的方向取得最大进展。所以我们有一个概率型的损失函数,我们选择第一个节点来最大程度地最小化那个损失。我们可以选择一个节点来最小化,然后在这个节点上分支,再对后续两个节点重复这个过程。所以你可以把这看作是一种贪婪优化,每次迭代试图在一个坐标上最小化损失函数。这只是一个高层次的描述,但这就是决策树所做的。你会经常看到它们。

The loss function for the typical decision tree is a little bit more complex, but you actually can look at standard ways of training decision trees as minimizing something that's sort of like logistic loss. It actually ends up being not quite that, but it is sort of a probabilistic type loss, which I'll get to later. That's the loss function there optimizing. But the real key here is that unlike a lot of the classes we've seen before, I guess other than neural networks, there's no real way to optimize exactly over the class of all decision trees. Right, we can't like with linear regression and just find the optimal solution, find the optimal tree. So what we do instead is we actually kind of greedily split the data in a way that starts making the most progress towards minimizing that loss function. So essentially we have some loss function that's kind of like a probabilistic loss, and we pick the first node in order to most minimize that loss. We can pick one node to minimize, then we branch upon that one node, and then for those two subsequent nodes we do this process again. So this you can think of as a greedy type of optimization that tries at each iteration to minimize this loss function over one coordinate at a time. Again, sort of very high level here, but that's kind of what decision trees are doing. And you will see these a lot.

9. 集成方法:随机森林与提升 Ensemble Methods: Random Forests and Boosting

Zico

现在你很少单独看到它们了。通常你会看到它们与集成方法结合使用。集成方法就是其他假设的组合。所以你的最终假设等于一个加权组合,比如在 K 个其他假设类上。这里的参数就是这些不同类上的权重。当然,每个子假设都有自己的参数集。所以我们应该把 theta 写成既是权重的参数,也是每个单独假设类的参数,但我这里只是用简写。你可能特别听说过两种常见类型,它们通常都用决策树作为个体假设。所以它们以一种很好的方式堆叠这些东西。一种是随机森林,它本质上是决策树的集成。因为很多树组成一个森林,每棵树建立在数据的不同子集上。这是一个非常简单的想法,但如果你想要一个在许多问题上表现良好的简单算法,随机森林做得非常好。它们是一种非常鲁棒的算法,在许多不同问题上都表现良好。你可能听说过的另一种是 Boosting。Boosting 也是一种组合不同分类器的方法,通常也是决策树。你可以把 Boosting 看作是在最小化指数损失函数。这个我之前没怎么讲,但在之前的幻灯片上有。指数损失是其中之一,Boosting 实际上是一种贪婪优化方法,用你想要的任何假设类作为基分类器来最小化指数损失。这是一个非常简短的概述。我没有完全公正地介绍所有这些方法,但你要记住的是,你在机器学习工作中可能会遇到这些算法,至少要知道,虽然它们并不总是完美契合,但它们并不一定是在这个框架下开发的,但你仍然可以用这个视角来看待它们。所以很容易把这些算法看作有一个假设类,最小化某个损失函数,使用某种优化方法,你可以把大多数算法(至少大多数监督算法)放进这个框架。

Now you actually don't see them much on their own anymore. What you typically see is them combined with what's called an ensemble method. What these are, very generally speaking, is ensemble methods. Ensemble method is really just a combination of other hypotheses. So your final hypothesis is just going to be equal to some weighted combination, say over K other hypothesis classes. Now our parameters here are just the weights on those different classes. And of course, each sub-hypothesis here would have its own set of parameters. So we should really write this as theta being both the parameters for the weighting and the parameters on each individual hypothesis class, but I'm just using brief notation here. Some that you might have heard of in particular are two very common types, and these actually both often use decision trees as the individual hypotheses. So they're kind of stacking these things in a very nice way. One is random forests, and that is essentially an ensemble of decision trees. Because you know, many trees that are a forest, where each tree is built on different subsets of the data. It's a very simple idea, but it actually ends up if you just sort of want a simple algorithm that will perform well on a lot of problems, random forests do very, very well. They're sort of a very robust algorithm, they do well on a lot of different problems. Another one you might have heard about is boosting. Boosting is also a way of combining different classifiers, again often decision trees. And you can actually view boosting as minimizing an exponential loss function. So this is one I haven't really talked about too much, but it was on the slides before. Exponential loss is one of them, and boosting actually looks like a greedy optimization approach to minimizing exponential loss with whatever hypothesis class you want as the base classifier. So that's sort of a very brief overview. I didn't really do any sort of justice to all these methods, but the thing to keep in mind is you probably will see these algorithms as you work in machine learning, and it's good to at least know that though the fit isn't always perfect, they weren't really developed necessarily with this kind of framework in mind, but you can still typically actually view them this way. So it's very easy to think of these algorithms as having a hypothesis class, minimizing some loss function, using some optimization method, and you can fit most algorithms into that framework, at least most supervised algorithms.

10. 无监督学习概述 Unsupervised Learning Overview

Zico

好的,再次非常简短,但你会看到其中一些的更多细节。现在我们来非常简短地谈谈无监督学习。记住,监督学习的基本流程是:我们有一些训练数据,比如一些数字及其对应的标签。我们用机器学习得到一个假设函数,当应用于训练数据时,它大致给出我们想要的输出,然后我们基于没有标签的新数据进行预测。这就是整个流程。无监督学习看起来很难,因为基本上流程相同,只是去掉了标签。我们没有告诉它输出应该是什么,似乎我们之前讨论的一切都会失效,对吧?因为现在,假设类的全部意义在于我们有输入 X_i,我们希望应用假设函数后,它近似等于对应的 Y_i。这就是我们衡量损失的方式,即这两者之间的差异。当我们没有 Y_i 时,我们怎么做呢?所以我们的设置有些相同:我们有一些输入特征,我们仍然有一些模型参数。我想我现在要放弃之前的一个假设,我慢慢地做了,但之前讲座中参数的数量总是等于输入向量的维度,但实际上如果你看我之前展示的所有其他假设类,情况已经不再是这样了。所以我现在就放弃那个假设。

Okay, so again very brief, but you'll see more detail about some of these. All right, so let's talk about now, again very shortly, about unsupervised learning. So remember that our basic pipeline for supervised learning is that we have some training data, so we have some digits and their corresponding labels. We use machine learning to come up with some hypothesis function such that when we apply it to the training data, it kind of gives us approximately the outputs that we want, and then we make predictions based upon new data that we don't have the labels for. All right, that's the overall pipeline. Now unsupervised learning seems really hard because basically we have the same pipeline except we take away the labels. We don't tell it anything about what these outputs should be, and it seems like everything we talked about so far will kind of break down then, right? Because now, I mean the whole point of this hypothesis class, the whole point was that we had some input X_i, and we wanted it to be the case that when we applied our hypothesis function to this, this was approximately equal to the corresponding Y_i. This is how we measure loss, the difference between these two things. How can we do this when we don't have Y_i anymore? All right, so our setting is somewhat the same: we have some input features, we still have some model parameters. And I think now I'm just going to abandon, I sort of did it slowly, but before lecture the number of parameters was always equal to the size of the input vectors, but actually if you look at almost all the things I've shown you before, the other hypothesis classes, this is not the case anymore. So I'm just going to abandon that for now.

11. 无监督学习设置 Unsupervised Learning Setup

Host

假设我的模型参数,假设有另一个大小 k,我们有 K 个不同的参数,这不总是等于输入大小,有点超出,但我要把 theta 称为一个向量。所以我们有同样的设定:我们有模型参数,有一些输入特征。但这到底意味着什么?意义何在?当我们没有任何输出来提出假设时,我们在这里做什么?实际上有几种看待无监督学习的方式,但一种能涵盖许多不同无监督学习方法的解释是:无监督学习本质上是在某种程度上尝试重建输入数据。所以我们试图——我们像之前一样有一个假设函数——我们会有点奇怪:我们说实际上我没有 y,所以我希望我的假设函数在应用于 X_i 时,近似等于 X_i。这意味着我们的假设类不再是从 R^n 到 R 的映射;我们的假设函数现在是从 R^n 到 R^n 的映射,我们要选择参数,使得当我们将 X_i 输入这个函数时,它近似等于 X_i。这听起来很奇怪,等一下,因为看起来你可以很容易地做到这一点,而且你不会学到太多。类似地,损失函数现在,因为我们测量这两者之间的距离,我们需要一个也接受两个输入向量作为输入的损失函数。所以损失函数现在是从 R^n × R^n 到 R_+ 的映射:两个 n 维向量作为输入,我们得到一个输出。一个常见的例子就是我们之前测量最近邻时看到的距离。这基本上就是平方损失对多变量输入的推广。所以一个常见的例子就是,我们要最小化的东西是这两个向量之差的 L2 范数的平方。所以平方与否无所谓。好的,这说得通。关于这个设定有什么问题吗?

Just call my model parameters say there's some other size k we have K different parameters which is not always the same as our input size it's a little outside but I'm going to now refer to theta as being a vector. So we have this same setup: we have some model parameters, we have some input features. But what does this even mean? What's the point? What do we do here when we don't have any outputs to formulate hypotheses towards? There's actually a couple of ways to view unsupervised learning, but one interpretation that encompasses a lot of different unsupervised learning methods is to say that unsupervised learning is really about trying, in some sense, to reconstruct the input data. So we're trying to take—we have a hypothesis function as before—we're going to be kind of strange: we're going to say that I actually don't have y's, so I actually want my hypothesis function, when applied to X_i, to be approximately equal to X_i. So this now means our hypothesis class is no longer a function mapping from R^n to R; our hypothesis function is now going to be a mapping from R^n to R^n, and we want it to be chosen—we're going to choose our parameters such that when we plug X_i into this function, it approximately equals X_i. That sounds weird, hold on a sec, because it seems like you could do this really easily and you wouldn't really learn very much. Similarly, the loss function now, because we're measuring the distance between these two things, we need a loss function that's also going to accept two input vectors as inputs. So the loss function will now be a mapping from R^n times R^n to R_+: two n-dimensional vectors as input, and we get an output. A common one, for example, would just be the distance we saw before when we were measuring nearest neighbors. This is just sort of the generalization of squared loss to multivariate inputs. So a common one, for example, would just be to say that the thing we're trying to minimize is the L2 norm of the difference between these two things, squared. So it doesn't matter if you square it or not. Okay, that makes sense. Any questions about the setup here?

12. 为何不直接用恒等函数? Why Not Just Identity Function?

Host

哦,是的,这是个好问题。好吧,当然你们可能都在想一个显而易见的问题:这看起来有点傻,因为我们可以直接选择 h 输出我们输入的任何东西,这样损失就是零,而我们实际上什么也没学到。显然,我们什么也没学到,因为没有理由不选择 h_theta(X) = X。是什么阻止了我们?当然,要阻止这一点,我们必须限制我们在假设类中考虑的函数类。我们不能考虑所有可能的函数,或者至少不能考虑任何包含这个函数的函数类,因为那显然会最小化损失,而且不会给我们任何信息。所以我们必须做的是以某种方式限制这个假设类,不允许那样做,并迫使函数在某种意义上需要从数据中提取一些结构,才能实际提出一个能最小化这个损失的假设函数。事实上,就像我们在监督学习中的问题一样,我们有一个典型的优化问题,可以涵盖很多这类问题。我们可以对无监督学习做同样的事情。所以我们可以把很多这类问题写成:在 theta 上最小化从 i=1 到 M 的 l(h_theta(X_i)) 之和。这就成了我们的——就像我们在分类和回归中有典型的损失最小化一样,这就是无监督学习的典型形式。我认为理解这一点的最好方法是看两个例子,它们都符合这个框架,尽管看起来非常不同。这两个算法叫做 k-means 和主成分分析,它们通常不是在这种框架下教授的,但你可以在这个框架下理解它们。好的,我将非常简要地介绍它们,获得直觉,但重点放在它们如何符合这个框架上。

Oh yes, that's a good question. All right, so of course the obvious question that maybe you're all thinking about is that this seems kind of silly, because we could just choose h to output whatever we input into it, and that would give us zero loss, and we wouldn't really have learned anything. Clearly, we wouldn't have learned anything, because there's just no—why not just choose h_theta of X equals X? What's preventing us? Well, of course, to stop this, what we have to do is restrict the class of functions we can consider in our hypothesis class. We can't consider all possible functions, or at least we can't consider any function class that would include this function, because that would clearly minimize the loss and it wouldn't give us any information. So what we have to do instead is somehow restrict this hypothesis class to not allow that, and to force the function, in some sense, to need to extract some structure out of the data in order to actually come up with a hypothesis function that can minimize this loss. In fact, just like our problem for supervised learning, we had one canonical minimization problem that kind of fit a lot of these things. We can do the same for unsupervised learning. So we can write a lot of these problems as minimize over theta the sum from i=1 to M of l(h_theta(X_i)). So this becomes kind of our—just like we had the canonical loss minimization for classification and regression, this is the one for unsupervised learning. I think the best way to see this is actually to look at two examples that both fit into this framework, though they seem quite different. These two algorithms are called k-means and principal component analysis, and they're typically taught not in this kind of framework, but you can do them both in this framework. Okay, so I'm just going to cover them very briefly, get the intuition, but focus on how they actually do fit this framework.

13. K 均值示例 K-Means Example

Host

我们换一块新板子。也许我能做好。不幸的是,你会看到。我想你能看到屏幕的最上方,对吧?你还能看到上面的假设函数或最小化函数。好的,那么让我们看看 k-means。k-means 说的是我们的参数将是输入空间中的一组 k 个不同的点。我把它们称为 mu_1 到 mu_k。每个点的大小与输入向量相同。我们的假设类——我们不能随便选任何输出;我们必须输出这 k 个数字中的一个作为我们的预测,然后与输入值 X 进行比较。抱歉。我们的假设类:我们必须输出这 k 个数字中的一个,当然如果你想做得合理,你可能会选择最接近输入的那个。所以假设函数最终是 mu,再次说明,这里的符号有点繁琐,但你选择使得 X 与 mu 之间距离最小的那个 mu。所以你查看所有这些不同的 mu,将 X 与所有不同的 mu 进行比较,然后选择使这个距离最小的那个。然后,给定这个,我只想最小化这里的标准优化问题。不过,与我们之前看到的许多问题不同,我想类似于神经网络这样的算法,我们不能精确地求解。它最终是这种假设类;你不能依赖——你甚至不能轻松地求梯度。就是你不能真的——不像最小二乘法,你不能写出一个精确解。解决这个问题的典型方法,而不是我们之前看到的精确方法,是在两个步骤之间交替。所以第一步,你为所有数据点——进行训练——找到离它们最近的均值。所以如果是 mu,你找到哪个均值是最小化器,然后你重新计算那个均值,使其成为所有分配给它的东西——基本上就是数据集中所有最接近那个均值的例子,你把新均值取为这些例子的均值。好的,这是一个非常粗略的介绍,但希望你能理解我们在这里试图最小化平方损失;这就是我们的假设类。

Let's get a new board. Maybe I will do this well. You're gonna happen unfortunately. I think you can see the very top of the screen right? You can still see that hypothesis function or that minimization function up there. Okay, all right. So let's look at k-means. What k-means says is that our parameters are going to be a set of k different points in the input space. I'm going to call those mu_1 to mu_k. Each of these is the same size as the input vector. And our hypothesis class—we can't just pick any output we want; we have to output one of those k numbers as our prediction that we're going to compare to our input value X. Sorry about that. Our hypothesis class: we've got to output one of these k numbers, and of course if you want to do any reasonable job at this, you'll probably pick the one closest to your input. So the hypothesis function ends up being mu, and again this is kind of cumbersome notation here, but you pick the mu such that it minimizes the distance between X and mu. So you look at all these different mu's, compare X to all the different mu's, and just pick the one that minimizes this distance. Then given that, I just want to minimize the standard optimization problem here. Now, unlike a lot of the things we've seen before, though, I guess similar to algorithms like neural networks, we can't solve this exactly. It ends up being this kind of hypothesis class here; you can't rely on—even you can't take gradients of this easily. It's just you can't really—unlike least squares, you can't write down an exact solution. The typical way to actually solve this, rather than the things we've seen exactly, is to alternate between two steps. So what you do in the first step is you find for all the data points—do training—you find the closest mean to those. So if I'm mu, you find whichever mean is the minimizer here, and then you just recompute that mean to be all the things that are assigned to it—essentially all the examples in your dataset that were closest to that mean, you take the new mean to be the mean of those examples. Okay, so a kind of very high level there, but hopefully the intuition comes across that we are trying to minimize squared loss here; this is our hypothesis class.

14. K 均值直觉与损失函数 K-means intuition and loss function

Zico

直觉上,我们限制自己的方式是,我们只让假设输出这 K 个值中的一个。因此,我们不能总是输出输入本身,对吧?因为只要 M 大于 K,你就不能总是输出输入。你必须找出那些最能最小化损失函数的均值。有问题吗?这些均值不必是训练样本;它们可以是任何东西,只是数字,是 R^N 中的向量。不,它们绝对不必是训练样本。在优化过程中,最小化这个损失的一种方法是将它们设为不同 X 的平均值,但这有点偶然。我故意略过这一点,因为高层次的重点是,你只是试图选择那些能最小化这个损失的均值。通常,这可以用一张图很好地展示——我不想过多强调——但如果你有一些像这样的数据,并且你有两个不同的均值,一个好的策略是选一个均值在这里,因为它会是这里所有点的输出,另一个均值大概在这里,因为它会是离这里所有 X 最近的那些点的输出。所以本质上,如果你优化得好,你会把这两个点归为一类,另外两个点归为另一类,然后找到合适的均值,一切就都好了。是的,好主意。有个问题:就像我们这里遇到的所有其他问题一样,比如如何选择正则化时的 lambda 值?如何做这些事情?一个很好的方法也是通过交叉验证。所以你可以计算这个损失,再次将训练集分成输入和输出——抱歉,更好的是:将训练集分成训练集和验证集,这里没有输出,然后用验证集来评估这个损失。好吧,这对聚类来说效果不如分类或回归那么好,但你仍然可以使用同样的思路。实际上,这种损失函数公式的一个优点是,它实际上让你清楚要解决什么问题,所以这种分离是可行的;它能给你一些东西,而许多典型的 k-means 观点只是说“嗯,这里看起来像两个东西,我就选两个。”这在二维情况下效果很好,但有时甚至没有明显的两个聚类。但这种损失函数公式真正好的一点是,你可以做同样的事情,并使用留出验证集来实际评估你应该选择多少个聚类。

And the intuition, the way we're limiting ourselves is that we're restricting our hypothesis to only output one of these K values. Therefore, we can't just always output the input, right? Because as long as you have M greater than K, you can't just output the input always. You have to figure out what are the means that do the best job of minimizing that loss function. Any questions? These means don't have to be the exercise; they can be anything, they're just numbers, they're vectors in R^N. No, they definitely don't have to be the exercise. In the optimization procedure, a way of actually minimizing this loss is to set them to be averages of different X's, but that's kind of incidental. I'm actually glossing over that intentionally because the high-level point here is that you're just trying to pick those means that minimize this loss. And the way this is typically seen nicely in a picture—which I didn't really want to highlight too much—but if you have some data that looks like this, and you have say two different means, a good strategy would be to pick one mean to be here because this one would be the output for all the guys in here, and one mean kind of here because that would be the output for the ones closest to all the X's in here. So what you do essentially is, hopefully if you do a good job of optimizing, you'll assign at some point these two one cluster and these two another cluster, and then you'll find the means appropriately, and then everything works well. Yeah, good idea. A question: so much like all the other problems we've had here, for example, the same thing with how do you choose a good value of lambda if you're regularizing? How do you do these things? A good way of doing this is actually through cross-validation too. So what you can do is you can compute this loss, you can again divide your training set into inputs and outputs—sorry, better: divide your training set into training and validation without any outputs here, and use the validation set to evaluate this loss. All right, now that doesn't work quite as well for clustering as it does for classification or regression, but you can still use the same point. Actually, in part, one of the advantages of this loss formulation is that it actually kind of makes clear what you're trying to solve, and so this sort of separation can actually work; it can give you something, whereas a lot of typical views of k-means it's just kind of 'well, they look like two things here, I'll pick two.' That works great for two dimensions, sometimes. Sometimes there aren't even clear two clusters here. But what's really nice about this loss function formulation is you can do the same thing and you can use a holdout validation set to actually evaluate how many clusters you should pick.

15. 主成分分析(PCA)框架 Principal Component Analysis (PCA) framework

Zico

另一个适合这个完全相同框架的算法——嗯,上面那个通用框架是主成分分析。这里,这个——我应该拿好一点——这个是 k-means,这个将是主成分分析。这里的想法是,我们要预测的输出不再来自某个固定的输入类别;相反,它们将位于一个低维空间。我们正式地这样写:我们说我们的假设——首先,我们的参数将是两个矩阵,beta 1 和 theta 2。大小在这里;我就不写出来了。一个是 n×K,theta 2 是 K×n,所以现在是矩阵,并且 K 小于 n。例如,theta 2 是一个胖矩阵,theta 1 是一个瘦矩阵。假设类说:我要输出 theta 1 乘以 theta 2 乘以 X。好的,我们这里没有真正讲线性代数,所以如果这有点令人困惑,别担心。本质上,这样做是因为 theta 2 的维度低于 X,这个操作迫使这个向量变成 R^K 中的一个向量,由于 K 小于 n,这个向量包含的信息比原始输入向量少。然后我们乘以这个东西,它会将我们带回原始的 R^n 空间,这样我们就可以与我们的样本进行比较。但本质上,这里发生的是,这一步在压缩数据;我们必须丢弃信息,不能再保留全部。从视觉上看,一种方法是像下面这样想。我还应该说的是,有了这个函数,我们再次解决了那个导致这个优化问题的问题。这个也是非凸的;别担心。它和我们有的所有其他范数一样。这个问题也不是我们通常认为的可解问题,但它实际上是可解的。事实上,这是少数几个即使不是所谓的凸问题也能高效求解的问题之一。所以我们实际上可以用所谓的特征值分解来求解它。好的,所以我们可以在这里找到精确解,尽管看起来可能不行。首先,是的,你可以让 K 大于 n。所以如果 K 大于 n,那就有点奇怪了,对吧?因为那样这个函数又回到了你的假设类中:函数 h(theta) = x 仍然在你的类中。你只需选择这些为单位矩阵。比如这里 K 等于 n,这两个都是单位矩阵;这意味着你的假设就是预测你看到的输入。所以你可以这样做,但没什么用;你得不到多少价值。PCA 的实际价值在于你压缩数据,找到数据所在的降维空间,然后将其投影回高维空间。一种理解方式是:如果这是你的原始数据,比如你有二维输入数据,并且 K 等于 1。所以这里的 n 是 2,选择 K 等于 1。好的,这在图形上看起来是,你必须取所有数据,然后找到你想要预测它们都位于的那条线。所以你想要预测这些点:这个,这个,这个,这个。所以是这些数据点在线上的最近点。PCA 实际上是如何做到这一点的呢?它是在寻找——因为——本质上,当你做这个操作时,你得到一个一维的量,表示你沿着这条线的位置,然后你投影回二维,这又给出了类似的图形,但所有这些点都在这一条线上。这里发生的是,PCA 实际上既选择了这些点本身,也选择了你能穿过数据画出的最佳直线。当然,在更高维度中,比如 10 维,如果 K 是 2,那么你就是在寻找最适合这些维度的最佳平面。

So another algorithm that fits into this exact same framework—well, the general framework up there is principal component analysis. And here, so this here—I should hold it better—this here is k-means, and this here will be principal component analysis. And the idea here is that the outputs that we're going to predict are no longer from some fixed class of inputs; instead, they're going to lie on a lower dimensional space. And the way we write that formally is the following: we say that our hypothesis—first of all, our parameters are going to be two matrices, beta 1 and theta 2. The sizes are here; I won't bother writing those down. One is going to be n by K, theta 2 is going to be K by n, so these are matrices now, and we have K less than n. So for example, theta 2 is going to be a fat matrix, and theta 1 is going to be a skinny matrix. And the hypothesis class says: I'm going to output theta 1 times theta 2 times X. Okay, and we didn't really cover linear algebra here, so if this is a little bit confusing, don't worry about it. Essentially, what this is doing is, because theta 2 is of lower dimension than X, this operation here forces this vector to become a vector in R^K, and since K is less than n, this vector here has less information in it than the original input vector. Then we multiply by this thing here, which will put us back into the original space of R^n, so we can compare with our examples. But essentially, what's happening here is that this step here is kind of compressing the data; we have to throw away information, we can't keep it all anymore. One way of looking at this visually is to think of it like the following. And I should also say that with this function here, we again just solve that problem that leads to this optimization problem. This one is also non-convex; don't worry about that. It's just the same as all the other norms we have. This problem is also not what we typically think of as a solvable problem, but it actually is. In fact, this is one of the few problems that we actually can solve efficiently even though it's not what's called a convex problem. So we actually can solve this using something called an eigenvalue decomposition. All right, so we can find the exact solution here even though it might not look like we can. First, yeah, you can keep K greater than n. So if K is greater than n, it's a little bit odd, right? Because then you have this function back in your class: the function h of theta equals x is still in your class. You just pick these to be the identity matrices. Say K equals n here, these are both the identity; that means your hypothesis is just predict what you saw as input. So you can do that, but it isn't very useful; you don't get a lot of value out of it. The actual value of PCA comes from the fact that you are compressing the data, and so you are finding this reduced dimension the data lies in, and then projecting it back into the higher dimension. One way of viewing this is: if this is your data originally, so say you have two-dimensional input data, and say K equals 1. So n here is 2, and pick K to be equal to 1. Okay, what this looks like graphically is you have to take all your data and essentially find the line that you want to predict it all lies on. So you want to predict the points: this one here, this one here, this one here, this one here. So the closest points to these data points that lie on a line. How PCA is actually doing this is finding—because what was so—essentially when you do this operation, you get a one-dimensional quantity that says where you lie along this line, and then you project back in two dimensions, that gives you sort of this figure again, but they all lie on this one line here. And what's happening here is that essentially PCA is picking both these points what they actually are and the best line you could draw through your data. Now, of course, in higher dimensions, it can be, you know, 10, and if K was 2, then you'd be finding the best plane that fits through those dimensions.

16. 无监督学习与概率模型 Unsupervised Learning and Probabilistic Models

Zico

如果你在找一个能穿过数据的好超平面,嗯,好问题。我觉得我同时也在讲。所以,好问题。如果 X 是一个虚拟维度,你总是可以用同样的技巧,给它加 1。这里有两种做法。第一种是给输入向量加 1,从而避开这个问题。更好的做法其实是在做 PCA 之前,先把数据中心化到原点,也就是减去均值。这样,我在这里没画坐标轴,所以其实没问题。我几乎是在骗你,这里的 p 是 100,当然我是那个意思。所以两种都可以,但可能后者更好。还有别的问题吗?没有,好吧。我就说这么多。无监督学习是一个很大的领域,虽然我不总是这样讲,但我认为你可以用非常类似的框架来看待它。用同样的损失函数框架来理解是很有价值的。好,我们来看最后一点:概率。昨天我讲了差不多三个小时的机器学习,却一直避免谈概率,但当然这些东西是紧密相关的。实际上,概率模型支撑着大量的机器学习算法。我之前用了逻辑损失之类的,并说这里有一些概率解释,而对于平方损失,我们选择它也是有概率上的原因的。概率模型支撑着机器学习的许多基本选择,也让我们能更好地解释结果,真正捕捉到我们对数据的不确定性。这正是概率模型派上用场的地方,因为它们不仅能给出预测,还能让你从预测的置信度角度来理解结果。所以我们来想想:为什么我们选了平方损失?在线性回归中,我们选了它。我说用这个损失函数,最小化预测输出和真实输出之间的距离,损失是平方差。为什么选它?我昨天给了一个答案:因为它容易优化,而且很好用,这完全是个好理由。但我们也可以想别的理由。实际上,使用平方损失的一个动机来自概率。这个想法在机器学习中非常普遍,在机器学习课上不提它可能不太好。想法是这样的:我们假设数据集中的输出不仅仅是 f(x),而是 f(x) 加上一些噪声。存在一个底层模型,输出 y 属于假设类,可能是线性函数或其他。但我们实际观察到的不是真实输出,而是输出加噪声。具体来说,我们观察到的所有 y_i 都是真实假设类加上噪声项。一个非常常见的噪声项是高斯随机变量。所以这是高斯随机变量的密度。另一种写法是重新排列:我们可以写出给定 x 和参数 theta 时 y 的概率密度,就是这个东西。这个概率也由高斯密度给出,所以等于 1/sqrt(2πσ) * exp(- (h_theta(x) - y)^2 / (2σ^2))。这里的 σ 是分布的方差,但如果假设它是已知常数,它实际上不影响我们得到什么样的损失函数。现在我们这样做:我们说,好,我这样定义一个模型。我定义了给定输入和参数 theta 时输出的概率。如果我想根据这个概率模型选择 theta,合理的做法是选择最大化我实际看到的数据的 theta。我看到了一堆 y 和 x。如果我们进一步假设这些是独立同分布的随机变量,那么它们都有相同的分布且相互独立,那么我们可以写出我们看到的数据的概率,即 y_1 到 y_m,是所有这些概率在数据点上的乘积。然后合理的做法是选择使这个概率尽可能大的 theta。这说得通。所以基本上,我们有数据的概率。给定一堆 x_i 和 y_i,数据的概率等于所有项 P(y_i|x_i, theta) 的乘积。我们只需找到最大化这个的 theta。这是一种概率方法,因为我们基于一些参数为数据定义了一个概率模型。这些参数决定了假设函数,和之前一样。但这里我们确实有一个概率模型。现在我们选择最大化观测数据概率的 theta。这很合理。接下来几步简单的推导表明,这个问题等价于最小化 theta 的平方误差。本质上,最大化这个乘积等同于最大化它的对数,因为对数函数是单调的。所以你可以最大化这个或最大化对数。对数的好处是把乘积变成和。所以从最大化乘积变成最大化对数之和。我们更进一步,考虑最小化,所以最小化负对数,即这些对数项的和。现在代入指数函数。取对数,得到这一项的对数,对数抵消指数,所以得到这个对数项加上 1/(2σ^2) 乘以平方损失项。这等价于:这一项根本不依赖于 theta,所以我们可以忽略它。

If you're finding a good hyperplane that fits through that, yeah, okay, it's a good point. So I thought the way I'm also doing is I'm also talking. So okay, good point. If X is a virtual dimension, you can always do the same trick where you add 1 to it. There are two things you can do here. The first is you can add 1 to your input vector and kind of get away from this. The better thing to do actually is, before you apply PCA, to actually center your data around the origin, so that you just subtract the mean off. So that actually, I didn't write any axes here, so I'm actually fine to do this. So I'm almost lying to you, this p is 100 here, of course that's what I meant. So you can do either one; this is probably the better one to do. There's another question over there? No, okay. All right, that's all I'm going to say. Unsupervised learning is a very big field, but I'm not always presenting it this way, but I think you can view it in a very similar framework. There's a lot of value to viewing it in the same kind of loss framework. All right, let's look at the last thing: probability. So I've somehow managed, as I spent yesterday, to avoid talking about probability when talking about machine learning for almost three hours, but of course these things are very tied together. And the reason really probabilistic models underlie a huge range of machine learning algorithms. You know, I used things like logistic loss and kind of said there's some probabilistic interpretation here, and with squared loss, it's actually a reason why we choose the squared loss function as opposed to anything else. And really, probabilistic models underlie a lot of the basic choices of machine learning, and they also let us interpret the results in a better way that actually captures how we are uncertain about the data. And that's really where probabilistic models can come in handy, because they can give you not just a prediction, they can let you interpret that in terms of maybe how certain you are about your predictions. So let's think about this for a second: why did we pick the squared loss? Now we picked it in linear regression. I said let's use this loss function that minimizes the distance between the output that we predicted and the true output, and our loss is the squared difference. Why did we choose that? And one answer that I gave yesterday was: well, we choose it because it's easy to optimize and it's kind of nice, and that's actually a perfectly good reason. But we can think of other reasons too. And actually, one of the motivations for why we use squared loss comes from probability. So here's the idea. This is a very general concept that's used all over the place in machine learning, and it would probably be a bad idea to not mention it at all in a lecture on machine learning. The idea is the following: we're going to hypothesize that the outputs in our dataset are not just equal to f of x, they're actually equal to f of x plus some noise. There is some underlying model that outputs the y's as the hypothesis class, which could be a linear function or something else, but we don't actually observe the real outputs here; what we observe is the output plus some noise term. So in particular, all our y_i's we observe are the true hypothesis class plus some noise term. And one very common noise term is a Gaussian random variable. So this is the density of a Gaussian random variable. Another way to write this would be to rearrange terms: we can write the probability of y, really the probability density of y given x and parameterized by theta, as this whole thing. The probability of this is also given by a Gaussian density, so this also equals 1 over sqrt(2 pi sigma) times exp(- (h_theta(x) - y)^2 / (2 sigma^2)). Sigma here is the variance of the distribution, but it actually ends up, if you assume it's some known quantity, not affecting what kind of loss function we get. And now we do the following: we say, okay, I'm defining a model this way. I'm defining what's the probability of an output given an input, parameterized by some theta. What I want to do, what is reasonable to do here, if I want to choose theta based upon this probabilistic model, is to choose the theta that maximizes what I actually saw. So what I saw was a bunch of y's and x's. If we assume furthermore these are all independent and identically distributed random variables, so they all have this same distribution and they're all independent from each other, then we can write the probability of what we saw, namely y_1 through y_m, as the product of all those probabilities evaluated at the data points that we've seen. And a reasonable thing to do then would be to pick the thetas that make that probability as high as possible. So that makes sense. So basically, we have the probability of our data. Given a bunch of x_i and y_i, the probability of our data is going to be equal to the product of all these terms: probability of y_i given x_i parameterized by theta. And we would just find the theta that maximizes this. So this is a probabilistic approach, because what we're doing is we're defining a probabilistic model for our data based on some parameters. And by the way, these parameters come in because they determine the hypothesis function much in the same way as our old setting. But this is really a probabilistic model that we have here. Now we're going to pick the thetas that maximize the probability of what we observed. That makes sense. Now a few very simple steps here show that this problem is equivalent to minimizing over theta of just the squared errors. Essentially, what happens is: maximizing the product here is the same as maximizing the log of this thing, because log is a monotonic function. So you could either maximize this or maximize the log. The nice thing about logs is it lets you turn this product into a sum. So you can go from maximizing a product of things to maximizing a sum of log things. And while we're at it, let's just take it one step further and minimize, so let's talk about minimizing. So far, let's minimize a negative log, so sum of the log terms here. Now we're going to plug in our exponential function. We take logs, get a log of this term, the log cancels out the exponent there, and so what you get is this log term here plus 1 over 2 sigma^2 times this squared loss term here. And that's just equivalent to this: this term here does not depend on theta at all, so we can ignore it.

17. 最大似然与最小二乘 Maximum Likelihood and Least Squares

Zico

这个项虽然有个 sigma,但它只是个缩放因子,所以没关系。最小化 sigma 乘以这个项,等价于最小化那个项本身,所以实际上和最小化平方损失是完全一样的。这挺好的,因为现在我们有了另一种解释最小二乘法的方式:我们实际上是在最大化高斯模型下观测到数据的概率。当然,从某种意义上说,这也有点令人不满,因为……这是一种叫做最大似然估计的技术。这个函数叫做似然函数,我们通常把它看作 theta 的函数,即 theta 的似然,然后选择 theta 来最大化似然。但这看起来有点令人不满,因为我们只是从选择一个损失函数变成了选择一个分布,对吧?我们为什么选择高斯随机变量呢?如果不是因为最终它们能导出一个好的优化问题,就像我们选择最小二乘法一样?但在某些情况下,出于理论或经验原因,选择高斯分布是合理的。你可以观察数据,看它是否近似服从高斯分布,尝试经验性地验证,这能让你更确信这是我们应该使用的损失函数。我们应该使用高斯模型,因此应该使用最小二乘损失。而且它还能告诉你,我们不仅有了求解 theta 的方法,还有一个概率模型,能告诉我们每个新输入的预期值范围,对吧?因为对于一个新的输入 x_i,你实际上有一个关于可能 y 的分布,这能告诉你预期的不确定性,即这些输出的预期范围。

This term here, though it has a sigma in it, doesn't really matter because it's a scaling factor. Whatever minimizes sigma times this minimizes that thing too, so effectively it's the exact same thing as just minimizing this squared loss. This is kind of nice because now we have another way of interpreting what we're doing when we're doing this least squares. What we're doing is maximizing the probability of observing the data under this Gaussian model. Of course, in some sense, this is kind of unsatisfying too because I shall so many... This is a technique called maximum likelihood estimation. This function here is called the likelihood function, and we usually view it as a function of theta; that's the likelihood of theta, and picking theta to maximize the likelihood. But it looks sort of unsatisfying because really we just went from picking a loss function to now choosing a distribution, right? Why do we choose Gaussian random variables here, if not for the fact that they actually lead to a nice optimization problem in the end, much like why we chose least squares? But there are cases where it's reasonable, for sort of theoretical or empirical reasons, to actually choose a Gaussian distribution. You can look at data, see if it follows the Gaussian distribution approximately, try to verify that empirically, and that can give you more confidence that this is the loss function we should be using. We should be using a Gaussian model here, therefore we should be using least squares loss. And it also can sort of tell you that not only do we now have a way to solve for theta, but we also have a probabilistic model that tells us the expected range of values for each new input, right? Because now for a new input x_i, you actually have a distribution over possible y's, and that can kind of tell you the expected uncertainty, the expected range of those outputs.

18. 逻辑回归与最大似然 Logistic Regression and Maximum Likelihood

Zico

我只想简单提一下,逻辑回归也是如此。你可以为二元随机变量定义一个类似这里的概率模型。你可以将给定 x 时 y 的概率定义为这个逻辑函数。所以如果你把 y 代入 +1 或 -1,它会给出 y 是 +1 或 -1 的概率。虽然这个函数和神经网络中使用的函数相同,但神经网络并没有把它当作概率函数,而是当作一个具有良好线性性质的函数。这里我们真的把它当作概率。同样的事情发生了:如果你建立最大似然估计任务,你得到的就是我们上次课看到的逻辑损失函数。所以我不多说了,但本质上完全相同。你为这些二元随机变量定义了一个概率模型,因为分类现在是二元分类任务。你求解最大似然估计问题,得到的就是逻辑损失函数。所以如果你相信那个概率模型,那么逻辑损失函数就是一个值得优化的好函数。刚才讲得很快,如果你以前没听过,我道歉。第一次听可能有点快,但真的完全一样。思路是我们有一个概率模型,我们要选择参数来最大化在该模型下观测到数据的似然,结果得到的正是逻辑损失函数。这就是我们最终得到的优化问题。顺便说一句,和线性回归一样,我们现在可以将模型的预测解释为概率,而不仅仅是数字。数值越大,我们越确信是 +1;数值越小,我们越确信是 -1。这让我们能够量化这种确定性。

I just want to mention briefly the same goes for logistic regression. Here you can essentially define a probability model much like that one for binary random variables. So you can define probability of y given x as this logistic function here. So if you plug in either +1 or -1 for y, this would give you a probability of it being either +1 or -1. Though this is the same function used for the neural network actually, neural networks simply are not using this as a probability function; they're using it as a function that has some nice linear properties. Here we're really treating it as a probability. And the exact same thing happens: if you set up the maximum likelihood estimation task, you get exactly this logistic loss function that we saw from the last lecture. So I won't dwell on that too much, but essentially the exact same thing happens here. You define a certain probabilistic model for these binary random variables because the classification is now a binary classification task. You solve the maximum likelihood estimation problem, and what you get out is the logistic loss function. So if you believe that probabilistic model up there, then the logistic loss function is a good one to optimize. That was pretty fast there, so I apologize if you hadn't seen that before. It's a little bit quick the first time, but it's really the exact same thing. The idea is we have a probabilistic model, we're going to pick parameters to maximize the likelihood of observing the data that we saw under that model, and what pops out is exactly the logistic loss function. That's the optimization problem we get in the end. And by the way, just like with linear regression, we can now interpret the predictions of our model as probabilities rather than just numbers. The bigger they are, the more certain we are it's +1; the smaller they are, the more certain we are it's -1. This kind of gives us a way to quantify that.

19. 评估算法:训练、验证、测试集 Evaluating Algorithms: Training, Validation, Test Sets

Zico

好了,我可能得先回答一些问题,直到休息时再继续,因为我还想提一件事,那就是关于评估算法。到目前为止,我简要地谈到了如何选择参数。很多人会问:如何为 k-means 选择 k?在做分类或回归时如何选择正则化参数 lambda?我也谈了很多,如果你有很多超参数,如何在它们之间做选择?对吧,如果你有很多不同的超参数来控制你的算法,如何选择最好的那些来得到最小的误差?这里有一个非常常见的方法。记得我之前告诉过你,你可以通过将数据划分为训练集和验证集来选择 lambda 或 k 等。所以你有全部的训练数据。假设我们用 20% 或 30% 作为验证集,剩下的作为训练集。就像昨天的大秀一样,我们基于训练集找到模型参数,然后基于这个留出集评估误差或损失,不在上面训练,然后可能用这个验证误差来选择你的 lambda 值。现在一个常见的错误——虽然现在不那么常见了——但你绝不应该犯的错误是:在这里运行你的机器学习算法来选择最好的 lambda 值,然后在你写的论文中报告你得到的验证损失。那么,Zuma,为什么这是个坏主意?有人猜猜为什么这真的很糟糕吗?是的,是的。所以实际上,我们是在训练我们的 theta——你可以认为是从这个集合中训练 theta 参数——但我们实际上是在基于验证集选择我们的 lambda 参数,而且这是你能找到的最好的可能值。而验证集仍然是训练数据,它不能代表新的留出数据。你用它来实际选择可能的最佳值,所以你实际上是在你的留出集上训练。只是你不是在训练你的 theta,而是在训练你的超参数,你的 lambda 或你用于优化器的任何参数。也许有一段时间这很常见:人们只是在这里找到最好的参数,然后报告他们得到的误差。正确的做法是实际上再分离出一个测试集。所以你把你的数据,实际上称之为测试集。在大数据时代,你扔掉所有这些数据的问题不那么严重,对吧?因为你们有这么多数据,所以用它们做测试可能没问题。你也可以像我之前说的那样,做在线学习,随着时间推移加入更多训练数据。但你要拿你的测试数据……

Alright, I actually probably have to field questions till you see me in the break, because I want to mention one more thing, which is about evaluating algorithms. So far, I've talked very briefly about how do you go about picking parameters. This is a question that a lot of people ask: how do you pick the k for k-means? How do you pick the regularization parameter lambda when you're doing classification or regression? And I've also talked a lot about how do you pick if you have many hyperparameters, how do you pick between them? Right, so if you have a lot of different hyperparameters that govern your algorithm, how do you pick the best ones that give you the best error? Here is a very common approach. Remember I told you before, the way you can do this, you can pick lambda or k or whatever, is by doing this division of your data into a training set and a validation set. So you have your whole training data. Say we use twenty percent or thirty percent as validation, and the rest as training. Just like on the great show you yesterday, we find the model parameters based upon this and then evaluate the error, the loss, based upon this held-out set. Do not train upon it, and maybe then use this validation error to pick your value of lambda. Now a common mistake—let's say not that common anymore—but a mistake that you should never make is to do that: run your machine learning algorithm here to pick the best value of lambda, and then you report in your papers that you write, you report the validation loss that you got. So, Zuma, why is that a bad idea? Any guesses why that's a really bad thing to do? Yeah, yeah. So effectively, we're training our theta—you can think of training our theta parameters from this set—but we're really picking our lambda parameter based upon this, and it's sort of the best possible one you could find. And this is still training data; this is not indicative of new held-out data. You were using it to actually choose the best value possible, so you were effectively training on your held-out set. It's just you're not training your theta; you're training your hyperparameters, your lambda or whatever parameters you use for your optimizer. Maybe this was actually a very common thing for a while: people would just find the best parameters here and report what they got as the error. The correct way to do this is to actually separate out an additional testing set. So you take your data, you actually call this a testing set. And in the land of big data, the fact that you're throwing away all this data here is less of an issue, right? Because you guys have so much data, so it's probably okay if you use them in testing. You can also do things like I was talking about, where you kind of do this online, so you incorporate more training data as you go. But you take your test data...

20. 数据划分与评估 Data Splitting and Evaluation

Zico

你将数据分为训练集和测试集,例如 70-30。然后你进一步将训练集分为训练集和验证集。你在训练数据上进行所有训练和模型构建,并使用验证集进行超参数调优。你只在最终完成时对测试集进行一次测试。多次测试违反了机器学习和统计学原理。理想情况下,如果你多次测试,你应该丢弃结果,废弃论文,再也不看它。但在实践中,人们常常通过在当前算法不工作时尝试新算法来过度拟合测试集。存在泄漏,即测试集上的性能影响算法选择或设计,因为你希望论文有好的结果。然而,在大数据领域,我们总是可以挑选新的测试集。如果在一个测试集上效果不好,我们就丢弃它并随机挑选一个新的,以避免偏向于任何单一测试集上的良好性能。所以在报告时,确保进行适当的分割,并尽量不过度拟合测试集,尽管即使最优秀的人有时也会这样做一点。

You divide the data into training and testing sets, for example 70-30. Then you further divide the training set into training and validation sets. You do all your training and model building on the training data, and use the validation set for hyperparameter tuning. You test only once on the testing set, when you are finally done. Testing more than once violates the principles of machine learning and statistics. Ideally, if you test more than once, you should throw away your results, scrap the paper, and never look at it again. But in practice, people often overfit the testing set by trying new algorithms when the current one doesn't work. There is leakage where performance on the testing set influences algorithm choice or design, because you want good results for your paper. However, in the realm of big data, we can always pick new testing sets. If it doesn't work on one testing set, we throw it away and pick a new one randomly, to avoid biasing ourselves towards good performance on any single set. So when reporting, make sure to do proper splits and try not to overfit the testing set, though even the best people do that a little sometimes.

互动版:逐字朗读 + 针对本期提问 →