model.py dimension misalignment
shenkev opened this issue · 1 comments
shenkev commented
There's a dimension misalignment error in the l2norm function when trying to expand the "norm" variable.
Log:
features = l2norm(features)
File "/media/shenkev/data/Ubuntu/vsepp/model.py", line 17, in l2norm
X = torch.div(X, norm.expand_as(X))
File "/usr/local/lib/python2.7/dist-packages/torch/autograd/variable.py", line 725, in expand_as
return Expand.apply(self, (tensor.size(),))
File "/usr/local/lib/python2.7/dist-packages/torch/autograd/_functions/tensor.py", line 111, in forward
result = i.expand(*new_size)
RuntimeError: The expanded size of the tensor (1024) must match the existing size (128) at non-singleton dimension 1. at /pytorch/torch/lib/THC/generic/THCTensor.c:323
I think you can fix this by using unsqueeze instead of expand_as, shown below
def l2norm(X):
"""L2-normalize columns of X
"""
norm = torch.pow(X, 2).sum(dim=1).sqrt()
X = torch.div(X, norm.unsqueeze(1))
return X