PyTorch torch.nanmean function
PyTorch torch reference manual
torch.nanmean is a function in PyTorch used to compute the mean while ignoring NaN values.
Function definition
torch.nanmean(input, dim, keepdim=False, out=None)
Usage examples
Example
import
torch
Create a tensor containing NaN
x
=
torch.
tensor
(
[
1.0
,
2.0
,
float
(
'nan'
)
,
4.0
,
5.0
]
)
Compute the mean of non-NaN values
mean
=
torch.
nanmean
(
x
)
(
"Non-NaN mean:"
,
mean
)
The output is:
非 NaN 均值: tensor(3.)