What do these methods in pytorch do?

username: ky_Pa
cooked: <p>I have read a lot of explanations for the following methods, but I still don’t understand the specific use. Please explain, thank you!</p>
<ol>
<li>.state_dict() , .load_state_dict()<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/d/b/dbabe86ca4d4f30640dc008be82e560bccacd4ee.png" alt="微信图片_20200318170000" data-base62-sha1="vliSngVt3oOJfpjF2eOewBnGsgu" width="578" height="37"></li>
<li>.data , .max(1)[1]<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/1/4/14b987952f33b31554b6d96c0ef3ab8cced0ffa0.png" alt="微信图片_20200318170440" data-base62-sha1="2Xl2M9XM2UJrhBca4zL0XCuhN28" width="361" height="34"></li>
<li>.detach()<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/0/5/05a84d8588f79f29fc4d2a855bbf97d45e03eabd.png" alt="微信图片_20200318170148" data-base62-sha1="O2YpTwZsg59rAAU8G4doSNbgVT" width="453" height="29"></li>
</ol>

username: ptrblck
cooked: <ol>
<li>
<p>Load the <code>state_dict</code>, which contains all parameters and buffers of this module, into <code>tgt_encoder</code>, which seems to be a model (based on the name).</p>
</li>
<li>
<p><code>preds.max(1)[1]</code> calculates the max values and indices in dim1 and returns the indices (by indexing the result as <code>[1]</code>). Don’t use the <code>.data</code> attribute, as it might have unwanted side effects. Alternatively you could also use <code>torch.argmax(preds, 1)</code> to get the indices. This is often used to get the prediction of a model output, which contains logits or log_probabilities.</p>
</li>
<li>
<p><code>detach()</code> detaches the tensor from the computation graph. Autograd will stop at this point and will not calculate any gradients for the previous operations involving <code>feat_concat</code>.</p>
</li>
</ol>

username: ky_Pa
cooked: <ol>
<li>
<p>Does this mean that the parameters in the two models src_encoder and tgt_encoder are exactly the same?  What are buffers?</p>
</li>
<li>
<p>I still can’t understand the meaning of .max (1) [1]. This is context. Can you tell me what it does? Or tell me how should I modify the code?<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/2/3/2352d24888a72a31c99bd855cdd8d0d099620950.png" alt="微信图片_20200318210311" data-base62-sha1="52u9DT63TOnKmsD8pqmOZA9ZMd2" width="597" height="369"></p>
</li>
<li>
<p>Can you explain what the computation graph is?</p>
</li>
</ol>

username: ptrblck
cooked: <ol>
<li>
<p>Yes, at this point both models contains the same parameters and buffers. If they are trained afterwards, the values might of course diverge. Buffers are non-trainable tensors, which are registered to the module, e.g. the running stats of batchnorm layers.</p>
</li>
<li>
<p>Assuming <code>preds</code> have the shape <code>[batch_size, nb_classes, *]</code> and return the logits/log_prbabilities, you’ll get the predicted classes using this code (same output with the <code>.max(1)[1]</code> call):</p>
</li>
</ol>
<pre><code class="lang-python">batch_size, nb_classes = 2, 10
output = torch.randn(batch_size, nb_classes)
print(output)
&gt; tensor([[-2.1836, -0.8442,  0.7195, -0.6133,  0.8548,  0.5433,  0.1492, -0.3216,
          0.1395, -0.5018],
        [-1.3000,  0.1784, -0.1760, -0.1664, -0.5358, -0.2816, -1.4170,  0.4686,
          0.8357, -0.0534]])
pred = torch.argmax(output, 1)
print(pred)
&gt; tensor([4, 8])
</code></pre>
<ol start="3">
<li>PyTorch creates a computation graph dynamically in the forward pass, which is then used in the backward pass to backpropagate and compute the gradients.</li>
</ol>

username: ky_Pa
cooked: <p>Does this line of code in question 2 mean that the index of pred_cls and labels is the same? Is it right to use the category index for comparison?</p>
<pre><code class="lang-auto">acc += pred_cls.eq(labels.data).cpu().sum()</code></pre>

username: ptrblck
cooked: <aside class="quote no-group" data-username="ky_Pa" data-post="5" data-topic="73611">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/k/eada6e/48.png" class="avatar"> ky_Pa:</div>
<blockquote>
<p>Does this line of code in question 2 mean that the index of pred_cls and labels is the same?</p>
</blockquote>
</aside>
<p>If the predicted indices and target indices are equal, your model predicted the right classes, otherwise the wrong ones.</p>
<p>Your code will give you the sum of correctly predicted classes, which is not the accuracy.<br>
However, if you divide by the number of samples afterwards, it should work.</p>
<p>Also, don’t use the <code>.data</code> attribute as mentioned before. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>

username: ky_Pa
cooked: <p>OK, thank you very much for answering all my questions. <img src="https://discuss.pytorch.org/images/emoji/apple/blush.png?v=9" title=":blush:" class="emoji" alt=":blush:"></p>