如何检查不同张量pytorch中的张量值? [英] How to check whether tensor values in a different tensor pytorch?

查看:56
本文介绍了如何检查不同张量pytorch中的张量值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个大小不等的张量

I have 2 tensors of unequal size

a = torch.tensor([[1,2], [2,3],[3,4]])
b = torch.tensor([[4,5],[2,3]])

我想要一个布尔数组,其中每个值是否存在于另一个张量中而无需迭代.类似的东西

I want a boolean array of whether each value exists in the other tensor without iterating. something like

a in b

结果应该是

[False, True, False]

因为只有 a[1] 的值在 b 中

as only the value of a[1] is in b

推荐答案

我认为不使用至少某种类型的迭代是不可能的.我能管理的最简洁的方法是使用列表理解:

I think it's impossible without using at least some type of iteration. The most succinct way I can manage is using list comprehension:

[True if i in b else False for i in a]

检查 b 中 a 中的元素并给出 [False, True, False].也可以反过来得到b中的元素a [False, True].

Checks for elements in b that are in a and gives [False, True, False]. Can also be reversed to get elements a in b [False, True].

这篇关于如何检查不同张量pytorch中的张量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆