如何检查某些张量值是否在一组张量值中

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

我有一个张量列表,我想检查该列表中的任何值是否不在包含张量值的集合中,这是我尝试过的,但我看到此代码发现所有值都不在 unique_values 中,即错了!

`data_with_id = Data(x=x_without_id, edge_index=edge_index.t().contiguous(),edge_weight=edge_feats,y=ground_truth_labels) data_with_id.id_column = torch.tensor(id_column, dtype=torch.float)

#我创建一个序列,它是特定时间步内的数据对象。`

for i in range(start_timestep-1, start_timestep+ timestep-1):
                np.set_printoptions(suppress=True)
                id_column_tensor = torch.tensor(sequence[i].id_column)
                list_tensor = torch.tensor([float(f"{value:.4f}") if isinstance(value, float) else value for value in id_column_tensor])
                for j in unique_values:
                    if not torch.isin(torch.tensor(j, dtype=list_tensor.dtype), list_tensor):    
                        missing_info.append(j)

这是 unique_values 中的值:

unique_values: {tensor(5008.), tensor(7.), tensor(5004.), tensor(0.), tensor(3.1000), tensor(7.), tensor(11.2000), tensor(12.1000), tensor(17.), tensor(5008.), tensor(18.), tensor(1.), tensor(9.2000)}

python pytorch tensor
1个回答
0
投票

如果我理解你的问题正确,你想检查某个元素是否在集合中。为此,您可以将

iter()
功能与
not in
一起使用。例如:

for t in tensors:
    if t not in iter(unique_values):
        print(t)

将打印列表中

tensors
中不在集合中的任何值(例如张量)
unique_values

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.