Using generator twice in Python

Ohm: <p>When I am using the “model.parameters()” twice, it just work for one time. It is because the type of model.parameters() is a generator. Is there any solution to tackle this issue?<br>
‘’’’’<br>
<span class="hashtag">#working</span><br>
for param in model.parameters():<br>
par1.append(param.grad.view(-1))<br>
<span class="hashtag">#Not</span> working<br>
for param in model.parameters():<br>
par2.append(param.grad.view(-1))</p>

ZimoNitrome: <p>I was not able to reproduce this. Seems to work well for me.<br>
But you could try to create iterators from the generators and use them:</p>
<pre><code class="lang-auto">for param in iter(model.parameters()):
    par1.append(param.grad.view(-1))

for param in iter(model.parameters()):
    par2.append(param.grad.view(-1))
</code></pre>
<p>If that doesn’t work you could look into <code>copy</code> and <code>deepcopy</code> to copy the parameters.</p>

Ohm: <p>Could you please share your runnable example?</p>

ZimoNitrome: <p>Is that code not running for you?</p>
<p>I get:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/d/3de972a9bc324134961f02f2a92e6a1623358ea6.png" data-download-href="https://discuss.pytorch.org/uploads/default/3de972a9bc324134961f02f2a92e6a1623358ea6" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/3/d/3de972a9bc324134961f02f2a92e6a1623358ea6.png" alt="image" data-base62-sha1="8PHhHxOlIWz3KdBSQwVtCkMtbZs" width="690" height="414" data-dominant-color="F1F1F1"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">809×486 18 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

Ohm: <aside class="quote no-group" data-username="Ohm" data-post="1" data-topic="113927">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ohm/48/29674_2.png" class="avatar"> Ohm:</div>
<blockquote>
<p>par1.append(param.grad.view(-1))</p>
</blockquote>
</aside>
<p>It is not working properly. The reason is the ‘break’. Without ‘break’, we turn back to the same point as I asked.</p>

ZimoNitrome: <p>It is for demonstrational purposes. Without break it lists ALL parameters twice for me. Notice how all elements are identical in my example above.</p>
<p>My only guess would be that you are running an older version of PyTorch or the model implements a custom <code>parameters()</code> method with behavior unlike the default <code>nn.Module</code>.</p>
<p>Like I said, you can try to copy the entire model/parameters to have two identical objects to loop through like:</p>
<pre><code class="lang-python">from copy import deepcopy

my_parameters = list(iter(model.parameters()))
my_parameters2 = deepcopy(my_parameters)

for param in my_parameters:
    par1.append(param.grad.view(-1))

for param in my_parameters2 :
    par2.append(param.grad.view(-1))
</code></pre>