问题
I have a 1d tensor looking kinda like this:
import numpy as np
import torch
my_list = [0, 1, 2, np.nan, np.nan, 4]
tensor = torch.Tensor(my_list)
How do i filter out the nan-values, so it becomes a tensor of size 4?
回答1:
You can use torch.isnan
my_list = [0, 1, 2, np.nan, np.nan, 4]
tensor = torch.Tensor(my_list)
tensor[~torch.isnan(tensor)]
tensor([0., 1., 2., 4.])
来源:https://stackoverflow.com/questions/61503138/filter-out-np-nan-values-from-pytorch-1d-tensor