Convert to numpy cuda variable

pronics2004: <p>How to convert cuda variables to numpy?</p>

fmassa: <p>You first need to convert them to <code>cpu</code>.</p>
<pre><code class="lang-python">cuda_tensor = torch.rand(5).cuda()
np_array = cuda_tensor.cpu().numpy()
</code></pre>

apaszke: <p>That’s because numpy doesn’t support CUDA, so there’s no way to make it use GPU memory without a copy to CPU first. Remember that <code>.numpy()</code> doesn’t do any copy, but returns an array that uses the same memory as the tensor.</p>

Adel_Saleh1: <p>I am facing this problem while try to convert  torch.FloatTensor to numpy<br>
pleas help what is the problem?</p>
<pre><code>import cv2
img=cv2.imread('xx.jpg')
img=cv2.resize(img,(32,32))
img = img.transpose((2,0,1))
img=np.expand_dims(img,axis=0)
img=img/255.0
img=torch.FloatTensor(img)
img=Variable(img)
img = img.cuda()

 output=net(img)

print(output)
print('---------------------------------------------------')

np_array = output.cpu()
print (np_array)
print('---------------------------------------------------')

np_array = np_array.numpy()
print (np_array)
print('---------------------------------------------------')
</code></pre>
<p>here wht I got:<br>
Variable containing:<br>
0.1437  0.1254 -0.2880 -0.3866 -0.1179  0.3432 -0.1611 -0.4105  0.0947 -0.7357<br>
[torch.cuda.FloatTensor of size 1x10 (GPU 0)]</p>
<hr>
<p>Variable containing:<br>
0.1437  0.1254 -0.2880 -0.3866 -0.1179  0.3432 -0.1611 -0.4105  0.0947 -0.7357<br>
of size 1x10]</p>
<hr>
<p>Traceback (most recent call last):<br>
File “/main.py”, line 186, in </p>
<pre><code>np_array = np_array.numpy()


AttributeError: numpy</code></pre>

spruceb: <p>Try <code>np_array.data.numpy()</code>, I don’t think <code>Variables</code> can be directly “converted”.</p>

Adel_Saleh1: <p>Thanks … fixed that</p>

ahmedmazari: <p>Hello,</p>
<p>what’s wrong with my model</p>
<pre><code>m=torch.nn.Softmax()
model.eval()
preds = model(image)
temps=preds.cpu()
prob=torch.max(m(temps)*100)
</code></pre>
<p>error with prob variable<br>
assert input.dim() == 2, 'Softmax requires a 2D tensor as input’<br>
AssertionError: Softmax requires a 2D tensor as input</p>

pvskand: <p>I don’t think this is a problem of numpy to cuda variable.<br>
The softmax function requires a <code>2D Tensor</code> as input and I guess you are giving a single<br>
vector as input.</p>
<p>Please go through the documentation of Softmax:  <a href="http://pytorch.org/docs/master/nn.html#torch.nn.Softmax" rel="nofollow noopener">http://pytorch.org/docs/master/nn.html#torch.nn.Softmax</a></p>

dhpollack: <p>Is there a reason that the numpy() call doesn’t check if the tensor is on the gpu and simply do .cpu().numpy()?</p>

smth: <p><a class="mention" href="/u/dhpollack">@dhpollack</a> it’s a matter of taste. We felt that doing <code>.numpy()</code> on GPU Tensors might introduce silent / careless performance bugs in user code.</p>

Mona_Jalal: <p>In my case I also had to use .detach()</p>
<pre><code class="lang-auto">
output.cpu().detach().numpy()
</code></pre>
<p>else I get this error:<br>
<code>RuntimeError: Can't call numpy() on Variable that requires grad. Use var.detach().numpy() instead.</code></p>

Golnoush123: <p>I am using it as below<br>
loss_semi_adv_value += loss_semi_adv.data.cpu().numpy()[0]/args.lambda_semi_adv<br>
I get this error. Does anybody know the reason?</p>
<pre><code class="lang-auto">loss_semi_adv_value += loss_semi_adv.data.cpu().numpy()[0]/args.lambda_semi_adv
IndexError: too many indices for array
</code></pre>