A4.3.3 · Machine learning approaches (HL only)
A4.3.3 · 机器学习方法(仅 HL)
The Role of Hyperparameter Tuning
超参数调优的作用
A model is never finished the first time it trains. Its performance is measured against a benchmark, and a set of dials fixed before training, the hyperparameters, are turned and re-tested until that performance is as good as it can be.
模型第一次训练完并不算完成。它的表现要对照一个基准来衡量,而一组在训练之前就定好的旋钮,也就是超参数,会被反复调整、反复测试,直到表现达到最好。
Linking to TOK: when a model scores higher on a metric, has it truly learned more, or only become more functional on that one task? A model can score well while still failing badly on cases the training data barely covered.
联系 TOK(知识论): 当一个模型在某个指标上得分更高时,它是真的学到了更多,还是只是在这一项任务上变得更管用了?一个模型完全可能在某个指标上得分很高,却在训练数据几乎没覆盖到的情况上严重失败。
Explainer
讲解
Parameters vs hyperparameters
参数与超参数
A parameter (a weight, a bias) is a value the training process works out on its own, by fitting the model to the data. A hyperparameter is a value set BEFORE training begins, that controls how the learning happens. Hyperparameter tuning means trying out different hyperparameter values and comparing how well each one performs, to land on the combination that works best.
参数(权重、偏置)是模型在训练过程中从数据中学到的值。超参数是在训练开始之前设定好的值,它控制着学习是如何进行的。超参数调优是尝试不同的超参数值、比较表现,从而找出效果最好的组合的过程。
| Algorithm | A hyperparameter it needs set in advance |
|---|---|
| K-Nearest Neighbours | The number of neighbours, k |
| Decision tree | The maximum depth of the tree |
| K-means clustering | The number of clusters, k |
| Neural network | The learning rate, the number of hidden layers, the activation function |
| 算法 | 它需要事先设定的一个超参数 |
|---|---|
| K 最近邻 | 邻居的数量 k |
| 决策树 | 树的最大深度 |
| K-均值聚类 | 簇的数量 k |
| 神经网络 | 学习率、隐藏层的数量、激活函数 |
Try it yourself
动手试试
Build a confusion matrix
搭一张混淆矩阵
A factory camera checks bottles for cracks. A positive result means "the model flags this bottle as cracked". Type in counts for a batch of 100 bottles and watch the four evaluation metrics update.
工厂里的一台相机检查瓶子有没有裂纹。阳性结果表示"模型把这个瓶子标记为有裂纹"。为一批 100 个瓶子输入各项计数,看看四个评估指标如何随之变化。
A missed crack (false negative) reaches a customer; a good bottle wrongly pulled (false positive) wastes stock. Try the two presets: one keeps accuracy respectable while recall collapses, the other keeps accuracy respectable while precision collapses. Accuracy alone hides both problems.
漏掉的裂纹(假阴性)会流到顾客手中;被误判的好瓶子(假阳性)会浪费库存。试试这两个预设:一个让准确率看起来还行,但召回率崩了;另一个让准确率看起来还行,但精确率崩了。单看准确率会把这两个问题都藏起来。
Explore
探索
Underfit, good fit, overfit
欠拟合、恰好合适、过拟合
Model complexity is itself controlled by hyperparameters (tree depth, number of neighbours, number of hidden layers). Drag along the complexity scale and watch what happens to training accuracy versus accuracy on new, unseen data.
模型的复杂度本身也是由超参数控制的(树的深度、邻居数量、隐藏层数量)。沿着复杂度刻度拖动,看看训练准确率与在新的、未见过的数据上的准确率会发生什么变化。
Check your understanding
检验你的理解
A screening tool flags patients who may have a rare disease. Both missing a real case and raising a false alarm matter. Which single metric is the best overall summary?
一个筛查工具用来标记可能患有某种罕见疾病的患者。漏掉一个真实病例和发出一次假警报都很要紧。哪一个单一指标最能概括整体表现?
Common mistake
常见错误
RecurringAnswers reach for accuracy and stop there, even when the data set is imbalanced (the rare class only makes up a small share of cases). A model that always predicts "no disease" can score 95% accuracy on a data set where disease is rare, while catching zero real cases. Precision and recall exist precisely because they look at the positive class specifically, not the whole data set at once.
反复出现答案只用准确率就止步了,即便数据集是不平衡的(稀有的那一类只占很小一部分案例)。一个永远预测"没有患病"的模型,在患病者稀少的数据集上完全可以得到 95% 的准确率,却一个真实病例都没抓到。精确率和召回率之所以存在,正是因为它们专门看阳性类,而不是一次性看整个数据集。
Wrong: "The model is 95% accurate, so it is performing very well."
错误: "这个模型的准确率是 95%,所以表现非常好。"
Right: "The model is 95% accurate, but the disease occurs in only 5% of cases, so a model that never predicts disease would also score 95%. Its recall must be checked separately to see whether it actually detects real cases."
正确: "这个模型的准确率是 95%,但这种疾病只出现在 5% 的病例中,所以一个从不预测患病的模型同样能得到 95% 的准确率。必须单独检查它的召回率,看它是否真的能检测出真实病例。"
Revision questions
复习题
Specimen-paper style. Write an answer, then reveal the marking points and mark yourself.
采用样卷风格。写下答案,然后展开评分要点给自己打分。
[2]State the difference between a parameter and a hyperparameter.
[2]说出参数和超参数之间的区别。
Marking points
评分要点
- A parameter is a value the training process works out for itself, by fitting to the data (e.g. a weight).
- A hyperparameter is set before training begins and controls how learning happens (e.g. number of neighbours, learning rate).
- 参数是训练过程中从数据中学到的(例如一个权重)。
- 超参数是在训练开始之前设定的,控制学习如何进行(例如邻居数量、学习率)。
[4]A model correctly predicts 60 of 75 actual positives, and out of 90 total positive predictions, 60 are correct. Calculate the precision and the recall, showing your working.
[4]一个模型在 75 个真实阳性中正确预测了 60 个,而在总共 90 个阳性预测中,有 60 个是正确的。计算精确率和召回率,写出过程。
Marking points
评分要点
- Precision = TP / (TP + FP) = 60 / 90 = 0.67 (2 sf).
- Recall = TP / (TP + FN) = 60 / 75 = 0.80.
- 精确率 = TP / (TP + FP) = 60 / 90 = 0.67(2 位有效数字)。
- 召回率 = TP / (TP + FN) = 60 / 75 = 0.80。
[3]Explain the difference between overfitting and underfitting.
[3]解释过拟合和欠拟合之间的区别。
Marking points
评分要点
- Overfitting: the model fits the training data (including its noise) too closely and performs poorly on new data.
- Underfitting: the model is not complex enough to pick up on the real trend in the data, so neither the training results nor its results on new cases are any good.
- Both are judged by comparing training performance against performance on unseen data.
- 过拟合:模型对训练数据(包括其中的噪声)拟合得过于紧密,在新数据上表现不佳。
- 欠拟合:模型太简单,学不到底层规律,因此在训练数据和新数据上都表现不佳。
- 两者都是通过比较训练表现与在未见数据上的表现来判断的。
[3]Explain why hyperparameter tuning is necessary even after a model has been trained once.
[3]解释为什么即使模型已经训练过一次,超参数调优仍然是必要的。
Marking points
评分要点
- The first choice of hyperparameters is rarely optimal.
- Different values change how well the model generalizes, not just how it performs on the training run.
- Systematic tuning (trying combinations, comparing a metric) finds a better-performing combination than guessing once.
- 第一次选定的超参数很少是最优的。
- 不同的取值会改变模型的泛化能力,而不只是它在这一次训练中的表现。
- 系统性的调优(尝试各种组合、比较某个指标)能找到比单凭猜测更好的组合。
Flashcards
记忆卡片
Active recall for this statement’s key terms.
针对本条考纲陈述关键术语的主动回忆练习。
Keep this statement’s practice with you
把这条考纲陈述的练习保存下来
Free account: save your progress and unlock every revision question for A4.3.3, and every other IB CS 2027 statement.
免费账户:解锁 A4.3.3 以及所有其他 IB CS 2027 考纲陈述的每一道复习题,并同步你的学习进度。