"can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first." and "list object has no attribute cpu"

username: naychelynn
cooked: <p>Hello guys,</p>
<ol>
<li>I have one of the common issues of type conversion “can’t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.”<br>
So, I tried to solve like the answer comment " .cpu().numpy() ".</li>
<li>But unfortunately, another issue “list object has no attribute cpu.”</li>
<li>By trying to solve with “.cpu().detach().numpy()”, I got the error “list object has no attribute cpu”.</li>
<li>I also tried one of the related suggestions in this forum like “new_tensor = torch.tensor(old_tensor.item(), device = ‘cpu’)” but still have another issue “4. only one element tensors can be converted to Python scalars”.</li>
<li>The final attempt is trying using with .item() but still " AttributeError ‘list’ object has no attribute ‘item’ ".</li>
</ol>
<p>So, if you have some idea or advice for me, please feel free to share.</p>
<p>Thanks in advance.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/2/b289bfa411470b59ff3377837b1e064cac96a525.png" data-download-href="https://discuss.pytorch.org/uploads/default/b289bfa411470b59ff3377837b1e064cac96a525" title="1. cannot convert cuda type tensor to numpy"><img src="https://discuss.pytorch.org/uploads/default/original/3X/b/2/b289bfa411470b59ff3377837b1e064cac96a525.png" alt="1. cannot convert cuda type tensor to numpy" data-base62-sha1="ptq775KyD7IVOjHRh92GDdFfKxT" width="690" height="220" data-dominant-color="1C282F"><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">1. cannot convert cuda type tensor to numpy</span><span class="informations">963×308 11.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><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/b/ab0efa1759d619cd660f4018d9db0502e0b44722.png" data-download-href="https://discuss.pytorch.org/uploads/default/ab0efa1759d619cd660f4018d9db0502e0b44722" title="2. list object has no attribute cpu"><img src="https://discuss.pytorch.org/uploads/default/original/3X/a/b/ab0efa1759d619cd660f4018d9db0502e0b44722.png" alt="2. list object has no attribute cpu" data-base62-sha1="opfJAFRqBbKjtom6Kpr6M9ZOsn0" width="690" height="45" data-dominant-color="20282F"><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">2. list object has no attribute cpu</span><span class="informations">878×58 1.92 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>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/6/8/68a1b1a72cb01490bcadbbe5fc8e7dc9c7f24427.png" alt="3. list object has no attribute cpu" data-base62-sha1="eVC5fkd3srZmg1NcI1I6jdUoOPB" width="425" height="69"><br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/1/1/114d0377d94bf44791acd987e42376f7d0437db4.png" alt="4. only one element tensors can be converted to Python scalars" data-base62-sha1="2t37iG1qE44TaHU58ntC7npVsvq" width="603" height="76"><br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/9/5/95844f4d88177f329d276e4027c6e6a3f201fc99.png" alt="5. AttributeError 'list' object has no attribute 'item'" data-base62-sha1="lkGC9VCD811wdG2Pjl328nTj06J" width="500" height="68"></p>

username: srishti-git1110
cooked: <p>Hello,<br>
According to me, <code>ratings</code> is probably a list of tensors.<br>
Can you please confirm? Also, please brief what exactly you are trying to achieve.</p>
<p>Not sure if it helps your use case, but here’s a way to convert a list of tensors into a list of numpy arrays -</p>
<pre><code class="lang-auto">import torch
list_of_tensors = []
x = torch.tensor([1.0, 2, 3], device='cuda')
list_of_tensors.append(x)

x = torch.tensor([4.0, 5, 6], device='cuda')
list_of_tensors.append(x)

list_of_tensors
</code></pre>
<p>out -</p>
<pre><code class="lang-auto">[tensor([1., 2., 3.], device='cuda:0'), tensor([4., 5., 6.], device='cuda:0')]
</code></pre>
<p>Conversion -</p>
<pre><code class="lang-auto">list_of_arrays = [t.cpu().numpy() for t in list_of_tensors]
list_of_arrays
</code></pre>
<p>out -</p>
<pre><code class="lang-auto">[array([1., 2., 3.], dtype=float32), array([4., 5., 6.], dtype=float32)]
</code></pre>

username: naychelynn
cooked: <p>Thanks for your suggestions.</p>
<ul>
<li>In fact, I’ve also tested that way using .tensor function but got the value error “ValueError: only one element tensors can be converted to Python”.</li>
<li>For the second way of using .cpu().numpy() in the loop got the same error " TypeError: can’t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first. " as in my very first screenshot. <img src="https://discuss.pytorch.org/images/emoji/apple/pleading_face.png?v=12" title=":pleading_face:" class="emoji" alt=":pleading_face:" loading="lazy" width="20" height="20"></li>
</ul>
<p>Let me add the screenshot with that variable.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/1/418bd68842dc147c86be1f8d43322f8a5f74cb99.png" data-download-href="https://discuss.pytorch.org/uploads/default/418bd68842dc147c86be1f8d43322f8a5f74cb99" title="00"><img src="https://discuss.pytorch.org/uploads/default/original/3X/4/1/418bd68842dc147c86be1f8d43322f8a5f74cb99.png" alt="00" data-base62-sha1="9lQDvcfzmUQvs3Ty28UWjMNvxdn" width="690" height="23" data-dominant-color="3A5A6A"><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">00</span><span class="informations">980×33 1.06 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><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/5/c5bdf89db320d6cd7fe58537e7b145dd0318ec93.png" data-download-href="https://discuss.pytorch.org/uploads/default/c5bdf89db320d6cd7fe58537e7b145dd0318ec93" title="000"><img src="https://discuss.pytorch.org/uploads/default/original/3X/c/5/c5bdf89db320d6cd7fe58537e7b145dd0318ec93.png" alt="000" data-base62-sha1="sdj4wCPI5vi9bvdZiYFJ92eZknV" width="265" height="500" data-dominant-color="3B6562"><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">000</span><span class="informations">426×801 6.17 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>

username: srishti-git1110
cooked: <aside class="quote no-group" data-username="naychelynn" data-post="3" data-topic="161515">
<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/n/e68b1a/48.png" class="avatar"> naychelynn:</div>
<blockquote>
<p>In fact, I’ve also tested that way using .tensor function but got the value error “ValueError: only one element tensors can be converted to Python”.</p>
</blockquote>
</aside>
<p>This error occured as you tried to put ratings (a list of tensors) in <code>torch.tensor</code>.</p>
<p>If you want to convert your list of tensors into a list of arrays, I’ve attached the code in my previous reply.</p>
<p>You can now use <code>np.vstack</code> on <code>list_of_arrays</code>.</p>
<p>As I asked earlier, please convey maybe by means of an example what exactly do you want to achieve.</p>

username: naychelynn
cooked: <aside class="quote no-group" data-username="srishti-git1110" data-post="4" data-topic="161515">
<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/srishti-git1110/48/52515_2.png" class="avatar"> srishti-git1110:</div>
<blockquote>
<p>If you want to convert your list of tensors into a list of arrays</p>
</blockquote>
</aside>
<p>Thank you, Ms. Srishti. The append function is already used for the variable label (Tensor object) to the variable ratings (a list of Tensor). Here is the code that I am testing and got the above error.<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/d/2/d22f6cb841b8171d09d42ddcf576e657ce808658.png" alt="image" data-base62-sha1="tZnSifME4o0Xn3nU5TRZ6Ux0isU" width="396" height="113"><br>
The types of variables:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/f/3f531c38011c15c943b15ee5df15c7d92ade8271.png" data-download-href="https://discuss.pytorch.org/uploads/default/3f531c38011c15c943b15ee5df15c7d92ade8271" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/3/f/3f531c38011c15c943b15ee5df15c7d92ade8271.png" alt="image" data-base62-sha1="92c8Tyelvtl1vdtNN6NaILmWAKd" width="690" height="93" data-dominant-color="375B61"><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">897×122 3.69 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>

username: srishti-git1110
cooked: <p>Hi, please consider going through my previous reply again.</p>
<p>Use this:</p>
<pre><code class="lang-auto">ratings_arr = [t.cpu().numpy() for t in ratings]
ratings_i = np.vstack(ratings_arr)

predictions_arr = [t.cpu().numpy() for t in predictions]
predictions_i = np.vstack(predictions_arr)
</code></pre>
<p>Let me know if you still face the error.</p>

username: R_Japh
cooked: <p>Hi, Srishti-git110, you sure are of help in this forum. I’d appreciate if I can get your help on this similar question;<br>
I get an error from this line:<br>
line3, = plt.plot(range(len(PredyList)),predyList, alpha = 0.8,label = ‘Predicted’)<br>
The Predy list  is now a one D list. It initially was two before i flatten. The element lists are tensors<br>
i.e. [tensor([9.5960], device=‘cuda:0’, grad_fn=), tensor([10.0286],<br>
This is the error i get:<br>
can’t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.</p>
<p>SEARCH STACK OVERFLOW</p>

username: srishti-git1110
cooked: <p>Hey <a class="mention" href="/u/r_japh">@R_Japh</a> ,<br>
Sorry for getting back so late.</p>
<p>As the error message says, you’re trying to use a tensor that’s on GPU as a numpy array which isn’t supported by numpy yet.</p>
<p>So, it’s the first element of PredyList that’s problematic -</p>
<aside class="quote no-group" data-username="R_Japh" data-post="7" data-topic="161515">
<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/r_japh/48/63008_2.png" class="avatar"> R_Japh:</div>
<blockquote>
<p>tensor([9.5960], device=‘cuda:0’, grad_fn=)</p>
</blockquote>
</aside>
<p>You’ll need to ensure that each item of your list is a tensor on the CPU.<br>
Use this to copy tensors in PredyList from GPU to the host memory -</p>
<pre><code class="lang-auto">PredyList = [t.cpu() for t in PredyList]
plt.plot(PredyList)
plt.show()
</code></pre>