# Back propagation

**URL:** <https://discuss.pytorch.org/t/back-propagation/22192>\
**Category:** autograd\
**Created:** [August 1, 2018, 12:35pm UTC](https://discuss.pytorch.org/t/back-propagation/22192 "2018-08-01T12:35:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![adityakovvali](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/65b543/32.png) [@adityakovvali](https://discuss.pytorch.org/u/adityakovvali)\
**Post date:** [August 1, 2018, 12:35pm UTC](https://discuss.pytorch.org/t/back-propagation/22192/1 "2018-08-01T12:35:05Z")

</div>

I have trained a network with UNet algorithm. The input and output for my network are images with (512 x 512) pixels. I use L1loss for certain epochs and then I change the loss function to customized loss function. Now when I back-propagate with the custom loss, I want to update only certain pixels of the image and keep the other pixels as they are. How do I do it? as I have a convolution layer as my last layer. Incase of linearly connected layers, I can think of manually setting the gradients of few pixels to zero.

---

<div class="post-metadata">

**Author:** ![klory](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/klory/32/5814_2.png) [@klory](https://discuss.pytorch.org/u/klory)\
**Post date:** [August 1, 2018, 1:05pm UTC](https://discuss.pytorch.org/t/back-propagation/22192/2 "2018-08-01T13:05:21Z")

</div>

The solution I could think of is using a `mask` to remember the pixel you want to keep, then you first update all the pixels to get a new image `I'`, then merge the original image `I` by

```auto
I_out = I * mask + I'

```

---

<div class="post-metadata">

**Author:** ![adityakovvali](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/65b543/32.png) [@adityakovvali](https://discuss.pytorch.org/u/adityakovvali)\
**Post date:** [August 1, 2018, 1:18pm UTC](https://discuss.pytorch.org/t/back-propagation/22192/3 "2018-08-01T13:18:15Z")

</div>

This would change the weights in the network for all the pixels (pixelsToBeChanged and pixelsNotToChange). When a next image is through the feedforward, it would give me altered values.

---

<div class="post-metadata">

**Author:** ![klory](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/klory/32/5814_2.png) [@klory](https://discuss.pytorch.org/u/klory)\
**Post date:** [August 1, 2018, 3:30pm UTC](https://discuss.pytorch.org/t/back-propagation/22192/4 "2018-08-01T15:30:30Z")

</div>

> [@adityakovvali](#):
>
> when I back-propagate with the custom loss, I want to update only certain pixels of the image and keep the other pixels as they are.

the loss is just for some pixels or all pixels? I don’t know how to help, but these two links may help

1. [Automatic differentiation package - torch.autograd — PyTorch 2.1 documentation](https://pytorch.org/docs/stable/autograd.html#torch.Tensor.register_hook)
2. [GitHub - albertpumarola/GANimation: GANimation: Anatomically-aware Facial Animation from a Single Image (ECCV'18 Oral) [PyTorch]](https://github.com/albertpumarola/GANimation) (they use a mask to only change the ROIs)
