AttributeError: 'numpy.ndarray' object has no attribute 'cpu'

thida: <p>Hi,<br>
I am having a problem in plotting the accuracy of trained model, I adopted the following code for plotting the accuracy from tutorial <a href="https://pytorch.org/tutorials/beginner/finetuning_torchvision_models_tutorial.html#model-training-and-validation-code" class="inline-onebox" rel="noopener nofollow ugc">Finetuning Torchvision Models — PyTorch Tutorials 2.2.0+cu121 documentation</a>.<br>
Here is the code,</p>
<pre><code class="lang-auto">ohist = []
shist = []

ohist = [h.cpu().numpy() for h in epoch_accuracy_o]
shist = [h.cpu().numpy() for h in epoch_accuracy_s]
print(ohist)
print(shist)
plt.title("Validation Accuracy vs. Number of Training Epochs")
plt.xlim([1, 20])
plt.ylim([0, 1])
plt.yticks(np.linspace(0, 1, 11))
plt.xticks(np.linspace(0, 20, 21))
plt.xlabel('epoch')
plt.ylabel('accuracy')
plt.grid(linestyle='--')

plt.plot(ohist,label="Pretrained")
plt.plot(shist,label="Scratch")
plt.legend()
plt.show()```
</code></pre>
<p>and here is the error I got,<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/d/1/d1cddc4ad7ceec899fb442502334849c5cb9843f.jpeg" alt="h1" data-base62-sha1="tW0QpTNlRjhRCACnospnxCpiR1J" width="665" height="280"><br>
After I printed what’s included in h, this is how it looks,<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/1/01278179ebba076d685441569a7a14814bdd652c.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/01278179ebba076d685441569a7a14814bdd652c" title="h2"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/1/01278179ebba076d685441569a7a14814bdd652c_2_414x500.jpeg" alt="h2" data-base62-sha1="ad7kiOabjkcgCR23mHQ5MKOXMg" width="414" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/1/01278179ebba076d685441569a7a14814bdd652c_2_414x500.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/0/1/01278179ebba076d685441569a7a14814bdd652c.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/0/1/01278179ebba076d685441569a7a14814bdd652c.jpeg 2x" data-dominant-color="E7E7E6"><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">h2</span><span class="informations">537×647 124 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>Is there any way I can solve it please?</p>

s_ashrafi: <p>I’m not sure, but it seems epoch_accuracy_o is a 2d NumPy array. Try this code:<br>
<code>ohist = [h.cpu()numpy() for h in epoch_accuracy_o[0]]</code></p>

thida: <p>Thank you for the quick reply sir.<br>
I tried and it says like<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/7/c708c93396b6f6443f554d2891c051914622ed50.png" data-download-href="https://discuss.pytorch.org/uploads/default/c708c93396b6f6443f554d2891c051914622ed50" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/c/7/c708c93396b6f6443f554d2891c051914622ed50.png" alt="image" data-base62-sha1="soJQ6yKm1t090f2kDqz61UwRl5K" width="690" height="273" data-dominant-color="F6F8F7"><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">701×278 10.2 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>

thida: <p>Somehow I changed the <code>[h.cpu().numpy() for h in epoch_accuracy_o]</code> into <code>ohist = [h for h in epoch_accuracy_o]</code> then it works.</p>

s_ashrafi: <p>Try this and show here the results:</p>
<pre><code class="lang-auto">print(epoch_accuracy_o)
print(epoch_accuracy_o.shape)
</code></pre>
<aside class="quote no-group" data-username="thida" data-post="4" data-topic="79512">
<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/t/8baadc/48.png" class="avatar"> thida:</div>
<blockquote>
<p>[h.cpu().numpy() for h in epoch_accuracy_o]</p>
</blockquote>
</aside>
<p>here h is a numpy array, but it must be pytorch tensor…</p>

thida: <p>Yes, and here is what I got,<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/b/fbc25d7f72a7d22f3801f43202e9a2d0c73811a5.png" data-download-href="https://discuss.pytorch.org/uploads/default/fbc25d7f72a7d22f3801f43202e9a2d0c73811a5" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/f/b/fbc25d7f72a7d22f3801f43202e9a2d0c73811a5.png" alt="image" data-base62-sha1="zVagMpz1nzOb8Tprhy6oW0LBxAx" width="690" height="247" data-dominant-color="F0F0F0"><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">893×320 18.9 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>