Course materials · AI Applications

Week 7 · Distinguish probability from classification criteria

English translation

Before practice

01 Numerical prediction and classification

Example scenario How do the desired model outputs differ between predicting tomorrow's temperature and deciding whether a device is operating normally?

Description
  • Temperature prediction produces a number; deciding normal versus abnormal produces a category. Set the model output and training criterion according to the desired result.
  • Binary Classification learns a score for choosing one of two categories. Logistic Regression is a basic model for this purpose.

02 Turn predictions into decisions

Example scenario A model gives a numerical likelihood of failure. How should the criterion for triggering an actual alarm be set?

Description
  • A Threshold is needed to convert the model's score into an alarm decision.
  • Consider both the cost of missed failures and unnecessary alarms. Distinguish the scoring model from the criterion for taking action.

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. Plot the three Loss curves together and tabulate the final Training Loss and Training Accuracy for each Learning Rate.
  3. Explain differences in the rate of Loss decrease by pointing to sections of the curves.

3. Interpret the output

  1. Choose one model trained in the previous activity and compute Probability once. Record the chosen Learning Rate. Apply Thresholds θ∈{0.3,0.5,0.7}\theta\in\{0.3,0.5,0.7\} separately to the same Probability.
  2. Record Threshold, Accuracy, and Positive prediction count in a three-row table.
  3. Choose up to 3 samples whose decisions change with the Threshold. Tabulate the sample index, Probability, target, and decisions at the three Thresholds. If no samples change decisions, state that.

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_Week7.ipynb.

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

Optional practice

When the BCE Target shape differs

  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)
    print("Logits:", debug_logits.shape)
    for debug_target in [y.squeeze(1), y]:
        print("Target:", debug_target.shape)
        try:
            print("Loss:", F.binary_cross_entropy_with_logits(
                debug_logits, debug_target).item())
        except (RuntimeError, ValueError) as error:
            print(type(error).__name__, str(error))
  2. In a Text Cell, tabulate the Logits shape, each Target shape, and execution success. Explain which axis squeeze(1) removed and how Logits and Target shapes must match for this Loss.