liveBook · Manning

Linear regression

As explained in Deep Learning with Python, Third Edition

A transformation that preserves linear structure and can be represented by matrix multiplication.

A regression model predicts a continuous value rather than a class label. A linear prediction rule computes that value from weighted input features plus a bias: prediction = matmul(inputs, W) + b. The bias makes this an affine transformation; calling it a linear model is common shorthand. section 4.3 section 3.3.2

Start with two features

For an input point [x, y], two weights and one bias give prediction = w1 × x + w2 × y + b. A worked TensorFlow classifier trains this same rule on two blobs of points labeled 0 and 1, then thresholds its predictions at 0.5. That demonstrates the linear calculation, but it is a classification example—not a worked linear regression model for continuous targets. section 3.3.2 section 4.3

Listing 3.18 The forward pass function
def model(inputs, W, b):
    return tf.matmul(inputs, W) + b

The forward pass shows the complete weighted-input-plus-bias calculation in TensorFlow. section 3.3.2

To fit the worked classifier, compute mean squared error between predictions and targets, obtain gradients for W and b with GradientTape, and update both variables. The training loop runs for 40 steps. This shows how to train the linear rule from scratch, though its targets remain class labels. section 3.3.2

For house-price regression, a small neural network instead ends with a single Dense unit and no activation. Leaving that output unconstrained lets it predict values outside the 0–1 range imposed by a sigmoid. Its two preceding ReLU layers mean the whole network is not a standalone linear regression model. section 4.3.3

Practical checks for continuous-value prediction:

  • Use mean squared error as a common training loss and mean absolute error to report the typical size of prediction errors; classification accuracy does not apply. section 4.3.3 section 4.3.6
  • Scale input features independently when their ranges differ. With little data, prefer a small model and use K-fold validation to evaluate it more reliably. section 4.3.6

How Deep Learning with Python, Third Edition covers it

How deep it goes
Works through TensorFlow training of a linear classifier and a neural network’s regression output, but not standalone linear regression on continuous targets.
Examples
Uses separable 2D point classes for the linear training loop and house prices for continuous-value prediction.
What it assumes
Assumes basic tensors, variables, tensor operations, and gradients for the from-scratch training example.
What you can do after
Build it from scratch as a TensorFlow linear classifier; apply an unactivated Keras Dense output to a house-price regression network.
How it connects
Connects affine transformations and gradient descent to mean squared error, Dense layers, and scalar regression.

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 Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.