IndexError: list index out of range for CNN model testing

username: MDing
cooked: <p>I am working on the VGG16 classification. But when I tried to test the model. I meet this error.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/8/181ae41d1c843e75c4f5d543fc6c010fb4579c18.png" data-download-href="https://discuss.pytorch.org/uploads/default/181ae41d1c843e75c4f5d543fc6c010fb4579c18" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/8/181ae41d1c843e75c4f5d543fc6c010fb4579c18_2_612x500.png" alt="image" data-base62-sha1="3rf4x9vz8i2MuOWsw2AXIcAOn0c" width="612" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/8/181ae41d1c843e75c4f5d543fc6c010fb4579c18_2_612x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/1/8/181ae41d1c843e75c4f5d543fc6c010fb4579c18_2_918x750.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/1/8/181ae41d1c843e75c4f5d543fc6c010fb4579c18.png 2x" data-dominant-color="F2F2F1"><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">930×759 101 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>
<p>I tried to change <em>for i in range(batch_size):</em> to <em>for i in range(len(target.data)):</em>. But I still get this error. Anybody help me?</p>

username: ptrblck
cooked: <p>Check which of the objects creates the indexing error, print its shape as well as the corresponding index, and make sure the index is inside the bounds. I don’t know how <code>correct</code> and <code>class_correct</code> are created and thus can only speculate.</p>

username: MDing
cooked: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/a/da1718c3aa2a7850392a1e4c127cf2eeab19a76f.png" data-download-href="https://discuss.pytorch.org/uploads/default/da1718c3aa2a7850392a1e4c127cf2eeab19a76f" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/a/da1718c3aa2a7850392a1e4c127cf2eeab19a76f_2_614x499.png" alt="image" data-base62-sha1="v7jzwWn0NTjd0hW5mEGIOf0r7eD" width="614" height="499" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/a/da1718c3aa2a7850392a1e4c127cf2eeab19a76f_2_614x499.png, https://discuss.pytorch.org/uploads/default/optimized/3X/d/a/da1718c3aa2a7850392a1e4c127cf2eeab19a76f_2_921x748.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/d/a/da1718c3aa2a7850392a1e4c127cf2eeab19a76f.png 2x" data-dominant-color="F3F3F3"><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">1019×829 99.7 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><br>
Thank you for your reply. Please check the shape.<br>
class_correct means the correct prediction number, for example, if the test image is negative, the prediction is negative, I made it add 1.<br>
I also read this: <a href="https://discuss.pytorch.org/t/indexerror-index-out-of-bounds-error/36671/5" class="inline-onebox">IndexError: index out of bounds error - #5 by Gutabaga</a></p>

username: polskyedd
cooked: <aside class="quote no-group" data-username="MDing" data-post="1" data-topic="141753">
<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/mding/48/45206_2.png" class="avatar"> MDing:</div>
<blockquote>
<p>But I still get this error.</p>
</blockquote>
</aside>
<p>The IndexError is raised when attempting to retrieve an index from a sequence (e.g. list, tuple), and the index isn’t found in the sequence. The Python documentation defines when this exception is raised:</p>
<p><code>Raised when a sequence subscript is out of range.</code></p>
<p>Here’s an <a href="http://net-informations.com/python/file/split.htm" rel="noopener nofollow ugc">Python Split()</a> example that raises the IndexError:</p>
<pre><code class="lang-auto">data = "one%two%three%four%five"
numbers = data.split('%')
</code></pre>
<p>The list numbers has 5 elements, and the indexing starts with 0, so, the last element will have index 4. If you try to subscript with an index higher than 4, the Python Interpreter will raise an IndexError since there is no element at such index.</p>