如何检查一维张量是否在一维张量列表中?

问题描述 投票:0回答:0

我有一个巨大的 1 号火炬张量列表

list_of_tensors
(所有维度相同)。我想检查我创建的另一个火炬张量
tensor_to_check
是否在列表中。我想从中运行一个 if 语句。 当我的
list_of_tensors
是一维时,那么声明

if tensor_to_check in list_of_tensors:
  do something

执行完美。然而,当我的

list_of_tensors
维度大于 1 时,我得到以下错误

多于一个值的Tensor的布尔值不明确

我试过了

if tensor_to_check in set(list_of_tensors):
  do something

然后即使维度 1 检查失败。

如果我尝试 torch.isin(tensor_to_check, torch.stack(list_of_tensors)) 我会得到同样的错误

我该如何解决这个问题?对所有维度执行此操作的最快方法是什么?

if-statement boolean tensor torch
© www.soinside.com 2019 - 2024. All rights reserved.