火炬逐元素逻辑运算 and/or

Torch Element-wise Logical Operation and/or

我正在尝试对张量执行逻辑元素运算,但似乎"and"关键字执行逻辑 , 而 "or" 关键字执行逻辑 and :

    a = torch.zeros(3)
    a[1] = 1                      -- a will be [1,0,0]
    b = torch.ones(3)
    b[3] = 0                      -- b will be [1,1,0]
    c = torch.eq(a,1) and torch.eq(b,1) 
    d = torch.eq(a,1) or  torch.eq(b,1)

我期待 c 变成 [1,0,0] 因为只有在 a b 都等于 1 的位置有 1 才有意义。我也是期望 d 变成 [1,1,0] 因为这些是 a b 等于 1 的位置。令我惊讶的是,结果完全相反! 有什么解释吗?

根据 Lua 文档:

The operator and returns its first argument if it is false; otherwise, it returns its second argument. The operator or returns its first argument if it is not false; otherwise, it returns its second argument

在这种情况下,该行为就像 "coincidence" 一样发生。在应用 and 运算符时它将 return 第二个参数 (Tensor a),在应用 or 运算符时将第一个参数 (Tensor b)。此外,张量 a 对应于元素逻辑,而张量 b 对应于元素逻辑或。