# Normalizing a Tensor column wise

I have a Tensor containing these values.

``````1000  10  0.5
765   5   0.35
800   7   0.09
``````

I want to normalize it column wise between 0 and 1 so that the final tensor looks like this:

``````1     1    1
0.765 0.5  0.7
0.8   0.7  0.18 (which is 0.09/0.5)
``````

Based on this question.

1 Like

I guess you just want to do `x_normed = x / x.max(0, keepdim=True)` (EDITED: added missing ``).

1 Like

I tried this code.

``````import torch

def normalize(x):
x_normed = x / x.max(0, keepdim=True)
return x_normed

t = torch.tensor([[0.0, 2, 3], [5, 10, 15], [10, 20, 30]])
print(normalize(t))

``````

This was the error.

``````Traceback (most recent call last):
File "/home/somnath/Code/ml/src/test.py", line 7, in <module>
print(normalize(t))
File "/home/somnath/Code/ml/src/test.py", line 3, in normalize
x_normed = x / x.max(0, keepdim=True)
TypeError: div() received an invalid combination of arguments - got (tuple), but expected one of:
* (Tensor other)
didn't match because some of the arguments have invalid types: (!tuple!)
* (float other)
didn't match because some of the arguments have invalid types: (!tuple!)
``````

How to correct this?

Looks like `max()` returned a tuple with one element. So, choosing the first element solved the issue. The corrected code as of PyTorch 0.4 is as below:

``````import torch

def normalize(x):
x_normed = x / x.max(0, keepdim=True)
return x_normed

t = torch.tensor([[1000, 10, 0.5], [765, 5, 0.35], [800, 7, 0.09]])
print(normalize(t))

``````

Ho yes I forgot that, it returns both the values and indices, you only want the values in your case.

1 Like

No issues, thanks all the same. If you normalize X by X max, and Y by Y max this is not quite right. You lost the X/Y ratio. What then? Should I ask this question on separate thread?

Is there a way to limit the normalised values between [-1, 1], instead of [0, 1]?

Well depends what you want as your normalization. But you can always do `2 * (t - 1)` to go from [0, 1] to [-1, 1].

1 Like