liveBook · Manning

Linear regression

As explained in Math and Architectures of Deep Learning

A function that preserves addition and scalar multiplication between vector spaces, as in matrix multiplication.

Linear regression is a model for quantitative estimation: it maps an input vector to a predicted numeric output. With weights w and bias b, its prediction is ŷ = wᵀx + b = Σⱼwⱼxⱼ + b. Training estimates the weights and bias from observed input–output pairs by reducing prediction error. It is suitable when the underlying function is simple; more complex relationships require nonlinear models. section 3.5.1 section 1.1

The weighted-sum portion of the model is a linear transformation: a transform preserves linear combinations, so transforming a weighted combination of vectors gives the same weighted combination of their transformed versions. In finite-dimensional spaces, every matrix-vector multiplication is such a transformation. The added bias makes the full prediction formula an affine shift rather than a matrix multiplication alone. section 2.10.1 section 2.10.2 section 3.5.1

A linear regression workflow is:

  • Represent each observation as an input vector xᵢ paired with an observed target yᵢ. section 3.5.1
  • Compute predictions with wᵀxᵢ + b and measure squared error against each target. section 3.5.1
  • Sum errors across a batch, then adjust weights and bias in the direction that reduces error. section 3.5.1
  • Scale features to a common range when their values have very different magnitudes. section 8.5

Learning a one-variable prediction

A synthetic target follows y = 1.5x + 2.73 with added noise. Starting from current w and b, repeatedly predict y_pred = wx + b, compute mean squared error, calculate gradients for w and b, and update both parameters. The learned result closely approaches y_pred = 1.50059x + 2.746823, illustrating how fitting recovers a numeric relationship from noisy observations. section 3.5.1

Predicting weekly demand

An e-commerce demand predictor uses previous-week sales, discount fraction, and weeks until the next holiday as an input vector, then predicts the number of phones sold in the upcoming week. This is a regression problem because its target is a quantity rather than one of several prespecified classes. section 8.5 section 1.5

Regression and classification can be combined but solve different output problems. A regressor emits a value; thresholding that value can produce a class decision. In object detection, regressor components similarly refine bounding-box coordinates while classifier components identify object classes. section 1.5 section 11.3.1 section 11.4.3

Listing 3.1 PyTorch linear model (closed-form gradient formula needed)
x = 10 * torch.randn(N)                              #1
  = 1.5 * x + 2.73 _obs = y + (0.5 * torch.randn(N)) #2

for step in range(num_steps):
    y_pred = w*x + b                                 #3
    mean_squared_error = torch.mean(
            (y_pred - y_obs) ** 2)                   #4

    w_grad = torch.mean(2 * ((y_pred - y_obs)* x))
    b_grad = torch.mean(2 * (y_pred - y_obs))        #5

    w = w - learning_rate * w_grad
    b = b - learning_rate * b_grad                   #6

print("True function: y = 1.5*x + 2.73")
print("Learned function: y_pred = {}*x + {}".format(w[0], b[0]))

This implementation places the prediction formula, mean squared error, gradients, and parameter updates side by side. section 3.5.1

Table 8.1 Sample training data for demand prediction

Previous week sales

Discount fraction (%)

Weeks to next holidays

Number of units sold

76,440

63

2

94,182

41,512

50

3

51,531

77,395

77

9

95,938

…

…

…

…

21,532

70

4

28,559

This table makes the multi-feature demand-prediction inputs and numeric target concrete. section 8.5

How Math and Architectures of Deep Learning covers it

How deep it goes
Derives the weighted-sum-plus-bias model, squared-error objective, and gradient updates, while deferring nonlinear-model details.
Examples
Uses noisy y = 1.5x + 2.73 data and weekly mobile-phone demand from sales, discounts, and holiday timing.
What it assumes
Introduces regression from input–output observations while relying on vectors, dot products, gradients, and matrix multiplication.
What you can do after
Build it from scratch with PyTorch by computing predictions, mean squared error, gradients, and weight-and-bias updates.
How it connects
Connects regression to linear transformations, matrix-vector multiplication, gradient descent, classification, and bounding-box localization.

Also explained in

Compare how 22 books explain linear regression →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Math and Architectures of Deep Learning. The section links point to the parts of the book it draws on.