训练机器学习模型
我的理解
机器学习的训练过程本质是自动化调试:对每个训练样本,只做一次“温和的微调”而非强制纠正,目的是在所有样本上累积出稳定的泛化能力,而非死记硬背。这种渐进式调整可以防止过拟合,但也带来“嘈杂”——一张热狗图片里的绿色篮子可能被模型错误地与热狗绑定,需要大量对比样本才能分离。最关键的认知是:模型并不“理解”数据,它只是在寻找能最小化损失函数的对应关系——即使输入是随机数也会找出某种模式。理解这一点,构建者就能预判模型的意外行为并设计更合理的验证机制。
相关链接
- Ch02-L03 理解常见陷阱 偷懒 — 模型寻找损失最小化对应的本质,是它走捷径、给出省力答案的底层根源
- Ch05-L04 研究 生成式 AI 建立在共识之上 — 训练依赖高频模式积累正是 GenAI 建立在共识之上的机制基础
- Ch07-L02 预训练大语言模型 — 本节的通用训练机制在 LLM 预训练中的具体实例化
原文
Lesson 52 of 68 训练机器学习模型 / Training a Machine Learning Model
对不熟悉机器学习(ML)的读者来说,模型训练过程与调试程序非常相似。设想我们有一个程序和一批输入数据,对于每一个输入,我们都知道它对应的预期输出,这些数据就被称为训练数据。我们把一个输入喂给程序,检查它的输出是否与预期一致。如果不一致,就调整程序以让两者匹配。模型训练与之类似,关键区别在于这种调整过程是完全自动的,无需人工去手动修复问题。
当我们在程序中修复一个 bug 时,往往又会引入另一个 bug:修完之后,程序在某个输入上表现良好,却又在另一个输入上出错。训练样本之间的这种相互关联性,在机器学习模型训练中同样存在。而且模型训练还更为复杂,因为模型可能会出现“过拟合”——即只是死记硬背训练样本以通过训练“考试”,但面对不同的输入时却无所适从。
为了应对这些挑战,我们不会激进地修正模型行为,让它一次就把当前样本完全答对。相反,我们只是轻轻地把它朝正确答案的方向推一下,然后继续处理下一条训练样本。通过在整个训练数据上反复进行这种温和的修正,我们可以帮助模型真正理解任务,而不只是记住样本。这种做法在模型的学习中取得了平衡,既降低了过拟合的风险,也提升了它在新的、未见过的输入上的表现。
我们以一个简单的例子来说明:训练一个“热狗 vs. 非热狗”分类器。我们有一批训练数据,由若干张图片组成,每张图片都被标注为热狗或非热狗。我们把一张图片喂给模型——假设这是一张热狗的照片——模型会给出一个输出。例如,它可能会说这张图片是热狗的概率为 30%。这时,我们按照数学方法去调整模型内部参数,使得下次它再看到同一张图片时,输出的“是热狗”的概率可能从 30% 变为 50%。我们在不同的图片上反复进行这种温和的推动。当模型多次看到足够多的此类图片之后,就能够正确分类其中的大多数。
由于前面提到的相互关联性,这种推动过程可能会噪声很大、起伏不定。例如,一张热狗的训练图片中,热狗可能放在一个绿色篮子里。当我们让模型把这张图片预测为热狗时,模型可能会错误地把绿色篮子和热狗联系起来,导致它把另一张只有空绿色篮子的图片也预测成热狗。只有在对这两张图片都进行多轮推动之后,模型才会逐渐学到“篮子”与“热狗”是相互独立的,从而正确分类两张图片。有时,受模型能力所限,可能无法同时把两类图片都分对。模型也可能始终把这两张图片都预测为热狗,这就反映出它在学习过程中的某种局限。
解读机器学习模型工作机制的一种方式是“寻找对应关系(finding correspondence)”这一概念。在这一过程中,模型并不像人类理解世界那样去“理解”数据,而是去寻找能够优化损失函数的模式与对应关系。事实上,即便你把一组随机数作为训练数据喂给机器学习模型,它仍然会在这些数据中找到某种对应关系。这也凸显出:模型的目标是最小化损失函数,而非对底层数据获得深层次的理解。
English Original
For readers not familiar with Machine Learning (ML), the model training process is similar to debugging a program. Imagine we have a program and a bunch of inputs. For each input, we know the expected output. This is called the training data. We feed an input into the program and check whether the output matches the expected output. If not, we tweak our program to make the two match. Model training is similar, with the key difference that the tweaking process is fully automatic, so there’s no need for humans to fix issues manually.
When we fix a bug in a program, we often introduce another bug. The program might perform well on one input but get another wrong after the fix. This interconnectivity between training examples is also present in ML model training. Model training is even more complicated because it may “overfit,” meaning it memorizes the training examples to pass the training “exams” but doesn’t know what to do with different inputs.
To address these challenges, we don’t correct the model’s behavior aggressively to get the example right in one shot. Instead, we nudge it gently in the direction of the correct answer and then move on to the next training example. By making these soft corrections repeatedly across the entire training data, we can help the model truly understand the task rather than just memorizing the examples. This approach balances the model’s learning, reducing the risk of overfitting and improving its performance on new, unseen inputs.
Let’s take a simple example of training a hotdog vs. not-hotdog classifier. We have a bunch of training data consisting of images labeled as either hotdog or not-hotdog. We feed an image into our model—let’s say it’s a photo of a hotdog—and the model gives an output. For instance, it might say there’s a 30% probability that the image is a hotdog. In this case, we follow the math and tweak the internals of the model so that next time it sees the same image, it might output a 50% probability of being a hotdog instead of 30%. We repeat this soft nudging process on different images. When the model sees enough of these images multiple times, it will be able to correctly classify the majority of them.
Due to the interconnectivity we mentioned earlier, this nudging process can be noisy and bumpy. For example, a training image of a hotdog might show the hotdog lying in a green basket. When we nudge the model to predict this image as a hotdog, the model might mistakenly associate the green basket with the hotdog, leading it to predict another image of an empty green basket as a hotdog. Only after several rounds of nudging both images will the model learn that the basket is separate from the hotdog and correctly classify both. Sometimes, depending on the model’s capabilities, it might not be possible to get both classifications right. The model might always output hotdog for both images, indicating a limitation in its learning process.
One way to interpret how machine learning models work is through the concept of “finding correspondence.” In this process, the model doesn’t seek to “understand” the data in the way humans understand the world. Instead, the model finds patterns and correspondences that optimize a loss function. In fact, even if you provide ML models with a set of random numbers as training data, they will still find correspondences in those data. This highlights that the model’s goal is to minimize the loss function rather than to achieve a deep understanding of the underlying data.