Is it possible to make a function transparent between cpu and gpu?
I defined a function in which there’re temporary variables are defined, for example: <SCODE>def foo(input_variable):    
    tmp = Variable(torch.zeros(input_variable.size()))    
    return torch.cat((input_variable, tmp), 1)       
<ECODE>
david-leon
by the way, you can use below to format your code it will be formated like this <SCODE>def foo(input_variable): 
     tmp = Variable(torch.zeros(input_variable.size())) 
     return torch.cat((input_variable, tmp), 1)
<ECODE> it’s  markdown sytanx
chenyuntc
Thanks, this really helps!
david-leon
The following works when X is on CPU but fails to if its not: <SCODE>>>> X = Variable(torch.zeros(2, 2))
>>> torch.Tensor.new(X.data, torch.zeros(2, 2))

 0  0
 0  0
[torch.FloatTensor of size 2x2]

>>> X = Variable(torch.zeros(2, 2)).cuda()
>>> torch.Tensor.new(X.data, torch.zeros(2, 2))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-44093d4f4ab5> in <module>()
----> 1 torch.Tensor.new(X.data, torch.zeros(2, 2))

TypeError: unbound method new() must be called with FloatTensor instance as first argument (got FloatTensor instance instead)

<ECODE>
pranav
Hi,
albanD
For batch size = 128, on my model way 1 runs 3 times slower than way 2 & 3 (~4s vs ~1.6s). Way3 is slightly faster than way 2. Is this supposed to be or am I doing wrong? Besides, is there any way to get the device a tensor currently resides on?
david-leon
what you want to do is: noise = x.data.new(x.size()).normal_() That will be the fastest.
smth
pranav
david-leon
do <SCODE>noise =x.data.new(*x.size()).normal_()
<ECODE>
chenyuntc