# How to Detach specific components in the loss?

**URL:** <https://discuss.pytorch.org/t/how-to-detach-specific-components-in-the-loss/13983>\
**Category:** autograd\
**Created:** [February 24, 2018, 8:00am UTC](https://discuss.pytorch.org/t/how-to-detach-specific-components-in-the-loss/13983 "2018-02-24T08:00:37Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [September 4, 2018, 10:20pm UTC](https://discuss.pytorch.org/t/how-to-detach-specific-components-in-the-loss/13983/8 "2018-09-04T22:20:55Z")

</div>

`.detach()` will return a detached version of your tensor. You can reuse the “attached” tensor for further computations as long as you don’t reassign it somehow:

```python
modelA = nn.Linear(10, 10)
modelB = nn.Linear(10, 10)
modelC = nn.Linear(10, 10)

x = torch.randn(1, 10)
a = modelA(x)
b = modelB(a.detach())
b.mean().backward()
print(modelA.weight.grad)
print(modelB.weight.grad)
print(modelC.weight.grad)

c = modelC(a)
c.mean().backward()
print(modelA.weight.grad)
print(modelB.weight.grad)
print(modelC.weight.grad)

```

---

_[View the full topic](https://discuss.pytorch.org/t/how-to-detach-specific-components-in-the-loss/13983)._
