Remove "device='cuda:0' " from tensor

In below code, when tensor is move to GPU and if i find max value then output is " tensor(8, device=‘cuda:0’)". How should i get only value (8 not 'cuda:0) in assign variable?

import torch
x=torch.arange(0,9)
print("cpu ",x)
x=x.to(0)
print("gpu ",x)
y=torch.max(x)
print("max is ",y)

use y.item() instead of y. For example,

import torch

x = torch.arange(0, 9)
print("cpu ", x)
x = x.to(0)
print("gpu ", x)
y = torch.max(x)
print("max is ", y.item())

will give you,

cpu  tensor([0, 1, 2, 3, 4, 5, 6, 7, 8])
gpu  tensor([0, 1, 2, 3, 4, 5, 6, 7, 8], device='cuda:0')
max is  8

:slight_smile:

1 Like