Save snapshots in a python list

username: Mohammed_Awney
cooked: <p>I attempt to apply snapshot ensemble following this paper <a href="https://arxiv.org/abs/1704.00109" rel="noopener nofollow ugc">Snapshot Ensembles: Train 1, get M for free</a> what I understand that I save model weights after each cycle. The one cycle contains many epochs. I want to save these weights at the end of each cycle into a list for further using them in test phase.<br>
At beginning I defined snaphots = <span class="chcklst-box fa fa-square-o fa-fw"></span> then @ end of each cycle append models weights to this list : snapshots.append(model.state_dict()). The problem that I found that all snapshot " weights" in the list are the same  although they are different during the training<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/e/efdb7f4c5fef1acdc38ca01e08db36c19733aa95.png" data-download-href="https://discuss.pytorch.org/uploads/default/efdb7f4c5fef1acdc38ca01e08db36c19733aa95" title="Untitled"><img src="https://discuss.pytorch.org/uploads/default/original/2X/e/efdb7f4c5fef1acdc38ca01e08db36c19733aa95.png" alt="Untitled" data-base62-sha1="ydSnWlrmNdrh5U5abCqutTxi9PT" width="690" height="452" data-dominant-color="F8F5F5"><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">Untitled</span><span class="informations">868×569 12.8 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>  any recommendation to solve this problem?@ <a href="https://discuss.pytorch.org/u/ptrblck">ptrblck</a></p>

username: Pranavan_Theivendira
cooked: <aside class="quote no-group" data-username="Mohammed_Awney" data-post="1" data-topic="54883">
<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/mohammed_awney/48/8656_2.png" class="avatar"> Mohammed_Awney:</div>
<blockquote>
<p>p</p>
</blockquote>
</aside>
<p>Hi,</p>
<p>The problem is that you are referring the same object. When you save it to the list instead of “snapshots.append(model.state_dict())”. Use the following command.</p>
<pre><code class="lang-auto">snapshots.append(model.state_dict().copy())
</code></pre>
<p>Then only, you can save the values at that point.</p>
<p>One small concern - when you ask question. Please do not mention a specific name. There are lots of people in the forum. Somebody will come and answer your question. If you mention a name, others may not answer.</p>
<p>Thanks</p>

username: Mohammed_Awney
cooked: <aside class="quote no-group" data-username="Pranavan_Theivendira" data-post="2" data-topic="54883">
<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/pranavan_theivendira/48/12366_2.png" class="avatar"> Pranavan_Theivendira:</div>
<blockquote>
<p>ebody will come and answer your question. If you mention a name, others may</p>
</blockquote>
</aside>
<p>thanks for your answer and advice</p>