somnath
(Somnath Rakshit)
July 5, 2018, 3:40pm
1
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
albanD
(Alban D)
July 5, 2018, 3:49pm
2
I guess you just want to do x_normed = x / x.max(0, keepdim=True)[0]
(EDITED: added missing [0]
).
1 Like
somnath
(Somnath Rakshit)
July 6, 2018, 6:05am
3
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?
somnath
(Somnath Rakshit)
July 6, 2018, 6:23am
4
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)[0]
return x_normed
t = torch.tensor([[1000, 10, 0.5], [765, 5, 0.35], [800, 7, 0.09]])
print(normalize(t))
albanD
(Alban D)
July 6, 2018, 9:44am
5
Ho yes I forgot that, it returns both the values and indices, you only want the values in your case.
1 Like
somnath
(Somnath Rakshit)
July 6, 2018, 1:46pm
6
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]?
albanD
(Alban D)
April 3, 2020, 1:30pm
9
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