Top_p, top_class = ps.topk(1, dim=1)

username: snip3r77
cooked: <p>Pls scroll towards 1/3 down from <a href="https://github.com/udacity/deep-learning-v2-pytorch/blob/master/intro-to-pytorch/Part%205%20-%20Inference%20and%20Validation%20(Solution).ipynb" rel="noopener nofollow ugc">this page</a></p>
<p>for</p>
<p><code>equals = top_class == labels</code></p>
<p>should it be</p>
<p><code>equals = top_class == labels.view(-1,64)</code></p>
<p><em>a small test that I did</em><br>
<em><img src="https://i.imgur.com/ffUKixU.png" alt="" role="presentation" width="" height=""></em></p>
<p>rather than</p>
<p><code>equals = top_class == labels.view(*top_class.shape)</code></p>
<p>*I understood the unpacking of <em>top_class.shape is based on <a href="https://stackoverflow.com/questions/11315010/what-do-and-before-a-variable-name-mean-in-a-function-signature" rel="noopener nofollow ugc">this</a></em></p>
<p>because the former has a shape of (64,64) based on the explanation of</p>
<blockquote>
<p>equals will have shape (64, 64), try it yourself. What it’s doing is comparing the one element in each row of top_class with each element in labels which returns 64 True/False boolean values for each row.This is because of broadcasting.</p>
</blockquote>
<p>Pls advise.<br>
Cheers and Happy New Year.</p>

username: ptrblck
cooked: <p>I haven’t executed the notebook, but if <code>top_class</code> has the shape <code>[64, 64]</code>, your <code>view</code> operation will return the same shape as the current implementation in the notebook.<br>
Also, using the shape of <code>top_class</code> seems to take into consideration use cases with another number of classes.</p>
<p>Please correct me, if I misunderstood something.</p>

username: snip3r77
cooked: <p>Thanks for your reply.</p>
<blockquote>
<p>Here <code>top_class</code> is a 2D tensor with shape <code>(64, 1)</code> while <code>labels</code> is 1D with shape <code>(64)</code> .</p>
</blockquote>
<p>We need to have top_class with shape of <code>(64, 1)</code> and ‘labels’ <code>(1,64)</code> in order for it to invoke broadcasting. With this top_class’s shape would be <code>(64, 64)</code> . The <code>64</code> refers to the batch size in this scenaraio.</p>
<p>Thanks.</p>

username: ptrblck
cooked: <p>As far as I understand the notebook, broadcasting should <em>not</em> be used:</p>
<blockquote>
<p>To get the equality to work out the way we want, <code>top_class</code> and <code>labels</code> must have the same shape.<br>
If we do<br>
<code>equals = top_class == labels</code><br>
<code>equals</code> will have shape <code>(64, 64)</code> , try it yourself. What it’s doing is comparing the one element in each row of <code>top_class</code> with each element in <code>labels</code> which returns 64 True/False boolean values for each row.</p>
</blockquote>
<p>So this behavior is not desired, but instead each element of <code>top_class</code> should be compared to only the corresponding element in <code>labels</code>.</p>

username: snip3r77
cooked: <p>Thanks I finally understood.</p>
<p>Would it be the same if I use</p>
<p><code>equals = clas == lab.view(-1,1)</code></p>
<p>as the output is the same.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://i.imgur.com/SjtIObU.png" title="" rel="nofollow noopener"><img src="https://i.imgur.com/SjtIObU.png" alt width="391" height="500"><div class="meta">
<svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use xlink:href="#far-image"></use></svg><span class="filename"></span><span class="informations">532×679</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use xlink:href="#discourse-expand"></use></svg>
</div></a></div></p>

username: ptrblck
cooked: <p><code>.view(-1, 1)</code> should work in this case, but <code>.view(*clas.shape)</code> seems to be the safest way here. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>