How does one reproduce the deepcopy error of tensors made by code?

pinocchio: <p>I’ve gotten an error like this before:</p>
<pre><code class="lang-auto">Only Tensors created explicitly by the user (graph leaves) support the deepcopy protocol at the moment
</code></pre>
<p>but my code is way to large and confusing to paste here. I tried to make a self contained jupyter note but failed. Anyone know how?</p>
<p>Will paste my attempts:</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn

import copy
from collections import OrderedDict

model = nn.Sequential( OrderedDict( [ ('fc0', nn.Linear(3,1)) ] ) )
#model.fc0.weight = nn.Parameter( torch.randn(3,1) + 3 )
for i in range(3):
    w_new = nn.Parameter(model.fc0.weight.clamp(0))
    model.fc0.weight = w_new

model_copy = copy.deepcopy(model)
</code></pre>

miker256: <p>This may help you out <a class="mention" href="/u/pinocchio">@pinocchio</a>. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<aside class="onebox stackexchange">
  <header class="source">
      <a href="https://stackoverflow.com/questions/56590886/how-to-solve-the-run-time-error-only-tensors-created-explicitly-by-the-user-gr" target="_blank" rel="nofollow noopener">stackoverflow.com</a>
  </header>
  <article class="onebox-body">
      <a href="https://stackoverflow.com/users/11645716/mazhar-abbas" target="_blank" rel="nofollow noopener">
    <img alt="mazhar abbas" src="https://lh3.googleusercontent.com/-lFEwfw2au9A/AAAAAAAAAAI/AAAAAAAAAAA/ACHi3rejNvdqEEoL--5NVKo-tht5cttnqA/mo/photo.jpg?sz=128" class="thumbnail onebox-avatar" width="60" height="60">
  </a>
<h4>
  <a href="https://stackoverflow.com/questions/56590886/how-to-solve-the-run-time-error-only-tensors-created-explicitly-by-the-user-gr" target="_blank" rel="nofollow noopener">How to solve the run time error "Only Tensors created explicitly by the user (graph leaves) support the deepcopy protocol at the moment"</a>
</h4>

<div class="tags">
  <strong>pytorch, python-2.5</strong>
</div>

<div class="date">
  asked by
  
  <a href="https://stackoverflow.com/users/11645716/mazhar-abbas" target="_blank" rel="nofollow noopener">
    mazhar abbas
  </a>
  on <a href="https://stackoverflow.com/questions/56590886/how-to-solve-the-run-time-error-only-tensors-created-explicitly-by-the-user-gr" target="_blank" rel="nofollow noopener">03:05AM - 14 Jun 19 UTC</a>
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


pinocchio: <p>managed to create my own example!</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn

import copy
from collections import OrderedDict

model = nn.Sequential( OrderedDict( [ ('fc0', nn.Linear(3,1)) ] ) )
#model.fc0.weight = nn.Parameter( torch.randn(3,1) + 3 )
print(model.fc0.weight)
w = model.fc0.weight
for i in range(5):
    w_new = w - 2*(w)
print()
print(w_new.is_leaf)
#model.fc0.weight = nn.Parameter( w_new )
setattr(model,'fc0.weight', w_new )
print(model.fc0.weight.is_leaf)
print(model.fc0.weight)
model_copy = copy.deepcopy(model)
</code></pre>
<p>seems that the issue is with this line of code:</p>
<pre><code class="lang-auto"> def __deepcopy__(self, memo): 
     if not self.is_leaf: 
         raise RuntimeError("Only Variables created explicitly by the user " 
                            "(graph leaves) support the deepcopy protocol at the moment") 
     result = type(self)(self.data.clone()) 
     result.requires_grad = self.requires_grad 
     result.volatile = self.volatile 
     memo[id(self)] = result 
     return result 
</code></pre>
<p>pytorch determines this by checking if things are leafs. Somehow doing <code>nn.Parameter(w_new)</code> sets things as new leafs.</p>
<p>but the <code>setattr</code> somehow avoids it and introduces the error.</p>