x = x/x.sum(0)

I saw you post this in a few places, but it doesn’t look right - why are you dividing by a sum? And you’re not taking into account negative values. i.e. the above code works only under certain conditions, and while it does make the vector’s max value lesser than 1, it could end up being lesser than 0.0001.
The following code would work correctly for any given 2d vector:
# rescale vectors to a desired range
x = torch.tensor([2, -1.0, 5, 6, 7])
if not all(x == 0): # non all-zero vector
    # linear rescale to range [0, 1]
    x -= x.min() # bring the lower range to 0
    x /= x.max() # bring the upper range to 1
    x # tensor([0.3750, 0.0000, 0.7500, 0.8750, 1.0000])
    # linear rescale to range [-1, 1]
    x = 2*x - 1
    x # tensor([-0.2500, -1.0000,  0.5000,  0.7500,  1.0000])