Hello,
I receive this error message following the launch of this function, I confess that it is a little fuzzy, especially since my colleague did not specify his variables, on his computer the code works as it is. So I don’t see much of a code problem, maybe a version problem (I’m really new, I may have wrong) ?
def disc_loss(fts, y, conf):
N,C = fts.size()
y = y.contiguous()
try:
num = y.max().data[0] + 1
except:
num = y.max().item() + 1
rs_var, rs_dist, rs_center = [], [], []
cc = []
for k in range(num):
msk = y.eq(k).view(-1,1).expand_as(fts)
val = fts.masked_select(msk).view(-1,C)
cur_center = val.mean(0)
rs_center.append(cur_center)
val = torch.norm(val-cur_center.expand_as(val),2,1)
cc.append(val)
val = val-conf['c0']
val = torch.clamp(val,min=1.e-7)
val = val*val
rs_var.append(val.mean())
dd =[]
for i in range(1,num):
for j in range(num):
if i>=j: continue
dd.append((rs_center[i]-rs_center[j]).norm(p=2))
val = 2*conf['c1']-(rs_center[i]-rs_center[j]).norm(p=2)
val = torch.clamp(val, min=1.e-7)
val = val*val
rs_dist.append(val)
rs_center_ = torch.stack(rs_center).view(-1,C)
rs_center = torch.norm(rs_center_,2,1)
rs_center = torch.clamp(rs_center, min=1.e-7)
rs_add = torch.norm(rs_center-6*conf['c0'],2,1)
rs_add = torch.clamp(rs_add, min=1.e-7)
a,b,c,d = conf['abcd']
loss = a*torch.stack(rs_var).mean() + b*torch.stack(rs_dist).mean() + d*rs_add.mean()
return loss
File "/home/VCNN/scripts/vcnnmodel.py", line 167, in disc_loss
rs_add = torch.norm(rs_center-6*conf['c0'],2,1,keepdim=True)
File "/home/anaconda3/lib/python3.7/site-packages/torch/functional.py", line 769, in norm
return torch._C._VariableFunctions.norm(input, p, dim, keepdim=keepdim)
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)
FORMAT VARIABLES
fts : tensor([[ 1.8531, -2.9964, 0.9598, ..., -0.6220, 0.0220, -0.1722],
[ 1.0035, -0.9662, 0.3225, ..., -0.6707, 0.5081, 0.3552],
[ 0.1899, -1.8759, 0.8710, ..., -0.2789, 1.3850, -0.4163],
...,
[-0.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