How to use if statement PyTorch using torch.FloatTensor
I am trying to use the if statement in my PyTorch code using torch.FloatTensor as data type, to speed it up into the GPU.

This is my code:

import torch
import time
def fitness(x):
     return torch.pow(x, 2)

def velocity(v, gxbest, pxbest, pybest, x, pop):
     return torch.rand(pop).type(dtype)*v + \
            torch.rand(pop).type(dtype)*(pxbest - x) + \
            torch.rand(pop).type(dtype)*(gxbest.expand(x.size(0)) - x)

dtype = torch.cuda.FloatTensor 
def main():

    pop, xmax, xmin, niter = 300000, 50, -50, 100
    v                      = torch.rand(pop).type(dtype)
    x                      = (xmax-xmin)*torch.rand(pop).type(dtype)+xmin
    y                      = fitness(x)
    [miny, indexminy]      = y.min(0)
    gxbest                 = x[indexminy] 
    pxbest                 = x
    pybest                 = y

    for K in range(niter):

        vnext = velocity(v, gxbest, pxbest, pybest, x, pop)

        xnext = x + vnext
        ynext = fitness(x)
        [minynext, indexminynext]  = ynext.min(0)

        if (minynext &lt; miny):
            miny   = minynext
            gxbest = xnext[indexminynext]

        indexpbest         = (ynext &lt; pybest)
        pxbest[indexpbest] = xnext[indexpbest]
        pybest[indexpbest] = ynext[indexpbest]
        x                  = xnext
        v                  = vnext
main()


Unfortanally it is not working. It is giving me a error message and I can not figure it out what is the problem. 


  RuntimeError: bool value of non-empty torch.cuda.ByteTensor objects is ambiguous


How can I use the if in PyTorch? I tried to convert the cuda.Tensor into a numpy array but it did not work also.  

  minynext = minynext.cpu().numpy()
  miny = miny.cpu().numpy()


PS: Am I doing the code the efficient/faster way possible ? Or should I change something to achieve faster results?

If you look into the following simple example:

import torch

a = torch.LongTensor([1])
b = torch.LongTensor([5])

print(a &gt; b)


Output:

 0
[torch.ByteTensor of size 1]


Comparing tensors a and b results in a torch.ByteTensor which is obviously not equivalent to boolean. So, you can do the following.

print(a[0] &gt; b[0]) # False


So, you should change your if condition as follows.

if (minynext[0] &lt; miny[0])

