Minimize a function of one variable in Tensorflow
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To minimize a one-variable function in TensorFlow, treat the variable as a tf.Variable, compute the loss from it, and let an optimizer update it using gradients. TensorFlow does not care that the function has only one variable; the workflow is the same as optimizing model parameters.
For smooth differentiable functions, this is straightforward with GradientTape. The main things to watch are the learning rate and whether the function has multiple local minima.
Use tf.Variable and GradientTape
Here is a minimal example that minimizes the function (x - 3)^2 + 2:
Because the minimum is at x = 3, the optimizer gradually moves the variable toward that value.
Why This Works
TensorFlow automatically differentiates the function with respect to x. For this example, the derivative is 2(x - 3), so gradient descent repeatedly nudges x in the negative-gradient direction.
This is the same mechanism used for training neural networks. The only difference is that here there is just one trainable variable.
Example with a More Interesting Function
You can minimize nonlinear functions the same way:
For non-convex functions, the result may depend on the initial value because the optimizer can converge to a local minimum rather than the global one.
Choose the Optimizer for the Problem
For a one-variable differentiable function, plain SGD is often enough. Adam can converge faster when the surface is awkward or poorly scaled, but it is not magic.
The learning rate matters a lot:
- too large and the updates bounce around or diverge
- too small and convergence becomes very slow
A quick plot of the function can help you choose a sensible starting point and learning rate.
That matters even more for non-convex objectives, where a poor starting point can send the optimizer toward a completely different basin.
Running from several initial points is often a practical safeguard.
Constraints and Bounded Variables
If the variable must stay in a range, you can clamp it after each update:
That is a simple way to enforce bounds when the optimization problem is really "minimize this function subject to x being inside an interval."
For more advanced constrained or probabilistic optimization, TensorFlow Probability can be a better fit than raw TensorFlow optimizers.
Common Pitfalls
- Forgetting to make the variable a
tf.Variable, which means the optimizer has nothing to update. - Using a learning rate that is far too large and then blaming TensorFlow for unstable optimization.
- Assuming the result is global even when the function has multiple local minima.
- Reading the final
lossfrom the loop without recomputing it after the last parameter update. - Trying gradient-based minimization on a function that is not differentiable where you need it.
Summary
- In TensorFlow, minimize a one-variable function by optimizing a
tf.VariablewithGradientTape. - The workflow is the same as training a model, just with one parameter.
- SGD and Adam both work for differentiable scalar objectives.
- Learning rate and initialization strongly affect convergence.
- For bounded or more specialized optimization problems, add constraints explicitly or use a more specialized library.

