Linear regression is the clearest bridge between basic Machine Learning and neural networks. It predicts a continuous value by learning the straight-line relationship that best describes the training data.
Suppose a benchmark records sorting comparisons and execution time. Measurements vary, but the overall trend can be represented by a line:
For one input feature, the model is:
The model learns the overall relationship instead of memorizing each row.
A single Linear neuron calculates the same equation:
In Deep Netts this is a 1 -> 1 network: one input, no hidden layers, and one output with Linear activation. It is the smallest useful neural network.
For sample \(i\), the residual is:
Mean Squared Error averages squared residuals:
Squaring prevents positive and negative errors from cancelling and penalizes large misses more strongly.
The runnable Sorting Execution Time walkthrough predicts execution_time_ms from comparison_count.
TabularDataSet<MLDataItem> dataSet = DataSets.readCsv(
DATASET_PATH, NUM_INPUTS, NUM_OUTPUTS, true, ",");
dataSet.shuffle(42);
TrainTestSplit split = dataSet.trainTestSplit(0.8);
DataSet<MLDataItem> trainingSet = split.getTrainingSet();
DataSet<MLDataItem> testSet = split.getTestSet();
The fixed seed makes the split reproducible. The test set remains unseen until evaluation.
Standardizer standardizer = new Standardizer(trainingSet);
standardizer.apply(trainingSet);
standardizer.apply(testSet);
Fit preprocessing on training data only. Applying test statistics during training would leak information and make evaluation look more reliable than it is.
FeedForwardNetwork neuralNet = FeedForwardNetwork.builder()
.addInputLayer(NUM_INPUTS)
.addOutputLayer(NUM_OUTPUTS, ActivationType.LINEAR)
.lossFunction(LossType.MEAN_SQUARED_ERROR)
.randomSeed(42)
.build();
There is no hidden nonlinear transformation, so the network learns \(\hat{y}=wx+b\).
neuralNet.getTrainer()
.setStopEpochs(3000)
.setLearningRate(0.001f)
.setOptimizer(OptimizerType.SGD)
.setShuffle(true);
neuralNet.train(trainingSet);
The learning rate controls update size. Too large can make loss unstable; too small can make learning unnecessarily slow.
RegressionMetrics metrics = (RegressionMetrics) neuralNet.test(testSet);
System.out.println(metrics);
neuralNet.setNormalizer(standardizer);
float comparisonCount = 100_000;
float predictedExecutionTime = neuralNet.predict(comparisonCount)[0];
Attaching the fitted normalizer lets raw future inputs receive the same transformation used during training.
| Metric | Meaning |
|---|---|
| \(R^2\) | Proportion of target variance explained by the model |
| MSE | Average squared error |
| RMSE | Error magnitude in target units |
| MAE | Average absolute error |
| Maximum error | Largest individual prediction miss |
Training loss and held-out metrics answer different questions. Use the test metrics to judge generalization.
With \(m\) features, the model becomes:
The Computer Hardware Performance walkthrough uses six inputs:
MYCT, MMIN, MMAX, CACH, CHMIN, CHMAX -> PRP
FeedForwardNetwork neuralNet = FeedForwardNetwork.builder()
.addInputLayer(6)
.addOutputLayer(1, ActivationType.LINEAR)
.lossFunction(LossType.MEAN_SQUARED_ERROR)
.randomSeed(42)
.build();
The 6 -> 1 network still learns a linear relationship, now represented by a plane or hyperplane rather than a two-dimensional line.
Training repeats:
For weight \(w\):
\(\eta\) is the learning rate. The gradient describes how the loss changes when the weight changes.
Linear regression cannot bend around a nonlinear relationship. Hidden layers with nonlinear activations change the model family:
The House Price Prediction walkthrough demonstrates a 1 -> 16 -> 8 -> 1 nonlinear regression network.
Looking for complete projects? Browse all Regression examples โ
Turn a weighted linear score into a binary-class probability and decision boundary.
Was this helpful?
Thank you!