Tensorflow. Nonlinear regression
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Nonlinear regression is about fitting a curve or surface when the relationship between inputs and outputs is not well described by a straight line. TensorFlow is a good fit for this because it lets you build flexible function approximators, optimize them with automatic differentiation, and train on arbitrary nonlinear patterns.
Create a Simple Nonlinear Dataset
A clear way to understand nonlinear regression is to generate data with a curved relationship and train a small neural network to approximate it.
This target is clearly nonlinear, so a plain linear model would struggle to match the curve.
Build a Small Keras Model
In TensorFlow, a multilayer perceptron is often the simplest nonlinear regressor. The hidden layers introduce nonlinear activation functions, which allow the model to represent curved relationships.
The final dense layer has no activation because regression usually predicts an unrestricted real-valued output.
Train and Evaluate the Regressor
Now fit the model and inspect the training behavior.
After training, run predictions for a few sample inputs:
That gives you a working nonlinear regression pipeline with only a few lines of TensorFlow code.
Why Hidden Layers Make It Nonlinear
A network with only one dense layer and no nonlinear activation is still just a linear model, no matter how you train it. The nonlinear part comes from activation functions such as relu, tanh, or sigmoid.
That is why this model can learn curves while a simple linear regressor cannot. The hidden layers transform the input into a richer representation before the output layer predicts the final numeric value.
For smooth scientific curves, tanh can sometimes work well. For many practical problems, relu is a solid default because it trains efficiently and is easy to scale.
Input Scaling Helps a Lot
Nonlinear regression models often train better if inputs are scaled. For small one-dimensional demos this may not look critical, but in real applications it can significantly affect optimizer stability and convergence speed.
If you scale during training, remember to apply the same transformation at inference time.
When a Neural Network Is Not the Best Tool
TensorFlow can solve nonlinear regression, but that does not mean a neural network is always the right first choice. If you know the functional form already, curve fitting with a domain-specific model may be easier to interpret.
Neural networks are most attractive when:
- the curve shape is unknown
- there are multiple interacting features
- you want one flexible model family for many problems
If interpretability is the main priority, a simpler nonlinear model may still be a better fit.
Common Pitfalls
One common mistake is building a regression model with no nonlinear activation in the hidden layers. That leaves you with a linear model even if the target relationship is curved.
Another is overfitting a tiny dataset with a network that is far too large. Validation loss is the signal to watch, not just the training loss.
Developers also sometimes forget to scale inputs or inspect the target range, which can make training unstable and predictions hard to interpret.
Finally, remember that regression targets are continuous. Using classification metrics or a classification-style output layer is the wrong setup for this problem.
Summary
- Nonlinear regression fits curved relationships rather than straight lines.
- In TensorFlow, a small neural network with nonlinear activations is a practical baseline.
- Hidden layers with activations are what make the model nonlinear.
- Input scaling and validation monitoring improve training stability.
- Use neural networks when you need flexibility, but do not ignore simpler nonlinear models when they fit the problem better.

