Neural network for square x2 approximation
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Approximating x^2 with a neural network is a small problem, but it is a useful one because it isolates the core idea of supervised learning: fit a nonlinear mapping from examples. Since the target function is known exactly, you can focus on data scaling, model capacity, and training behavior instead of application noise.
Why a Network Can Learn x^2
A feed-forward network with nonlinear activations can approximate smooth functions over a bounded interval. For x^2, the task is regression: input one number, output one number.
The important detail is the interval. A network trained only on [-1, 1] may perform well there and fail badly on [-100, 100]. Function approximation is local to the data distribution unless you intentionally train for broader coverage.
For this problem, a small multilayer perceptron is enough:
- one scalar input
- one or two hidden layers
- nonlinear activation such as
TanhorReLU - one scalar output
Because x^2 is smooth and symmetric, the network does not need to be deep. The training setup matters more than model size.
A Minimal PyTorch Example
The following script generates training data, fits a small network, and prints a few predictions:
This is runnable as-is if PyTorch is installed. The predicted values should be close to 4.0, 0.25, 0.0, 2.25, and 4.0.
Why Normalization Helps
Even simple functions benefit from scaled inputs. Large raw values produce larger targets, larger gradients, and harder optimization. When the goal is to demonstrate learning rather than stress-test numeric range, normalize first.
For example, if your real inputs live in [0, 1000], you can divide by 1000 during training and rescale later:
This keeps the optimization problem well-behaved. It also makes the network spend capacity learning shape rather than raw magnitude.
Interpreting the Result
A trained network does not "discover the formula" in symbolic form. It learns weights that approximate the relationship over the range it saw during training. That distinction matters.
If you ask for:
- exact arithmetic
- guaranteed extrapolation
- a closed-form rule
then a neural network is the wrong tool. For x^2, direct computation is simpler, faster, and exact. The value of this example is educational: it shows how neural networks fit continuous functions and where that process can fail.
Choosing Activations and Loss
MSELoss is the natural choice because the target is continuous. Activation choice is flexible:
- '
Tanhworks well on small normalized intervals' - '
ReLUcan work too, but may need slightly more width to fit curvature smoothly' - no activation should be used in the final layer for plain regression
You also do not need a huge dataset. Since the target function is deterministic, a few hundred evenly spaced samples are enough to demonstrate the idea clearly.
Common Pitfalls
One mistake is expecting good extrapolation. A network trained on [-2, 2] may output nonsense at 10, even if the in-range fit looks excellent.
Another mistake is using a classification-style output activation such as Sigmoid in the final layer. That compresses predictions into a limited range and makes it impossible to represent larger squared values.
Overbuilding the model is another frequent issue. A deep network can fit x^2, but it adds noise to the learning problem and makes the example harder to reason about. Start small.
Finally, do not judge the model only by the training loss. Print several predicted values across the interval and compare them with the real targets. For function approximation, spot checks reveal shape errors quickly.
Summary
- Approximating
x^2is a simple regression task that illustrates core neural-network behavior. - A small feed-forward network is enough when training data covers a bounded interval.
- Input scaling improves optimization and makes the fit more stable.
- The network learns an approximation over the observed range, not an exact symbolic rule.
- For real computation of squares, direct arithmetic is better; the neural-network version is mainly a learning exercise.

