How to fix: 'can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.'

username: demmzy419
cooked: <p>Hi guys,</p>
<p>I’m trying to make a plot and I get this error, here’s a summary of my code:</p>
<pre><code class="lang-auto">### Visualize

x_arr = np.arange(len(model_sum[0])) + 1
fig = plt.figure(figsize=(12, 4))
ax = fig.add_subplot(1, 2, 1)
ax.plot(x_arr, model_sum[0], '-o', label='Train Loss')
ax.plot(x_arr, model_sum[1], '--&lt;', label='Validation Loss')
ax.legend(fontsize=15)
ax = fig.add_subplot(1, 2, 2)
ax.plot(x_arr, model_sum[2], '-o', label='Train Acc.').unsqueeze(0).cuda().cpu()
ax.plot(x_arr, model_sum[3], '-&lt;', label='Validation Acc.')
ax.legend(fontsize=15)
ax.set_xlabel('Epoch', size=15)
ax.set_ylabel('Accuracy', size=15)
plt.show()
</code></pre>
<p>Error:</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [49], in &lt;cell line: 11&gt;()
      9 ax.legend(fontsize=15)
     10 ax = fig.add_subplot(1, 2, 2)
---&gt; 11 ax.plot(x_arr, model_sum[2], '-o', label='Train Acc.').unsqueeze(0).cuda().cpu()
     12 ax.plot(x_arr, model_sum[3], '-&lt;', label='Validation Acc.')
     13 ax.legend(fontsize=15)

File ~\anaconda3\envs\myai\lib\site-packages\matplotlib\axes\_axes.py:1635, in Axes.plot(self, scalex, scaley, data, *args, **kwargs)
   1393 """
   1394 Plot y versus x as lines and/or markers.
   1395 
   (...)
   1632 (``'green'``) or hex strings (``'#008000'``).
   1633 """
   1634 kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-&gt; 1635 lines = [*self._get_lines(*args, data=data, **kwargs)]
   1636 for line in lines:
   1637     self.add_line(line)

File ~\anaconda3\envs\myai\lib\site-packages\matplotlib\axes\_base.py:312, in _process_plot_var_args.__call__(self, data, *args, **kwargs)
    310     this += args[0],
    311     args = args[1:]
--&gt; 312 yield from self._plot_args(this, kwargs)

File ~\anaconda3\envs\myai\lib\site-packages\matplotlib\axes\_base.py:488, in _process_plot_var_args._plot_args(self, tup, kwargs, return_kwargs)
    486 if len(xy) == 2:
    487     x = _check_1d(xy[0])
--&gt; 488     y = _check_1d(xy[1])
    489 else:
    490     x, y = index_of(xy[-1])

File ~\anaconda3\envs\myai\lib\site-packages\matplotlib\cbook\__init__.py:1311, in _check_1d(x)
   1305 # plot requires `shape` and `ndim`.  If passed an
   1306 # object that doesn't provide them, then force to numpy array.
   1307 # Note this will strip unit information.
   1308 if (not hasattr(x, 'shape') or
   1309         not hasattr(x, 'ndim') or
   1310         len(x.shape) &lt; 1):
-&gt; 1311     return np.atleast_1d(x)
   1312 else:
   1313     return x

File &lt;__array_function__ internals&gt;:180, in atleast_1d(*args, **kwargs)

File ~\anaconda3\envs\myai\lib\site-packages\numpy\core\shape_base.py:65, in atleast_1d(*arys)
     63 res = []
     64 for ary in arys:
---&gt; 65     ary = asanyarray(ary)
     66     if ary.ndim == 0:
     67         result = ary.reshape(1)

File ~\anaconda3\envs\myai\lib\site-packages\torch\_tensor.py:757, in Tensor.__array__(self, dtype)
    755     return handle_torch_function(Tensor.__array__, (self,), self, dtype=dtype)
    756 if dtype is None:
--&gt; 757     return self.numpy()
    758 else:
    759     return self.numpy().astype(dtype, copy=False)

TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

</code></pre>
<p>I’ve tried different methods suggested by forum members and none seem to work</p>
<p>Thanks</p>

username: AlphaBetaGamma96
cooked: <p>Hi <a class="mention" href="/u/demmzy419">@demmzy419</a>,</p>
<aside class="quote no-group" data-username="demmzy419" data-post="1" data-topic="159656">
<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/demmzy419/48/52442_2.png" class="avatar"> demmzy419:</div>
<blockquote>
<pre><code class="lang-auto">ax.plot(x_arr, model_sum[0], '-o', label='Train Loss')
</code></pre>
</blockquote>
</aside>
<p>Could you try,</p>
<pre><code class="lang-auto">ax.plot(x_arr, model_sum[0].cpu().detach().numpy(), '-o', label='Train Loss')
</code></pre>
<p>When you have your data on the GPU, and you pass it to a function which contains a <code>numpy</code> operation, you need to first move your Tensor to the CPU then detach to numpy via, <code>.cpu().detach().numpy()</code> as <code>numpy</code> is a CPU only python package</p>

username: demmzy419
cooked: <aside class="quote no-group" data-username="AlphaBetaGamma96" data-post="2" data-topic="159656">
<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/a/3da27b/48.png" class="avatar"> AlphaBetaGamma96:</div>
<blockquote>
<p><code>ax.plot(x_arr, model_sum[0].cpu().detach().numpy(), '-o', label='Train Loss')</code></p>
</blockquote>
</aside>
<p>Thanks for your response. After changing I got an attribute error:</p>
<pre><code class="lang-auto">AttributeError: 'list' object has no attribute 'cpu'
</code></pre>

username: AlphaBetaGamma96
cooked: <p>So, <code>model_sum[0]</code> is a list which you might need to un-pack this further via <code>model_sum[0][0]</code> but that depends how <code>model_sum</code> is created. Can you share the code that creates <code>model_sum</code>?</p>
<p>In short, you just need to extract out  a 1d-array so that you can plot it via matplotlib.</p>

username: demmzy419
cooked: <aside class="quote no-group" data-username="AlphaBetaGamma96" data-post="2" data-topic="159656">
<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/a/3da27b/48.png" class="avatar"> AlphaBetaGamma96:</div>
<blockquote>
<p><code>.cpu().detach().numpy()</code></p>
</blockquote>
</aside>
<p>My model_sum was used to train the model, here’s the code:</p>
<pre><code class="lang-auto"># Train the Model:

def train(model, num_epochs, train_dl, valid_dl):
    loss_hist_train = [0]*num_epochs
    accuracy_hist_train = [0]*num_epochs
    loss_hist_valid = [0]*num_epochs
    accuracy_hist_valid = [0]*num_epochs
    
    for epoch in range(num_epochs):
        model.train()
        for x_batch, y_batch in train_dl:
            x_batch = x_batch.to(device)
            pred = model(x_batch)
            y_batch = y_batch.to(device)
            loss = loss_func(pred, y_batch)
            loss.backward()
            optimizer.step()
            optimizer.zero_grad()
            loss_hist_train[epoch] += loss.item()*y_batch.size(0)
            is_correct = (torch.argmax(pred, dim=1)==y_batch).float()
            accuracy_hist_train[epoch] += is_correct.sum()
        loss_hist_train[epoch] /= len(train_dl.dataset)
        accuracy_hist_train[epoch] /= len(train_dl.dataset)
        
        model.eval()
        
        with torch.no_grad():
            for x_batch, y_batch in valid_dl:
                x_batch = x_batch.to(device)
                pred = model(x_batch)
                y_batch = y_batch.to(device)
                loss = loss_func(pred, y_batch)
                loss_hist_valid[epoch] += loss.item()*y_batch.size(0)
                is_correct = (torch.argmax(pred, dim=1)==y_batch).float()
                accuracy_hist_valid[epoch] += is_correct.sum()
            loss_hist_valid[epoch] /= len(valid_dl.dataset)
            accuracy_hist_valid[epoch] /= len(valid_dl.dataset)
            
            print(f'Epoch {epoch+1} accuracy: '
                  f'{accuracy_hist_train[epoch]:.4f} val_accuracy: '
                  f'{accuracy_hist_valid[epoch]:.4f}')
    return loss_hist_train, loss_hist_valid, accuracy_hist_train, accuracy_hist_valid
</code></pre>
<p>then:</p>
<pre><code class="lang-auto">model_sum = train(model, num_epochs, train_dataset, valid_dataset)
</code></pre>
<p>Thanks</p>

username: AlphaBetaGamma96
cooked: <p>So, the issue isn’t with <code>model_sum[0]</code> it’s with <code>model_sum[2]</code>, so remove the <code>.cpu().detach().numpy()</code> comment I stated earlier and replace,</p>
<aside class="quote no-group" data-username="demmzy419" data-post="5" data-topic="159656">
<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/demmzy419/48/52442_2.png" class="avatar"> demmzy419:</div>
<blockquote>
<pre><code class="lang-auto">accuracy_hist_valid[epoch] += is_correct.sum()
</code></pre>
</blockquote>
</aside>
<p>with,</p>
<pre><code class="lang-auto">accuracy_hist_valid[epoch] += is_correct.sum().item()
</code></pre>
<p>which is a Tensor on the GPU but needs to be moved to the CPU for matplotlib which you can do via <code>.item()</code> as the value is a scalar (in general it’s <code>.cpu().detach().numpy()</code>). The stacktrace shows this here,</p>
<aside class="quote no-group" data-username="demmzy419" data-post="1" data-topic="159656">
<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/demmzy419/48/52442_2.png" class="avatar"> demmzy419:</div>
<blockquote>
<pre><code class="lang-auto">---&gt; 11 ax.plot(x_arr, model_sum[2], '-o', label='Train Acc.').unsqueeze(0).cuda().cpu()
</code></pre>
</blockquote>
</aside>
<p>(Also, remove the .unsqueeze(0).cuda().cpu() too, that isn’t applicable to matplotlib)</p>

username: demmzy419
cooked: <aside class="quote no-group" data-username="AlphaBetaGamma96" data-post="6" data-topic="159656">
<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/a/3da27b/48.png" class="avatar"> AlphaBetaGamma96:</div>
<blockquote>
<p><code>accuracy_hist_valid[epoch] += is_correct.sum().item()</code></p>
</blockquote>
</aside>
<p>I changed it but my error looked the same:</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [56], in &lt;cell line: 11&gt;()
      9 ax.legend(fontsize=15)
     10 ax = fig.add_subplot(1, 2, 2)
---&gt; 11 ax.plot(x_arr, model_sum[2], '-o', label='Train Loss')
     12 ax.plot(x_arr, model_sum[3], '-&lt;', label='Validation Acc.')
     13 ax.legend(fontsize=15)

File ~\anaconda3\envs\myai\lib\site-packages\matplotlib\axes\_axes.py:1635, in Axes.plot(self, scalex, scaley, data, *args, **kwargs)
   1393 """
   1394 Plot y versus x as lines and/or markers.
   1395 
   (...)
   1632 (``'green'``) or hex strings (``'#008000'``).
   1633 """
   1634 kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-&gt; 1635 lines = [*self._get_lines(*args, data=data, **kwargs)]
   1636 for line in lines:
   1637     self.add_line(line)

File ~\anaconda3\envs\myai\lib\site-packages\matplotlib\axes\_base.py:312, in _process_plot_var_args.__call__(self, data, *args, **kwargs)
    310     this += args[0],
    311     args = args[1:]
--&gt; 312 yield from self._plot_args(this, kwargs)

File ~\anaconda3\envs\myai\lib\site-packages\matplotlib\axes\_base.py:488, in _process_plot_var_args._plot_args(self, tup, kwargs, return_kwargs)
    486 if len(xy) == 2:
    487     x = _check_1d(xy[0])
--&gt; 488     y = _check_1d(xy[1])
    489 else:
    490     x, y = index_of(xy[-1])

File ~\anaconda3\envs\myai\lib\site-packages\matplotlib\cbook\__init__.py:1311, in _check_1d(x)
   1305 # plot requires `shape` and `ndim`.  If passed an
   1306 # object that doesn't provide them, then force to numpy array.
   1307 # Note this will strip unit information.
   1308 if (not hasattr(x, 'shape') or
   1309         not hasattr(x, 'ndim') or
   1310         len(x.shape) &lt; 1):
-&gt; 1311     return np.atleast_1d(x)
   1312 else:
   1313     return x

File &lt;__array_function__ internals&gt;:180, in atleast_1d(*args, **kwargs)

File ~\anaconda3\envs\myai\lib\site-packages\numpy\core\shape_base.py:65, in atleast_1d(*arys)
     63 res = []
     64 for ary in arys:
---&gt; 65     ary = asanyarray(ary)
     66     if ary.ndim == 0:
     67         result = ary.reshape(1)

File ~\anaconda3\envs\myai\lib\site-packages\torch\_tensor.py:757, in Tensor.__array__(self, dtype)
    755     return handle_torch_function(Tensor.__array__, (self,), self, dtype=dtype)
    756 if dtype is None:
--&gt; 757     return self.numpy()
    758 else:
    759     return self.numpy().astype(dtype, copy=False)

TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
</code></pre>

username: AlphaBetaGamma96
cooked: <aside class="quote no-group" data-username="demmzy419" data-post="1" data-topic="159656">
<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/demmzy419/48/52442_2.png" class="avatar"> demmzy419:</div>
<blockquote>
<p><code>x_arr</code></p>
</blockquote>
</aside>
<p>Is <code>x_arr</code> stored on the GPU too?</p>
<p>Check that both <code>x_arr</code> and <code>model_sum[2]</code> are numpy arrays via <code>print(type(x_arr))</code> (and same for <code>model_sum[2]</code>)</p>

username: demmzy419
cooked: <aside class="quote no-group" data-username="AlphaBetaGamma96" data-post="8" data-topic="159656">
<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/a/3da27b/48.png" class="avatar"> AlphaBetaGamma96:</div>
<blockquote>
<p>print(type(x_arr))</p>
</blockquote>
</aside>
<p>x_arr outputted <code>&lt;class 'numpy.ndarray'&gt;</code> and model_sum[2] <code>&lt;class 'list'&gt;</code></p>
<p>I did train on the GPU</p>

username: AlphaBetaGamma96
cooked: <aside class="quote no-group" data-username="AlphaBetaGamma96" data-post="6" data-topic="159656">
<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/a/3da27b/48.png" class="avatar"> AlphaBetaGamma96:</div>
<blockquote>
<pre><code class="lang-auto">accuracy_hist_valid[epoch] += is_correct.sum().item()
</code></pre>
</blockquote>
</aside>
<p>Can you do the same for <code>accuracy_hist_train</code> too?</p>
<p>You currently have a list of Tensors on the GPU whereas matplotlib will require a list of scalars on the CPU.</p>

username: demmzy419
cooked: <p>My <code>accuracy_hist_train</code> and <code>accuracy_hist_valid</code> are both <code>&lt;class 'list'&gt;</code></p>

username: AlphaBetaGamma96
cooked: <p>yes but they are a list of GPU tensors, do <code>print(accuracy_hist_train[0])</code></p>
<p>For example,</p>
<pre><code class="lang-auto">import torch
 
mylist = [torch.randn(1, device='cuda') for _ in range(10)]

print(type(mylist)) #returns &lt;class 'list'&gt;
print(type(mylist[0])) #returns &lt;class 'torch.Tensor'&gt;
print(mylist[0].device) #returns cuda:0 &lt;-------- a problem with matplotlib
</code></pre>

username: demmzy419
cooked: <p>Ahhhh, accuracy_hist_train prints <code>&lt;class 'torch.Tensor'&gt;</code> whilst accuracy_hist_valid prints <code>&lt;class 'float'&gt;</code></p>

username: AlphaBetaGamma96
cooked: <p>So give this a go, and see if it solves your problem.</p>
<aside class="quote no-group" data-username="AlphaBetaGamma96" data-post="10" data-topic="159656" data-full="true">
<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/a/3da27b/48.png" class="avatar"> AlphaBetaGamma96:</div>
<blockquote>
<pre><code class="lang-auto">accuracy_hist_valid[epoch] += is_correct.sum().item()
</code></pre>
<p>Can you do the same for <code>accuracy_hist_train</code> too?</p>
<p>You currently have a list of Tensors on the GPU whereas matplotlib will require a list of scalars on the CPU.</p>
</blockquote>
</aside>

username: demmzy419
cooked: <p>Thanks for the help, it worked now!</p>
<p>Cheers!</p>