liveBook · Manning

Linear regression

As explained in Deep Learning with R, Third Edition

A linear transform is a mapping that preserves vector addition and scalar multiplication and can be represented by matrix multiplication.

Regression predicts a continuous value, such as a house price, rather than choosing a class label. A linear model starts with a simple prediction rule: multiply each input by a learned weight, add the results, then add a bias. For two inputs, that is prediction = w1 × x1 + w2 × x2 + b. Training adjusts the weights and bias so predictions better match known targets. section 4.3 section 3.3.2

model <- function(inputs, W, b) {
  tf$matmul(inputs, W) + b
}

This R/TensorFlow function expresses the prediction rule as matrix multiplication plus a bias: tf$matmul(inputs, W) + b. The same computation can produce a continuous score or serve as the starting point for a classifier. section 3.3.2

Strictly, matrix multiplication is a linear transform; adding b makes the computation an affine transform. Rotation and scaling are geometric examples of linear transforms. Stacking layers that perform only affine transforms still yields an affine transform, so additional layers need activation functions to model nonlinear relationships. section 2.3.5

Fit and inspect a simple model

For two synthetic clusters of points, initialize W and b, calculate predictions, measure mean squared error against the targets, and repeatedly update W and b using gradients. Plotting the resulting predictions shows a straight decision boundary when scores are split at 0.5. This is a linear classifier trained with squared error—not an example of predicting a continuous regression target. section 3.3.2 section 3.4.2

For continuous targets, mean squared error (MSE) is a common training loss, while mean absolute error (MAE) gives an interpretable evaluation measure; classification accuracy does not apply. A scalar regression model commonly ends with one unit and no activation, leaving its output range unconstrained. That does not make the whole model linear: the house-price example has two earlier layers with relu activations. section 4.3.3 section 4.3.6

Listing 4.24 Model definition
get_model <- function() {  #1
  model <- keras_model_sequential() |>
    layer_dense(64, activation = "relu") |>
    layer_dense(64, activation = "relu") |>
    layer_dense(1)
  model |> compile(
    optimizer = "adam",
    loss = "mean_squared_error",
    metrics = "mean_absolute_error"
  )
  model
}

Compare the final layer_dense(1) with the two preceding relu layers. The final prediction is unconstrained, but the complete house-price model is not a linear regression model. section 4.3.3

How Deep Learning with R, Third Edition covers it

How deep it goes
Works through weights, bias, squared-error training, and gradients for a classifier, but does not work through a standalone linear-regression fit.
Examples
Uses two clusters of synthetic points for a linear classifier and house prices for continuous-value regression.
What it assumes
Assumes familiarity with tensors, variables, operations, and gradients for the training loop; introduces regression as continuous-value prediction.
What you can do after
Build it from scratch as an affine predictor trained with squared error, and distinguish a linear output from an entirely linear model.
How it connects
Connects matrix multiplication and affine transforms to Dense layers, activation functions, mean squared error, 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 R, Third Edition. The section links point to the parts of the book it draws on.