是否可以检查张量的元素是否超出边界?

Is it possible to check if elements of a tensor are out of boundaries?

是否可以使用 PyTorchGPU 方法中的 torch.cuda.FloatTensor 检查张量的元素是否超出边界?

示例 (check limits):

for i in range(pop):
    if (x[i]>xmax):
        x[i]=xmax
    elif (x[i]<xmin):
        x[i]=xmin

我尝试了以下方法,但没有加速:

idxmax    = (x > xmax) # elements that are bigger that upper limit
idxmim    = (x < min)  # elements that are smaller that upper limit
x[idxmax] = xmax
x[idxmin] = xmin

如果没有,是否可以仅使用 CPU 来完成此 check limits 部分?如何?

您可以获得张量的 CPU 副本 x,执行您的操作,然后再次将张量推送到 GPU 内存。

x = x.cpu()  # get the CPU copy
# do your operations
x = x.cuda() # move the object back to cuda memory