# Why is forward pass generating nan values?

**URL:** <https://discuss.pytorch.org/t/why-is-forward-pass-generating-nan-values/112342>\
**Category:** Uncategorized\
**Created:** [February 19, 2021, 10:29am UTC](https://discuss.pytorch.org/t/why-is-forward-pass-generating-nan-values/112342 "2021-02-19T10:29:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpj](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/j/ed655f/32.png) [@jpj](https://discuss.pytorch.org/u/jpj)\
**Post date:** [February 19, 2021, 10:29am UTC](https://discuss.pytorch.org/t/why-is-forward-pass-generating-nan-values/112342/1 "2021-02-19T10:29:33Z")

</div>

```auto
def forward(self, x):

        hidden_inputs = self.hidden(x)

        hidden_outputs = self.sigmoid(hidden_inputs)

        final_inputs = self.output(hidden_outputs)

        final_outputs = self.softmax(final_inputs)

        return final_outputs

```

```auto
torch.isnan(self.features).any() # false
label_predict = self.forward(self.features)
torch.isnan(label_predict).any() # true

```

```auto

```

I checked, and it’s the nn.linear causing the output to be nan. Why is that and how can I fix that?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [February 20, 2021, 9:24am UTC](https://discuss.pytorch.org/t/why-is-forward-pass-generating-nan-values/112342/2 "2021-02-20T09:24:05Z")

</div>

Could you check the stats of the input tensor as well as the parameters of the linear layer, which is causing this issue?  
E.g. if your input contains `Infs` or very large values in their magnitude, the result might overflow and could be set to NaN in further operations.

---

<div class="post-metadata">

**Author:** ![jpj](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/j/ed655f/32.png) [@jpj](https://discuss.pytorch.org/u/jpj)\
**Post date:** [February 22, 2021, 9:46am UTC](https://discuss.pytorch.org/t/why-is-forward-pass-generating-nan-values/112342/3 "2021-02-22T09:46:09Z")

</div>

Thanks. I had checked for NaN values in preprocessing but not for Inf values.
