# I wonder if it is possible to multiply the output of the module by the weight of another module

**URL:** <https://discuss.pytorch.org/t/i-wonder-if-it-is-possible-to-multiply-the-output-of-the-module-by-the-weight-of-another-module/113808>\
**Category:** autograd\
**Created:** [March 5, 2021, 9:32am UTC](https://discuss.pytorch.org/t/i-wonder-if-it-is-possible-to-multiply-the-output-of-the-module-by-the-weight-of-another-module/113808 "2021-03-05T09:32:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![11153](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/11153/32/20038_2.png) [@11153](https://discuss.pytorch.org/u/11153)\
**Post date:** [March 5, 2021, 9:32am UTC](https://discuss.pytorch.org/t/i-wonder-if-it-is-possible-to-multiply-the-output-of-the-module-by-the-weight-of-another-module/113808/1 "2021-03-05T09:32:47Z")

</div>

Assuming that the model has two linear layers,

First, put the **input x** in the first layer.

And **output** from the first layer manipulates the **weight** of the second layer.

Finally, put the same **input x** into the second layer.

 ![sample](https://discuss.pytorch.org/uploads/default/original/3X/6/b/6b565e44a7da777477a2be4d1604e88e71ef636f.jpeg)

I would like to ask if both layers will be updated normally in the following situations.

Thank you.

---

<div class="post-metadata">

**Author:** ![soulitzer](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/839c29/32.png) [@soulitzer](https://discuss.pytorch.org/u/soulitzer)\
**Post date:** [March 5, 2021, 10:59pm UTC](https://discuss.pytorch.org/t/i-wonder-if-it-is-possible-to-multiply-the-output-of-the-module-by-the-weight-of-another-module/113808/2 "2021-03-05T22:59:59Z")

</div>

Are you creating a custom nn module?

```auto
def forward(self, x):
    return torch.cat((x, x)) * self.w * x + self.b

```

If you do something like this, it should be OK. Trying to reuse Linear OTOH, and modifying its weight in-place is not recommended.

---

<div class="post-metadata">

**Author:** ![11153](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/11153/32/20038_2.png) [@11153](https://discuss.pytorch.org/u/11153)\
**Post date:** [March 8, 2021, 2:25am UTC](https://discuss.pytorch.org/t/i-wonder-if-it-is-possible-to-multiply-the-output-of-the-module-by-the-weight-of-another-module/113808/3 "2021-03-08T02:25:15Z")

</div>

Thanks for you kind reply!!

Originally, I tried to approach it second way, but I’ll try the first way.

Thank you.

---

<div class="post-metadata">

**Author:** ![CloudFan](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/cloudfan/32/30499_2.png) [@CloudFan](https://discuss.pytorch.org/u/CloudFan)\
**Post date:** [April 27, 2021, 6:27pm UTC](https://discuss.pytorch.org/t/i-wonder-if-it-is-possible-to-multiply-the-output-of-the-module-by-the-weight-of-another-module/113808/4 "2021-04-27T18:27:01Z")

</div>

Hi, did you solve this problem? I’m also trying to manipulate the weights in the second model with the outputs of the first model. The following is the foward function in second model:  
def forward(self, x, kernel, bias):  
self.conv[0].weight.data = self.conv[0].weight.data\*kernel  
self.conv[0].bias.data = self.conv[0].bias.data +torch.squeeze(bias)  
out = self.conv(x)  
return out  
where kernel and bias are the output of the first model. But I got the error message  
RuntimeError: element 0 of tensors does not require grad and does not have a grad\_fn  
Do you have any idea to solve this problem?
