0
$\begingroup$

Below function takes in 2D tensor and normalizes it using broadcasting .The issue is except all values to be in range 0-1 but the result has values outside this range . How to get all values in 2D tensor in range 0-1

def torch_normalize(tensor_list):
    means = tensor_list.mean(dim=1, keepdim=True)
    stds = tensor_list.std(dim=1, keepdim=True)
    normalized_data = (tensor_list - means) / stds
    return normalized_data

INPUT
tensor_list=tensor([[-5.6839, -7.5829, -7.2277, -6.5066, -8.4702, -7.9844, -5.6841,  1.8570,
          1.6170, -3.7592, -4.4140, -0.4981,  0.2501,  5.8463,  1.8897, -1.3968,
         -5.5402, -2.4561, -5.6819]])


Normalized result 

tensor([[-0.5981, -1.0615, -0.9748, -0.7988, -1.2780, -1.1594, -0.5981,  1.2420,
          1.1835, -0.1284, -0.2882,  0.6673,  0.8499,  2.2155,  1.2500,  0.4480,
         -0.5630,  0.1896, -0.5976]])
$\endgroup$
2
$\begingroup$

Short Answer: What you're thinking about is called min-max normalization. You want the minimum value at each component to be mapped to 0, and the max to 1. Would look like this

mins = tensor_list.min(dim=1, keepdim=True)
maxs = tensor_list.max(dim=1, keepdim=True)
normalized_data = (tensor_list - mins) / (maxs - mins)

Longer Answer: What you tried is usually called standardization, and what it achieves is to make the mean and std 0 and 1, respectively, in the standardized sample. I'll happily expand on that if needed. That condition I mentioned over the mean and std doesn't imply at all that the transformed sample would stay in the [0, 1] range, which is what you observed.

$\endgroup$
2
  • $\begingroup$ so standardization focus on values to have mean -0 and std -1 and not about the range of values to be between [0-1] where as normalization using min-maz is the opposite it focus on the range to be [0-1] and not about having mean 0 and std 1 .Am i right ? $\endgroup$ – star Feb 24 at 11:26
  • $\begingroup$ Yep, that's it! $\endgroup$ – Bananin Feb 25 at 2:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.