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.

Linear regression predicts a continuous value using a linear model of the inputs. A typical prediction combines a matrix product with a bias: prediction = input %*% W + b. The matrix product is a linear transform; adding the bias makes the full computation an affine transform. section 2.3.5 section 4.3

A model with two input features

For an input point [x, y], a model with two weights and one bias computes w1 * x + w2 * y + b. The same affine calculation can produce a continuous prediction or serve as the score of a classifier; the target and task determine how it is used. section 3.3.2 section 4.3

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

This R function uses TensorFlow to compute the affine prediction. It appears in a classifier example, but the calculation is also the linear-model component needed for regression. section 3.3.2

For regression, mean squared error (MSE) measures squared differences between predictions and targets and can be minimized during training. Mean absolute error (MAE) is a useful evaluation metric: it measures the average absolute difference in the target’s units. Classification accuracy is not a regression metric. section 4.3.3 section 4.3.6

A single output unit without an activation is a common choice for scalar regression because it does not restrict predictions to a fixed range. But a linear output layer does not make the entire model linear: preceding layers with activations can learn nonlinear transformations. Without activations, stacking affine layers is equivalent to using one affine layer. section 4.3.3 section 2.3.5

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
}

This house-price model uses a linear output, MSE, and MAE. Its two ReLU hidden layers make it a neural regression model rather than a worked example of linear regression. section 4.3.3

When features have different ranges, scale each feature independently. With little training data, a small model can help limit overfitting, and K-fold validation can help evaluate it reliably. section 4.3.6

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains affine computation and regression losses, but works through a linear classifier and a nonlinear neural regression model rather than linear regression itself.
Examples
Uses a two-feature classifier and a house-price model with a linear output to illustrate related computations.
What you can do after
Evaluate and understand how an affine prediction relates to a regression output and its loss.
How it connects
Relates affine transforms to dense layers, activation functions, mean squared error, and mean absolute error.

Also explained in

Compare how 23 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.