我正在更改/分配数组上的值(torch.cuda.floatTensor)。我尝试了某种方式,但它不起作用。请帮我!
#1
#dis is [torch.cuda.FloatTensor of size 3185x1 (GPU 0)]
s = dis.size(0) #3185
for i in range (0,s,1):
if (dis[i,0] < 0):
dis[i,0]== 0
#There is no error but It does not work.
#2
#dis is [torch.cuda.FloatTensor of size 3185x1 (GPU 0)]
s = dis.size(0)
a = torch.zeros(s, 1).cuda()
idx = (dis > a)
dis[idx] = a[idx]
AssertionError: can't compare Variable and tensor
#3
#dis is [torch.cuda.FloatTensor of size 3185x1 (GPU 0)]
s = dis.size(0)
a = torch.zeros(s, 1).cuda()
for i in range (0,s,1):
if (dis[i,0] < a[i, 0]):
dis[i,0]==a[i, 0]
#RuntimeError: bool value of Variable objects containing non-empty torch.cuda.ByteTensor is ambiguous
IIUC,你需要用0替换小于0的值,只需使用torch.clamp,这是用于这种用例:
dis = dis.clamp(min=0)
例:
import torch
dis = torch.tensor([[1], [-3], [0]])
#tensor([[ 1],
# [-3],
# [ 0]])
dis.clamp(min=0)
#tensor([[1],
# [0],
# [0]])