Generally, we create a tensor by following code:
t = torch.ones(4)
t is a tensor on cpu, How can I create it on GPU as default??
In other words , I want to create my tensors all on GPU as default.
2 Likes
torch.set_default_tensor_type('torch.cuda.FloatTensor')
13 Likes
Hi, here is my code:
import torch
torch.set_default_tensor_type(‘torch.cuda.FloatTensor’)
t = torch.rand(1,3,24,24)
and it caused bellow error:
TypeError Traceback (most recent call last)
in ()
----> 1 t = torch.rand(1,3,24,24)TypeError: Type torch.cuda.FloatTensor doesn’t implement stateless methods
Any idea for this type of error?
t = torch.Tensor(1,3,24,24).uniform_()
1 Like
use_cuda = torch.cuda.is_available()
FloatTensor = torch.cuda.FloatTensor if use_cuda else torch.FloatTensor
LongTensor = torch.cuda.LongTensor if use_cuda else torch.LongTensor
Tensor = FloatTensor
if use_cuda:
lgr.info ("Using the GPU")
X = Variable(torch.from_numpy(x_data_np).cuda()) # Note the conversion for pytorch
Y = Variable(torch.from_numpy(y_data_np).cuda())
else:
lgr.info ("Using the CPU")
X = Variable(torch.from_numpy(x_data_np)) # Note the conversion for pytorch
Y = Variable(torch.from_numpy(y_data_np))
2 Likes