Is the L1 regularization in Keras/Tensorflow really L1-regularization?
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, the built-in L1 regularizer in Keras and TensorFlow is genuinely an L1 penalty: it adds a term proportional to the sum of absolute weight values to the training objective. The confusion usually comes from expecting exact zero weights immediately, while in practice the observed sparsity depends on optimizer behavior, regularization strength, and which parameters the regularizer is attached to.
What L1 Regularization Means in Practice
The textbook idea of L1 regularization is simple: take the original loss and add a penalty based on the absolute values of the weights. In words, the objective becomes:
- prediction loss
- plus
lambda * sum(abs(weights))
That is exactly the penalty Keras applies when you use an L1 regularizer on a layer parameter such as the kernel.
A minimal example looks like this:
When you train this model, TensorFlow adds the regularization term to the model loss automatically.
Where the Penalty Shows Up in Keras
In Keras, regularizers contribute through the layer losses collection. You can inspect that directly.
After the layer has been called once and its weights exist, layer.losses contains the L1 penalty term. At the model level, Keras includes these extra losses during training, so the optimizer minimizes both the prediction loss and the regularization loss together.
That behavior is not an approximation of some other penalty. It is the standard L1 form applied to the selected parameter tensor.
Why You May Not See Exact Zeros Immediately
This is where most confusion starts. In theory, L1 regularization encourages sparsity. In practice, seeing lots of exact zeros depends on training details.
For example:
- a very small regularization factor may barely move weights toward zero
- adaptive optimizers may not produce sharp sparsity as aggressively as people expect
- short training runs may stop before the regularizer has much visible effect
- the penalty may only be attached to kernels, not biases or other parameters
You can inspect the learned weights after training:
If many values become small but not exactly zero, the regularizer is still working. It is pushing the solution toward sparsity, even if the final optimizer path does not produce a perfect pile of literal zeros.
Apply It to the Right Parameter
In Keras, you choose which parameters receive the penalty. The most common option is kernel_regularizer, but there are also bias_regularizer and activity_regularizer options.
If someone expects "the whole layer" to be L1-regularized but only sets kernel_regularizer, that expectation mismatch can look like the implementation is wrong when it is really just narrower than assumed.
Compare L1 to L2 and L1L2
Keras also exposes L2 and combined L1L2 regularizers.
L2 pushes weights to be small in magnitude, while L1 pushes many weights toward zero more aggressively. If your model ends up dense but smooth, you may have chosen a regularization strength that behaves more like mild weight shrinkage than strong sparsity induction.
The Correct Answer to the Original Question
So, is Keras or TensorFlow "really" doing L1 regularization. Yes. The penalty term is the ordinary sum of absolute values multiplied by the specified coefficient. What varies is the optimization outcome, not the mathematical definition of the regularizer.
The practical lesson is to validate the full setup rather than judging from a single expectation such as "I do not see exact zeros after one epoch." The regularizer can be correct while the training configuration is too weak to make the sparsity obvious.
Common Pitfalls
- Expecting exact zeros immediately can lead to the wrong conclusion. L1 encourages sparsity, but the visible result depends on optimizer, coefficient strength, and training duration.
- Setting
kernel_regularizerand then assuming biases are regularized too is incorrect. Only the specified parameters receive the penalty. - Using a tiny L1 coefficient often makes the effect too small to notice in learned weights.
- Comparing L1 and L2 outcomes without checking the actual regularizer configuration can confuse shrinkage behavior with implementation correctness.
- Inspecting only prediction loss and ignoring
model.lossescan make it seem like regularization is missing when Keras is actually adding it internally.
Summary
- Keras and TensorFlow implement true L1 regularization as an absolute-value penalty added to the loss.
- The regularizer is mathematically correct even when trained weights are only near zero instead of exactly zero.
- In practice, sparsity depends on coefficient strength, optimizer choice, and training duration.
- '
kernel_regularizer,bias_regularizer, and related options apply the penalty only where you specify it.' - To judge the effect correctly, inspect both the model configuration and the learned weights after training.
Related reading
- Is the L1 regularization in Keras/Tensorflow really L1-regularization?
- Is there a built-in KL divergence loss function in TensorFlow?
- Is there a keras method to split data?
- Is there a momentum option for Adam optimizer in Keras?
- Is the xgboost documentation wrong ? early stopping rounds and best and last iteration
- Is there a better way to guess possible unknown variables without brute force than I am doing? Machine learning?
- is there a simple way to use features from tf.data.Dataset.from_generator with a custom model_fnEstimator in tensorflow
- Is there a tensorflow equivalent to np.empty?
.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.