我遇到了一个逻辑门的问题。我试图通过组合现有的门如OR、AND、NOR或NAND来创建一个异或门。
我有两个辅助函数:
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.00, 1: 1.01, 0: 1.01, 1: 1.0and_gate = logic_gate(15,15,-20)test(and_gate)output:0, 0: 0.00, 1: 0.01, 0: 0.01, 1: 1.0nor_gate = logic_gate(-15,-15,10)test(nor_gate)output: 0, 0: 1.00, 1: 0.01, 0: 0.01, 1: 0.0nand_gate = logic_gate(-15,-15,20)test(nand_gate)output:0, 0: 1.00, 1: 1.01, 0: 1.01, 1: 0.0
所以目标是创建两个异或函数。
-
(非a且b)或(a且非b) (¬𝑎∧𝑏)∨(𝑎∧¬𝑏)或一个稍微简化的版本
-
(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函数的权重吗?我如何在异或函数中使用已经创建的门?希望能得到正确的指导!
谢谢!
回答:
单层感知器无法实现异或操作。因此需要多层感知器
例如:
tmp1 = f(x1+x2-0.5), tmp2 = f(x1-x2+1.5)out = f(tmp1 + tmp2 - 1.5)其中f()是sigmoid函数