使用现有门创建 XOR 门

Creating an XOR gate with existing gates

所以我遇到了逻辑门问题。 我正在尝试通过组合现有门(如 OR、AND、NOR 或 NAND)来创建 XOR 门。

我有 2 个辅助函数:

def logic_gate(w1, w2, b):  # weight_x1, weight_x2, and bias
    return lambda x1, x2: sigmoid(w1 * x1 + w2 * x2 + b)

# Helper function to test out our weight functions.
def test(gate):
    for x1, x2 in (0, 0), (0, 1), (1, 0), (1, 1):
        print(f"{x1}, {x2}: {np.round(gate(x1 , x2))}")

我已经定义了其他门:

or_gate = logic_gate(20, 20, -10)
test(or_gate)
output: 
0, 0: 0.0
0, 1: 1.0
1, 0: 1.0
1, 1: 1.0

and_gate = logic_gate(15,15,-20)
test(and_gate)
output:
0, 0: 0.0
0, 1: 0.0
1, 0: 0.0
1, 1: 1.0

nor_gate = logic_gate(-15,-15,10)
test(nor_gate)
output: 
0, 0: 1.0
0, 1: 0.0
1, 0: 0.0
1, 1: 0.0

nand_gate = logic_gate(-15,-15,20)
test(nand_gate)
output:
0, 0: 1.0
0, 1: 1.0
1, 0: 1.0
1, 1: 0.0


所以目标是创建 2 个 XOR 函数。

  1. (不是a和b)或(a和不是b)(¬∧)∨(∧¬) 或稍微简化的版本

  2. (a 或 b) 而不是 (a 和 b) (∨)∧¬(∧)

函数是这样的:

def xor_gate_1(a, b):
    return ...

test(xor_gate_1)



def xor_gate_2(a, b):
    return ...


test(xor_gate_2)

我正在努力找出输入 a 和 b 的含义。它们应该像 logic_gate 函数的权重吗? 如何在 XOR 函数中使用已经创建的门? 向正确的方向推进将不胜感激!

谢谢!

单层感知器无法实现异或运算。因此需要一个多层感知器

例如:

tmp1 = f(x1+x2-0.5), tmp2 = f(x1-x2+1.5)
out = f(tmp1 + tmp2 - 1.5)

where f() is sigmoid