Changing back to CPU with Google Colab
I am running a model on Google Colab.  The final step I would like to do is print an image, and show the top 5 classification predictions of the model.  Here is the code:

image = process_image(imgpath)

index = 17
plot = imshow(image, ax = plt)
plot.axis('off')
plot.title(cat_to_name[str(index)])
plot.show()

axes = predict(image, model)

yaxis = [cat_to_name[str(i)] for i in np.array(axes[1][0])]
y_pos = np.arange(len(yaxis))
xaxis = np.array(axes[0][0])   

plt.barh(y_pos, xaxis)
plt.xlabel('probability')
plt.yticks(y_pos, yaxis)
plt.title('probability of flower classification')

plt.show()


I am getting this error when I run this cell:

TypeError                                 Traceback (most recent call last)
&lt;ipython-input-19-d0bb6f461eec&gt; in &lt;module&gt;()
     11 axes = predict(image, model)
     12 
---&gt; 13 yaxis = [cat_to_name[str(i)] for i in np.array(axes[1][0])]
     14 y_pos = np.arange(len(yaxis))
     15 xaxis = np.array(axes[0][0])

/usr/local/lib/python3.6/dist-packages/torch/tensor.py in __array__(self, dtype)
    447     def __array__(self, dtype=None):
    448         if dtype is None:
--&gt; 449             return self.numpy()
    450         else:
    451             return self.numpy().astype(dtype, copy=False)

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


Is there a way to temporarily use CPU on Google Colab and in this particular step?  I don't really need to switch back to GPU because this is the final step in my code.

Try the following:

yaxis = [cat_to_name[str(i)] for i in axes[1][0].cpu()]
xaxis = axes[0][0].cpu().numpy() 

