Tensorflow on simple linear regression
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Simple linear regression is one of the easiest ways to understand how TensorFlow trains a model. The goal is to learn a straight-line relationship of the form y = mx + b, and TensorFlow handles the gradient calculations and optimization for you.
The Regression Problem
In simple linear regression, there is one input feature and one target value. The model learns two parameters:
- '
m, the slope' - '
b, the intercept'
Given data points, training adjusts those parameters so the predicted values are close to the observed targets.
A small synthetic dataset looks like this:
This roughly follows y = 2x + 1.
Build It with Keras
The easiest TensorFlow solution is a one-layer Keras model.
The Dense(1) layer is enough because a linear neuron with one input already represents mx + b.
Inspect the Learned Parameters
After training, you can inspect the learned slope and intercept.
On this dataset, those values should end up close to 2 and 1.
This is one of the nicest things about starting with linear regression: the model is simple enough that the parameters are directly interpretable.
The Loss Function and Optimizer
The standard loss for linear regression is mean squared error.
This penalizes larger prediction errors more heavily than smaller ones.
The optimizer updates the parameters using gradients. In this example, stochastic gradient descent is enough:
TensorFlow computes the gradients automatically during training, so you do not have to derive update formulas by hand.
Manual TensorFlow Version
If you want to see the mechanics more directly, you can train the same model with tf.Variable and GradientTape.
This version makes the training loop explicit and is useful for understanding what Keras is automating for you.
When Linear Regression Is a Good Baseline
Even in larger machine learning projects, a linear model is a useful baseline because:
- it trains quickly
- it is interpretable
- it tells you whether a simple linear relationship already explains much of the data
If a linear model performs surprisingly well, it may save you from building a more complex system too early.
Common Pitfalls
- Feeding inputs with the wrong shape when Keras expects one feature per sample.
- Using too large a learning rate and causing training to diverge.
- Expecting a linear model to fit nonlinear data well.
- Treating TensorFlow as if it were only for deep networks when simple models are also valid.
- Ignoring the learned weights instead of checking whether they make sense for the problem.
Summary
- Simple linear regression in TensorFlow is just a one-neuron linear model.
- Keras makes it easy with
Dense(1)and mean squared error loss. - You can inspect the learned slope and intercept after training.
- A manual
GradientTapeloop helps explain what TensorFlow is doing internally. - Linear regression is a strong baseline because it is fast, interpretable, and easy to debug.
Related reading
- TensorFlow on Windows Couldn't open CUDA library cudnn64_5.dll
- Tensorflow One Hot Encoder?
- Tensorflow OOM on GPU
- Tensorflow Optimizers - multiple loss values passed to minimize?
- Tensorflow on windows - ImportError DLL load failed The specified module could not be found
- TensorFlow on Windows not a supported wheel on this platform error
- Tensorflow One Hot Encoder?
- TensorFlow Opening log data written by SummaryWriter
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.