【问题标题】:Filter out np.nan values from pytorch 1d tensor从 pytorch 1d 张量中过滤掉 np.nan 值
【发布时间】:2020-04-29 21:49:03
【问题描述】:

我有一个看起来像这样的一维张量:

import numpy as np
import torch

my_list = [0, 1, 2, np.nan, np.nan, 4]
tensor = torch.Tensor(my_list)

如何过滤掉 nan-values,使它变成一个大小为 4 的张量?

【问题讨论】:

    标签: python pytorch


    【解决方案1】:

    您可以使用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.])
    

    【讨论】:

      猜你喜欢
      • 2021-02-12
      • 2017-06-30
      • 2021-06-17
      • 2019-12-25
      • 2019-06-24
      • 1970-01-01
      • 2019-08-08
      • 1970-01-01
      • 2020-03-23
      相关资源
      最近更新 更多