NameError: name 'args' is not defined when I load mode follow the saving and loading model tutorial

bubu_ka: <p>I followed the saving and loading models tutorial  using Jupiter notebook.<br>
<a href="https://pytorch.org/tutorials/beginner/saving_loading_models.html" class="onebox" target="_blank" rel="nofollow noopener">https://pytorch.org/tutorials/beginner/saving_loading_models.html</a></p>
<p>But there is error</p>
<p>35     print(var_name, “\t”, optimizer.state_dict()[var_name])<br>
36 torch.save(model.state_dict(), PATH)<br>
—&gt; 37 model = TheModelClass(*args, **kwargs)<br>
38 model.load_state_dict(torch.load(PATH))<br>
39 model.eval()</p>
<p>NameError: name ‘args’ is not defined</p>

iArunava: <p>First, Please properly indent your code with ```. Eg</p>
<pre><code class="lang-auto">&gt;&gt;&gt; import code from code
&gt;&gt;&gt; code.magic()
magggggiicccc
</code></pre>
<p>Coming to your question,<br>
Since there are no <code>args</code> or <code>kwargs</code> to pass, just do it like</p>
<pre><code class="lang-auto">model = TheModelClass()
</code></pre>
<p>It will work fine.</p>
<hr>
<p><a class="mention" href="/u/ptrblck">@ptrblck</a> I don’t think we need to the <code>*args</code> and <code>*kwargs</code> as there are none. Right? Or I am missing something? I can make a PR to fix it if that’s the case.</p>

ptrblck: <p>I think the code snippets use <code>*args</code> and <code>**kwargs</code> just to demonstrate that you should pass all necessary arguments to the model and optimizer.<br>
If that’s confusing, maybe a short note in the text saying that you should adapt the arguments to your use case would be nice. What do you think?</p>

iArunava: <p>True. I agree with you.<br>
But it might be confusing for beginners (in python) / someone who is just following along the tutorial.</p>
<p>May be</p>
<pre><code class="lang-auto"># Pass all parameters required while initializing the model here.
model = TheModelClass()
</code></pre>
<p>or we can just</p>
<pre><code class="lang-auto"># Modify the arguments passed to the model as needed.
model = TheModelClass(*args, **kwargs)
</code></pre>
<p>Let me know.</p>
<hr>
<p>Also, I just went to the Colab notebook to make sure that everything works in Colab.<br>
But I saw, there are no code cells at all!!!</p>
<p>Here: <a href="https://colab.research.google.com/github/pytorch/tutorials/blob/gh-pages/_downloads/saving_loading_models.ipynb" rel="nofollow noopener">Colab Notebook</a><br>
Here is the Github notebook: <a href="https://github.com/pytorch/tutorials/blob/gh-pages/_downloads/saving_loading_models.ipynb" rel="nofollow noopener">Github Notebook</a><br>
??!</p>

ptrblck: <p>This tutorial looks as if it’s not supposed to be executable without modifications (e.g. <code>PATH</code> seems also to be undefined).</p>
<p>CC <a class="mention" href="/u/matthewinkawhich">@MatthewInkawhich</a> who wrote this tutorial.</p>

MatthewInkawhich: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> is correct. This “tutorial” is meant to be a centralized reference document. It was NOT intended to be executed as-is. Variables in the text such as <code>args</code> and <code>PATH</code> are simply placeholders.</p>

iArunava: <p>If that’s the case, I think it should be updated to be executed with appropriate comments. As beginners in PyTorch can hugely benefit from these. What do you think? <a class="mention" href="/u/ptrblck">@ptrblck</a> <a class="mention" href="/u/matthewinkawhich">@MatthewInkawhich</a></p>