Python 是否有一种方法可以实现Pytorch元素平等性,将每个维度视为一个元素?
我有两个张量,我想检查是否相等,将一维数组作为元素 我有两个张量Python 是否有一种方法可以实现Pytorch元素平等性,将每个维度视为一个元素?,python,pytorch,Python,Pytorch,我有两个张量,我想检查是否相等,将一维数组作为元素 我有两个张量 lo = torch.Tensor(([1., 1., 0.], [0., 1., 1.], [0., 0., 0.], [1., 1., 1.])) lo = torch.Tensor(([1., 1., 0.], [0., 0., 0.],
lo = torch.Tensor(([1., 1., 0.],
[0., 1., 1.],
[0., 0., 0.],
[1., 1., 1.]))
lo = torch.Tensor(([1., 1., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]))
我试过使用
torch.eq(lee,lo)
返回一个类似张量的
tensor([[1, 1, 1],
[1, 0, 0],
[1, 1, 1],
[0, 0, 0]], dtype=torch.uint8)
有没有办法让输出变成
tensor([1, 0, 1, 0])
因为唯一匹配的完整元素是第一个
编辑:
我想出了这个解决办法
lee = lee.tolist()
lo = lo.tolist()
out = []
for i, j in enumerate(lee):
if j == lo[i]:
out.append(1)
else:
out.append(0)
结果是[1,0,1,0]
但有没有更简单的方法呢?或者拿火炬。eq(lee,lo)和row必须加总到它的len,这意味着所有1必须在那里
import torch
lo = torch.Tensor(([1., 1., 0.],
[0., 1., 1.],
[0., 0., 0.],
[1., 1., 1.]))
l1 = torch.Tensor(([1., 1., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]))
teq = torch.eq(l1, lo)
print(teq)
tsm = teq.sum(-1)
print(tsm == 3)
tsm是张量([3,1,3,0])
打印输出返回[1,0,1,0]您只需使用
torch.all(张量,dim)
代码:
输出:
tensor([[1, 1, 1],
[1, 0, 0],
[1, 1, 1],
[0, 0, 0]], dtype=torch.uint8)
tensor([0, 0, 0], dtype=torch.uint8)
tensor([1, 0, 1, 0], dtype=torch.uint8) # your desired output
走曼哈顿的距离,看看哪一行加起来等于零。现在,我要写一段代码。或者拿着火炬。eq(lee,lo)和row必须和它的len相加,意味着所有的1都必须在那里,这太棒了!非常感谢。或者您执行tsub=torch.abs(lo-l1)tsm2=tsub.sum(-1)打印(tsm2==0),但是带有eq的是main方法刚刚找到了一个更好的方法,只需执行tmin=teq.min(-1),对于至少有一个0保留的行,将为0
tensor([[1, 1, 1],
[1, 0, 0],
[1, 1, 1],
[0, 0, 0]], dtype=torch.uint8)
tensor([0, 0, 0], dtype=torch.uint8)
tensor([1, 0, 1, 0], dtype=torch.uint8) # your desired output