How to convert cuda variables to numpy?

You first need to convert them to `cpu`

.

```
cuda_tensor = torch.rand(5).cuda()
np_array = cuda_tensor.cpu().numpy()
```

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 `.numpy()`

doesnâ€™t do any copy, but returns an array that uses the same memory as the tensor.

I am facing this problem while try to convert torch.FloatTensor to numpy

pleas help what is the problem?

```
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('---------------------------------------------------')
```

here wht I got:

Variable containing:

0.1437 0.1254 -0.2880 -0.3866 -0.1179 0.3432 -0.1611 -0.4105 0.0947 -0.7357

[torch.cuda.FloatTensor of size 1x10 (GPU 0)]

Variable containing:

0.1437 0.1254 -0.2880 -0.3866 -0.1179 0.3432 -0.1611 -0.4105 0.0947 -0.7357

of size 1x10]

Traceback (most recent call last):

File â€ś/main.pyâ€ť, line 186, in

```
np_array = np_array.numpy()
AttributeError: numpy
```

Try `np_array.data.numpy()`

, I donâ€™t think `Variables`

can be directly â€śconvertedâ€ť.

Thanks â€¦ fixed that

Hello,

whatâ€™s wrong with my model

```
m=torch.nn.Softmax()
model.eval()
preds = model(image)
temps=preds.cpu()
prob=torch.max(m(temps)*100)
```

error with prob variable

assert input.dim() == 2, 'Softmax requires a 2D tensor as inputâ€™

AssertionError: Softmax requires a 2D tensor as input

I donâ€™t think this is a problem of numpy to cuda variable.

The softmax function requires a `2D Tensor`

as input and I guess you are giving a single

vector as input.

Please go through the documentation of Softmax: http://pytorch.org/docs/master/nn.html#torch.nn.Softmax

Is there a reason that the numpy() call doesnâ€™t check if the tensor is on the gpu and simply do .cpu().numpy()?

@dhpollack itâ€™s a matter of taste. We felt that doing `.numpy()`

on GPU Tensors might introduce silent / careless performance bugs in user code.

In my case I also had to use .detach()

```
output.cpu().detach().numpy()
```

else I get this error:

`RuntimeError: Can't call numpy() on Variable that requires grad. Use var.detach().numpy() instead.`

I am using it as below

loss_semi_adv_value += loss_semi_adv.data.cpu().numpy()[0]/args.lambda_semi_adv

I get this error. Does anybody know the reason?

```
loss_semi_adv_value += loss_semi_adv.data.cpu().numpy()[0]/args.lambda_semi_adv
IndexError: too many indices for array
```