Week 8 · Multiclass Classification and Logits

English translation

Before practice

01 Distinguish multiple categories

Example scenario You want to classify a photo as a cat, dog, or bird. How should targets and predictions be represented so the model can distinguish these three categories?

Description
  • Assign a Class ID to each category and configure the model to output a score for each category.
  • Class IDs are identifiers. Their magnitude does not imply category order or distance.

02 Prediction confidence

Does choosing one category mean that the model distinguishes it confidently? When would you want to withhold a decision?

Description
  • The position of the largest score specifies the predicted Class
  • Score differences and a separate confidence assessment are needed. A Logit itself is not a probability

Practice

1. Check CSV inputs

  1. Run the data-loading cell. Print the shape, dtype, and first five rows of x_raw and x separately. Label the outputs before and after preprocessing.
  2. In the same cell, print the shape, dtype, and first five values of labels and y. Find where Labels are converted to the form required by Loss.
  3. Explain why you must reuse the training data's mean and scale to predict new samples, relating this to the scale of each Feature fed to the model.

2. Run baseline training

  1. Call train_classifier with Learning Rates η∈{0.001,0.01,0.1}\eta\in\{0.001,0.01,0.1\}, each for 1000 epochs. Keep the Seed and model initialization inside the function.
  2. Tabulate the final Loss and Training Accuracy returned by each call for each Learning Rate. Compare the two returned histories at the same epoch when examining changes in Loss and Accuracy.
  3. If Loss decreases while Accuracy remains unchanged, select and mark one such interval. Explain it using the difference between the Probability of the correct Class and the final Class decision. If no such interval exists, state that.

3. Interpret the output

  1. Find the prediction-results code cell in the practice notebook that contains logits=model(x). Keep the model trained in the preceding activity.
  2. Record sample indices 0–9 as 10 rows, with four columns: logits, probabilities, predicted label, and target label. Read the values from logits[:10], probability[:10], prediction[:10], and labels[:10], respectively.
  3. In a separate cell, print the return value of the torch.allclose call inside the provided assert statement; record the probability-sum check across all samples as true or false. Then compare logits.argmax(1) with probability.argmax(1) element by element and record whether they agree.
  4. Select up to 3 misclassified samples and record each sample index, target label, and predicted label. If there are none, record ‘none’.

Submission and saving

Include code, actual outputs and plots, and your own interpretation. Submit through KLAS; check the semester schedule and latest announcements for the deadline.

Filename: Name_StudentID_Week8.ipynb.

Restart the runtime, run all code from the top, then save.

Optional practice

When Class indices are passed as a Float Tensor

  1. After training the model, add a Code Cell at the bottom of the notebook and run the following code.
    with torch.no_grad():
        debug_logits = model(x)
    debug_target = y.float()
    print("original Target:", y.shape, y.dtype)
    print("Float Target:", debug_target.shape, debug_target.dtype)
    try:
        F.cross_entropy(debug_logits, debug_target)
    except (RuntimeError, ValueError) as error:
        print(type(error).__name__, str(error))
    print("integer Target Loss:", F.cross_entropy(debug_logits, y).item())
  2. Add a Text Cell below the output and record these two points.
    • The shape and dtype of both Targets, and the dtype required by the error message.
    • Why Loss cannot be computed when a Target holding one Class index per sample is supplied as a Float Tensor, even with the same indices.

    y.float() creates a new Tensor. The original y remains unchanged, so the last line computes Loss with integer Targets.