Automatically set cuda variables in pytorch
I'm trying to automatically set Variables to use cuda if a flag use_gpu is activated. 

Usually I would do:

import torch
from torch import autograd
use_gpu = torch.cuda.is_available()

foo = torch.randn(5)

if use_gpu:
    fooV = autograd.Variable(foo.cuda())
else:
    fooV = autograd.Variable(foo)


However, to do things faster when I have to initialise variables in different parts of the code I wanted to do something like this:

import torch
from torch import autograd
use_gpu = torch.cuda.is_available()

class Variable(autograd.Variable):
    def __init__(self, data, *args, **kwargs):
        if use_gpu:
            data = data.cuda()
        super(Variable, self).__init__(data, *args, **kwargs)

foo = torch.randn(5)
fooV = Variable(foo)


Unfortunately, this doesn't seem to be working and fooV doesn't contain a cuda Tensor when the use_gpu is True.

An alternative is to use type method on CPU Tensor to convert it to GPU Tensor,

if use_cuda:
    dtype = torch.cuda.FloatTensor
else:
    dtype = torch.FloatTensor

x = torch.rand(2,2).type(dtype)


Also, you can find a discussion here.
