Week 6 · Find a line by reducing prediction error

English translation

Before practice

01 From equations to data

How does computing values from a line equation differ from finding a line equation from measurements?

Description
  • Previously you computed outputs from a known slope; now you will find a slope that explains the trend in the data.
  • This exercise fixes the intercept at 0 and adjusts only the slope, simplifying the set of lines allowed by the model.

Review related concepts ↗

02 Learning and assumptions

Can adjusting the slope repeatedly explain any data without error?

Description
  • Allowing only lines through the origin makes it difficult to accurately represent relationships that do not pass through the origin or data with a curved pattern.
  • Use Loss and Gradients to reduce error within the chosen model. Increasing the iteration count differs from changing the model's assumptions.

Practice

1. Regression through the origin

Each row of A contains an input value and the constant 1.

  1. In the data cell, print A and A_prime side by side and mark the selected columns.
  2. Explain the constant column's role in ŷ=wx\hat y=wx and ŷ=wx+b\hat y=wx+b. Choose the expression corresponding to this model's bias=False.
  3. Compare the Target of the sample with input x=0x=0 to the values the model can produce, and state whether there is an error that training cannot eliminate.

2. Training and records

  1. Call train_linear with Learning Rates η∈{0.001,0.01,0.1}\eta\in\{0.001,0.01,0.1\}, each for 1000 epochs. Keep the same Seed and model initialization inside the function.
  2. Plot the three Loss curves together and tabulate the final Weight and MSE for each Learning Rate. Explain the rate of Loss decrease with reference to the curves.
  3. In train_linear, immediately after loss.backward(), add code that prints model.weight.grad only during the first epoch. Relate the Gradient sign to the Weight update direction and explain why zero_grad() is needed before the next epoch.

3. Compare with the direct solution

  1. Print the values and Determinant of A_prime.T @ A_prime and determine whether its Inverse exists.
  2. In a three-row table, record torch.linalg.inv(A_prime.T @ A_prime) @ A_prime.T @ B_prime, torch.linalg.pinv(A_prime) @ B_prime, and the Weight trained with Learning Rate 0.01 for 1000 epochs. Also record the absolute difference between the directly computed solution and the training result.
  3. For x∈{0,1,2,3}x\in\{0,1,2,3\}, record Input, Target, prediction, and prediction−Target in one table.

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

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

Optional practice

When model and input dtypes differ

  1. After training the model, add a Code Cell at the bottom of the notebook and run the following code.
    print("model:", model.weight.dtype)
    for debug_input in [A_prime.double(), A_prime.float()]:
        print("Input:", debug_input.shape, debug_input.dtype)
        try:
            with torch.no_grad():
                print("Output:", model(debug_input).shape)
        except (RuntimeError, ValueError) as error:
            print(type(error).__name__, str(error))
  2. In a Text Cell, tabulate the dtype of each input and whether execution succeeded, and explain the cause in one sentence in terms of the model's dtype. Input values and shapes are identical in both calls.