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






















