Search code examples
pythonpytorch

Normalizing an input and output tensors giving Nan Result


I am new python, pytorch and machine learning.I am trying to understand ANN example with my dataset. I am trying to normalize the input and output tensor. My input tensor is :

conts_total[:5]
tensor([[ 1.0000, 13.0000,  8.0000,  7.0000,  7.0000,  0.3171,  0.4800,  0.0000,
          0.0000,  0.0000,  0.1148,  1.0000,  0.0000],
        [ 3.0000, 16.0000,  8.0000,  3.0000,  9.0000,  0.4634,  0.4200,  0.0000,
          0.0000,  0.0000,  0.0820,  0.0000,  1.0000],
        [ 4.0000,  4.0000,  5.0000, 11.0000, 11.0000,  0.6829,  0.4400,  0.0000,
          0.0000,  0.0000,  0.0656,  0.0000,  0.0000],
        [ 5.0000, 13.0000,  4.0000, 17.0000, 17.0000,  0.6829,  0.5800,  0.0000,
          0.0000,  0.0000,  0.0000,  0.0000,  1.0000],
        [ 7.0000, 11.0000,  5.0000, 10.0000, 10.0000,  0.7317,  0.7000,  0.0000,
          0.0000,  0.0000,  0.2623,  2.0000,  0.0000]])

The shape of the input tensor is:

conts_total.shape
torch.Size([20453, 13])

When I normalize the conts_total using the below formula:

model_input = (conts_total-conts_total.min())/(conts_total.max()-conts_total.min())
model_input[:5]

The result I get it as follows:

tensor([[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan],
        [nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan],
        [nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan],
        [nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan],
        [nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan]])

It would be great help if someone can guide with normalization.

** Edit **

conts_total.min()
tensor(nan)

conts_total.max()
tensor(nan)

Solution

  • High probability that the input data itself has some nans, please check using:

    torch.isnan(tensor_name).any()