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$
0

1 Answer 1

1
$\begingroup$

The normalisation you do does not re-scale to $[0,1]$ range! It normalises to have mean $0$ and std $1$ instead.

To scale the tensor to be in $[0,1]$ range you should subtract min value and divide by absolute max-min value.

$\endgroup$
1
  • $\begingroup$ thanks for the info $\endgroup$
    – star
    Feb 24, 2021 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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