# Adding L1/L2 regularization in a Convolutional Networks in PyTorch?

**URL:** <https://discuss.pytorch.org/t/adding-l1-l2-regularization-in-a-convolutional-networks-in-pytorch/7734>\
**Category:** vision\
**Created:** [September 22, 2017, 6:24pm UTC](https://discuss.pytorch.org/t/adding-l1-l2-regularization-in-a-convolutional-networks-in-pytorch/7734 "2017-09-22T18:24:01Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![blade](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/blade/32/23861_2.png) [@blade](https://discuss.pytorch.org/u/blade)\
**Post date:** [January 14, 2023, 7:17pm UTC](https://discuss.pytorch.org/t/adding-l1-l2-regularization-in-a-convolutional-networks-in-pytorch/7734/4 "2023-01-14T19:17:36Z")

</div>

Since the L1 regularizer is not differentiable everywhere, what does PyTorch do when it encounters differentiating this functions? A simple example shows PyTorch returns zero.

```auto
import torch

x = torch.linspace(-1.0, 1.0, 5, requires_grad=True)
y = torch.abs(x)
y[2].backward()

print(x.grad)

```

> `tensor([-0., -0., 0., 0., 0.])`

Why is this the case?

---

_[View the full topic](https://discuss.pytorch.org/t/adding-l1-l2-regularization-in-a-convolutional-networks-in-pytorch/7734)._
