Hi guys,
I’m trying to make a plot and I get this error, here’s a summary of my code:
### 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], '--<', 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], '-<', label='Validation Acc.')
ax.legend(fontsize=15)
ax.set_xlabel('Epoch', size=15)
ax.set_ylabel('Accuracy', size=15)
plt.show()
Error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [49], in <cell line: 11>()
9 ax.legend(fontsize=15)
10 ax = fig.add_subplot(1, 2, 2)
---> 11 ax.plot(x_arr, model_sum[2], '-o', label='Train Acc.').unsqueeze(0).cuda().cpu()
12 ax.plot(x_arr, model_sum[3], '-<', 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)
-> 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:]
--> 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])
--> 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) < 1):
-> 1311 return np.atleast_1d(x)
1312 else:
1313 return x
File <__array_function__ internals>: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:
---> 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:
--> 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.
I’ve tried different methods suggested by forum members and none seem to work
Thanks