如何检查两个 Torch 张量或矩阵是否相等? [英] How to check if two Torch tensors or matrices are equal?
本文介绍了如何检查两个 Torch 张量或矩阵是否相等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个 Torch 命令来检查两个张量是否具有相同的内容,如果它们具有相同的内容则返回 TRUE.
I need a Torch command that checks if two tensors have the same content, and returns TRUE if they have the same content.
例如:
local tens_a = torch.Tensor({9,8,7,6});
local tens_b = torch.Tensor({9,8,7,6});
if (tens_a EQUIVALENCE_COMMAND tens_b) then ... end
我应该在这个脚本中使用什么来代替 EQUIVALENCE_COMMAND
?
What should I use in this script instead of EQUIVALENCE_COMMAND
?
我简单地尝试了 ==
但它不起作用.
I tried simply with ==
but it does not work.
推荐答案
torch.eq(a, b)
eq()
实现了 ==
运算符,将 a
中的每个元素与 b
(如果 b 是一个值)或 中的每个元素进行比较>a
及其在 b
中的对应元素(如果 b
是张量).
eq()
implements the ==
operator comparing each element in a
with b
(if b is a value) or each element in a
with its corresponding element in b
(if b
is a tensor).
来自@deltheil 的替代方案:
torch.all(tens_a.eq(tens_b))
这篇关于如何检查两个 Torch 张量或矩阵是否相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文