How to slice Tensorflow network into two maintaining gradient back-propagation?
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
Yes, you can split a TensorFlow network into two pieces and still preserve gradient back-propagation, as long as the intermediate value remains an ordinary TensorFlow tensor in the same differentiable graph. The split itself is not the problem. Gradients break only when you convert the intermediate result to NumPy, wrap it as a fresh constant, or explicitly stop gradients.
The Core Rule
Gradient flow is preserved when:
- part one produces a tensor
- part two consumes that tensor
- the full forward pass stays inside TensorFlow ops tracked by
tf.GradientTapeor the Keras model graph
Gradient flow is broken when you do things such as:
- '
tensor.numpy()inside training' - '
tf.stop_gradient(...)' - rebuilding the intermediate output as
tf.constant(...) - moving values through non-differentiable external code
That distinction matters more than whether the model is stored in one object or two.
Simple Two-Part Model Example
Gradients flow through z automatically because z is just an intermediate tensor in the same differentiable computation.
Functional API Version
If you want a clean model split for reuse, the Keras Functional API is often the best fit.
Now you have two reusable parts while still understanding that, during training, the actual bridge tensor must remain connected in one TensorFlow computation.
What Breaks The Gradient
This is the classic mistake:
Why this breaks training:
- '
z.numpy()leaves TensorFlow's differentiable graph' - '
tf.constant(z_numpy)creates a new leaf tensor unrelated topart1' - the tape can no longer trace the path back into
part1
If your gradients for the first half are None, this kind of graph break is the first thing to check.
Training The Parts With One Or Two Optimizers
You can still use separate optimizers if needed. The important part is that the loss is computed from the connected forward pass.
In practice, many people compute all gradients in one call and split them later, but the principle is the same.
When tf.stop_gradient Is Useful
Sometimes you intentionally want to cut the backward path.
This freezes learning into part1. That can be correct for staged training or frozen feature extractors, but it is the opposite of maintaining back-propagation across the split.
Common Pitfalls
- Converting the intermediate tensor to NumPy during training.
- Re-wrapping the intermediate output as a new constant or placeholder.
- Assuming separate model objects automatically break gradients. They do not if tensors stay connected.
- Using
tf.stop_gradientwithout realizing it cuts the backward path intentionally. - Debugging the optimizer first when the real problem is that the graph was broken between the two halves.
Summary
- Splitting a TensorFlow network into two parts does not break gradients by itself.
- Back-propagation is preserved as long as the intermediate output stays a connected TensorFlow tensor.
- '
tf.GradientTapeand the Keras Functional API both support this pattern naturally.' - Gradients break when you convert tensors to NumPy, recreate them as constants, or call
tf.stop_gradient. - If the first half gets no gradients, inspect the bridge tensor path before changing the optimizer or architecture.
Related reading
- How to solve ' CUDA out of memory. Tried to allocate xxx MiB' in pytorch?
- How to solve ImportError Keras requires TensorFlow 2.2 or higher. Install TensorFlow via pip install tensorflow?
- How to solve the famous `unhandled cuda error, NCCL version 2.7.8` error?
- How to specify number of GPUs in Python interface?
- How to solve Cholesky decomposition error in Tensorflow caused by low precision datatype tf.float32?
- How to specify padding with keras in Conv2D layer?
- How to specify the correlation coefficient as the loss function in keras
- How to speed up Tensorflow 2 keras model for inference?
.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.