Filter out np.nan values from pytorch 1d tensor

北城以北 提交于 2021-02-10 05:14:45

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!