Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.nansum function

PyTorch torch.nansum function


PyTorch torch reference manual PyTorch torch reference manual

torch.nansum is a function in PyTorch used to return the sum of the non-NaN values of a tensor. Unlike sum, it ignores all NaN values.

Function definition

torch.nansum(input, dim, keepdim=False)


Usage examples

Example

import

torch

x

=

torch.

tensor

(

[

1.0

,

float

(

'nan'

)

,

3.0

,

2.0

,

5.0

]

)

Return the sum of non-NaN values

print

(

"Sum of non-NaN values:"

,

torch.

nansum

(

x

)

)

Sum of non-NaN values along dim=0

y

=

torch.

tensor

(

[

[

1.0

,

float

(

'nan'

)

,

2.0

]

,

[

4.0

,

1.0

,

3.0

]

]

)

print

(

"dim=0 sum of non-NaN values:"

,

torch.

nansum

(

y

,

dim

=

0

)

)

print

(

"dim=1 sum of non-NaN values:"

,

torch.

nansum

(

y

,

dim

=

1

)

)

The output is:

非NaN值之和: tensor(11.)

dim=0 非NaN值之和: tensor([5., 1., 5.])

dim=1 非NaN值之和: tensor([3., 8.])


PyTorch torch reference manual PyTorch torch reference manual