iClicker: Lecture 7 recap
Let \(z=\mathbf w^{\mathsf T}\mathbf x+b\) and \(p=\sigma(z)=1/(1+e^{-z})=P(y=1\mid\mathbf x)\).
Which statements are true? Select all that apply.
- A. If \(z=0\), then \(p=0.5\); increasing \(z\) increases \(p\).
- B. The sigmoid converts the score directly into a class label.
- C. For a true label \(y=1\), increasing \(p\) from \(0.6\) to \(0.9\) reduces log loss.
- D. A correct class prediction always has zero log loss.
- E. Mean squared error gives larger errors disproportionately more weight than smaller errors.
Logistic loss in terms of the score
Fix a review whose true sentiment is positive (\(y=1\)).
\[z=\mathbf w^{\mathsf T}\mathbf x+b,
\qquad p=P(y=1\mid\mathbf x)=\sigma(z)=\frac{1}{1+e^{-z}}\]
Substitute the sigmoid into the familiar log loss:
\[L=-\log(p)=-\log\left(\frac{1}{1+e^{-z}}\right)
=\log(1+e^{-z})\]
Increasing \(z\) increases the true-class probability and reduces loss.
Linear SVM and hinge loss
Both models compute \(z=\mathbf w^{\mathsf T}\mathbf x+b\).
- Same class decision rule: positive scores favour class 1; negative scores favour class 0.
- Different fitting losses: they can learn different coefficients and biases.
For this positive review (\(y=1\)):
\[L_{\text{logistic}}=\log(1+e^{-z}),
\qquad L_{\text{hinge}}=\max(0,1-z)\]
Hinge loss remains positive for \(0<z<1\), even though the class prediction is correct.
Hinge loss vs. logistic loss
Hinge loss and support vectors
- Hinge loss focuses fitting on examples that are misclassified or close to the decision boundary, connecting to the support vectors we saw earlier.
- For this positive review, once \(z \geq 1\), the loss is zero: pushing its score higher brings no further benefit.
Exponential loss
For the same positive review (\(y=1\)), another alternative is:
\[L_{\text{exponential}}=e^{-z}\]
- Negative scores are wrong for this review; their penalty grows exponentially as \(z\) decreases.
- Positive scores reduce the loss toward zero, without reaching zero at any finite score.
- Exponential loss connects to AdaBoost, coming in DSCI 573.
Comparing classification losses
Hinge loss reaches zero at \(z=1\). Exponential loss penalizes strongly wrong scores most sharply.
Classification losses: Behaviour and uses
| Emphasis |
True-class probability |
Correct class with sufficient separation |
Strong penalty for confidently wrong scores |
| Zero loss (\(y=1\)) |
Approaches zero |
Zero at \(z\geq1\) |
Approaches zero |
| Typical use |
Logistic regression; probabilities |
SVMs; class decisions |
AdaBoost and related boosting methods |
Loss choice usually comes through choosing a model or algorithm.
iClicker: Correct prediction, positive loss?
A truly positive review receives score \(z=0.5\).
Which statements are true? Select all that apply.
- A. Its predicted class is positive.
- B. Its hinge loss is zero.
- C. Its hinge loss is 0.5.
- D. Increasing this review’s score from \(0.5\) to \(1.5\) reduces both its hinge loss and its logistic loss.
Fitting loss and evaluation measures
How we learn parameters and how we assess predictions
Fitting versus evaluation
| Question |
Which parameters should the model learn? |
How well does the model predict held-out examples? |
| Data |
Training data |
Validation or test data |
| Measure |
The estimator’s fitting loss |
A measure chosen for the application |
| Logistic regression example |
Minimize log loss plus regularization |
Assess accuracy, log loss, or both |
The fitting loss and evaluation measure can be the same or different.
Evaluating logistic regression
Every candidate is fitted using log loss plus regularization.
We can compare candidates on validation data using:
- Accuracy: how often is the predicted class correct?
- Log loss: how much probability is assigned to the true class?
Changing the evaluation measure changes how we rank candidates, not how each candidate fits its parameters.
Accuracy and log loss capture different changes
For a truly positive review, compare two predictions:
| 0.20 |
Negative |
No |
1.609 |
| 0.30 |
Negative |
No |
1.204 |
- Accuracy is unchanged: both class predictions are wrong.
- Log loss improves: the true class receives more probability.
Your turn: Which model wins?
Two logistic-regression candidates, each fitted using log loss plus regularization.
| 0.1 |
0.96 |
0.12 |
| 10 |
0.97 |
0.18 |
- Which candidate wins if we evaluate using accuracy? Using log loss?
- Does changing the evaluation measure change either candidate’s fitting loss?
Course evaluations (~10 minutes)
- The online course evaluations are up, it’ll be great if you can fill them in when you get a chance.
Cross-entropy loss
How much probability did the model assign to the true class?
One-hot targets and cross-entropy
True species: Chinstrap. Class order: Adelie, Chinstrap, Gentoo.
\[\mathbf y=\begin{bmatrix}0 \\ 1 \\0\end{bmatrix},
\qquad \hat{\mathbf p}=\begin{bmatrix}0.1\\0.8\\0.1\end{bmatrix}\]
\[L=-\sum_{k=1}^{K} y_k\log(\hat p_k)
=-\log(\hat p_{\text{true class}})\]
\[L=-[0\log(0.1)+1\log(0.8)+0\log(0.1)]\approx0.223\]
Binary and multiclass log loss
For two classes, the target indicators are \(y\) and \(1-y\):
\[L=-[y\log(\hat p)+(1-y)\log(1-\hat p)]\]
For \(n\) examples and \(K\) classes, average the losses:
\[\overline L=-\frac1n\sum_{i=1}^n\sum_{k=1}^K
y_{ik}\log(\hat p_{ik})\]
- High probability for the true class gives low loss.
- A confident wrong prediction can receive a very large loss.
iClicker: Is a log loss of 1.10 good?
A three-class model has mean validation log loss 1.10. (\(-log(1/3) = 1.09\))
Which statement is best supported?
- A. The model is useful because 1.10 is close to zero.
- B. The model performs about the same as this baseline.
- C. The model has about 110% classification error.
- D. We cannot compute the baseline loss without its accuracy.
Penguins: A complete prediction pipeline
penguin_pipeline = make_pipeline(
SimpleImputer(strategy="median"),
StandardScaler(),
LogisticRegression(solver="lbfgs", max_iter=1000),
)
- Reserve a test set first.
- Compare with
DummyClassifier() using training-data CV.
- Keep imputation and scaling inside the pipeline.
- Inspect both accuracy and log loss. Leave test data untouched.
Penguins: Validation results
| Model |
|
|
| Class-proportion baseline |
0.442 |
1.049 |
| Logistic regression |
0.977 |
0.068 |
- Accuracy evaluates class decisions.
- Log loss evaluates probability predictions.
- Both improve relative to the class-proportion baseline here.
Same class prediction, different probabilities
| A |
0.40 |
0.35 |
0.25 |
| B |
0.90 |
0.05 |
0.05 |
- What class does each model predict?
- Which has lower log loss if the true class is Adelie?
- Which has lower log loss if it is Chinstrap?
- Can accuracy distinguish the models on this example?