# Convert tensor to Parameter (while keeping the graph)

**URL:** <https://discuss.pytorch.org/t/convert-tensor-to-parameter-while-keeping-the-graph/27219>\
**Category:** autograd\
**Created:** [October 14, 2018, 9:05am UTC](https://discuss.pytorch.org/t/convert-tensor-to-parameter-while-keeping-the-graph/27219 "2018-10-14T09:05:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zen](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/z/7c8e57/32.png) [@Zen](https://discuss.pytorch.org/u/Zen)\
**Post date:** [October 14, 2018, 9:05am UTC](https://discuss.pytorch.org/t/convert-tensor-to-parameter-while-keeping-the-graph/27219/1 "2018-10-14T09:05:06Z")

</div>

I want to create a neural network generating the parameters of another NN. The NN ‘A’ generates ‘B’.

But there is a problem: the output of ‘A’ is a ‘tensor’ (with requires\_grad = True) while the parameters of ‘B’ are ‘Parameter’. Since I want to backward the gradient up to the parameters of ‘A’, I am looking for a conversion tensor -\> Parameter that keeps the computational graph.

A naive conversion ‘w = Parameter(y)’, where w is the generated parameter of ‘B’, and y is the output of ‘A’, does not work: the computational graph is cut between w and y (the gradient of the parameters of ‘A’ are None, while the gradient of w, which should be non-leaf, is not None).

---

<div class="post-metadata">

**Author:** ![InnovArul](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/innovarul/32/5282_2.png) [@InnovArul](https://discuss.pytorch.org/u/InnovArul)\
**Post date:** [October 14, 2018, 9:34am UTC](https://discuss.pytorch.org/t/convert-tensor-to-parameter-while-keeping-the-graph/27219/2 "2018-10-14T09:34:41Z")

</div>

It depends on what ‘B’ does. Have you looked into `torch.nn.functional` APIs?

---

<div class="post-metadata">

**Author:** ![Zen](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/z/7c8e57/32.png) [@Zen](https://discuss.pytorch.org/u/Zen)\
**Post date:** [October 14, 2018, 9:51am UTC](https://discuss.pytorch.org/t/convert-tensor-to-parameter-while-keeping-the-graph/27219/3 "2018-10-14T09:51:05Z")

</div>

‘B’ is a NN with fully connected layers and convolutional layers, whose parameters are of type ‘Parameter’.  
So, I could replace the usual layers by layers using torch.nn.functional functions… but I need to rewrite each layer I want to use, then. Is this the only solution?

---

<div class="post-metadata">

**Author:** ![InnovArul](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/innovarul/32/5282_2.png) [@InnovArul](https://discuss.pytorch.org/u/InnovArul)\
**Post date:** [October 14, 2018, 10:54am UTC](https://discuss.pytorch.org/t/convert-tensor-to-parameter-while-keeping-the-graph/27219/4 "2018-10-14T10:54:17Z")

</div>

if you want the network ‘A’ to predict the parameters of network ‘B’, this could be the only easier way than meddling around with default `nn` layers.

---

<div class="post-metadata">

**Author:** ![Brando\_Miranda](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/brando_miranda/32/14355_2.png) [@Brando\_Miranda](https://discuss.pytorch.org/u/Brando_Miranda)\
**Post date:** [January 27, 2020, 8:25pm UTC](https://discuss.pytorch.org/t/convert-tensor-to-parameter-while-keeping-the-graph/27219/5 "2020-01-27T20:25:05Z")

</div>

Just responding to the actual title of the question. What is wrong with using `nn.Parameter(old_tensor)`

where `old_tensor` is of type `torch.Tensor`. Does that not work for you? Why does it not?

---

<div class="post-metadata">

**Author:** ![Zen](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/z/7c8e57/32.png) [@Zen](https://discuss.pytorch.org/u/Zen)\
**Post date:** [April 1, 2020, 9:44am UTC](https://discuss.pytorch.org/t/convert-tensor-to-parameter-while-keeping-the-graph/27219/6 "2020-04-01T09:44:44Z")

</div>

As I mentioned, `new_tensor = nn.Parameter(old_tensor)` breaks the computational graph of autograd.

In other words, `new_tensor` does not contain any information about the way `old_tensor` has been computed, which I want.

---

<div class="post-metadata">

**Author:** ![zacharynew](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zacharynew/32/19474_2.png) [@zacharynew](https://discuss.pytorch.org/u/zacharynew)\
**Post date:** [April 13, 2020, 8:18pm UTC](https://discuss.pytorch.org/t/convert-tensor-to-parameter-while-keeping-the-graph/27219/7 "2020-04-13T20:18:57Z")

</div>

Hi @Zen did you ever find a solution to this problem? I am now facing exactly the same issue.

---

<div class="post-metadata">

**Author:** ![CNguyen](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/cnguyen/32/71957_2.png) [@CNguyen](https://discuss.pytorch.org/u/CNguyen)\
**Post date:** [January 20, 2022, 6:35am UTC](https://discuss.pytorch.org/t/convert-tensor-to-parameter-while-keeping-the-graph/27219/8 "2022-01-20T06:35:58Z")

</div>

For one that is still looking for a solution, you might find this [thread](https://discuss.pytorch.org/t/assign-parameters-to-nn-module-and-have-grad-fn-track-it/62677) useful. There are 2 ways to do it:

1. Delete the weights of each layer manually and re-assign by the tensor of interst (with history)
2. Use a package from [GitHub - SsnL/PyTorch-Reparam-Module: Reparameterize your PyTorch modules](https://github.com/SsnL/PyTorch-Reparam-Module), but note that it might not work with `batchnorm` layer when `track_running_stats=True`.

Another useful package that is supported directly from PyTorch is `functorch` with `make_functional`.
