Convert to numpy cuda variable
How to convert cuda variables to numpy?
pronics2004
<SCODE>cuda_tensor = torch.rand(5).cuda()
np_array = cuda_tensor.cpu().numpy()
<ECODE>
fmassa
apaszke
<SCODE>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('---------------------------------------------------')
<ECODE> <SCODE>np_array = np_array.numpy()


AttributeError: numpy<ECODE>
Adel_Saleh1
spruceb
Thanks … fixed that
Adel_Saleh1
Hello, what’s wrong with my model <SCODE>m=torch.nn.Softmax()
model.eval()
preds = model(image)
temps=preds.cpu()
prob=torch.max(m(temps)*100)
<ECODE>
ahmedmazari
pvskand
Is there a reason that the numpy() call doesn’t check if the tensor is on the gpu and simply do .cpu().numpy()?
dhpollack
smth
In my case I also had to use .detach() <SCODE>
output.cpu().detach().numpy()
<ECODE>
Mona_Jalal
<SCODE>loss_semi_adv_value += loss_semi_adv.data.cpu().numpy()[0]/args.lambda_semi_adv
IndexError: too many indices for array
<ECODE>
Golnoush123