【问题标题】:Creating an XOR gate with existing gates使用现有门创建 XOR 门
【发布时间】:2021-07-26 19:52:07
【问题描述】:

所以我遇到了一个逻辑门问题。 我正在尝试通过组合现有的门(如 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 or b) 而不是 (a and 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 函数中使用已经创建的门? 向正确的方向推进将不胜感激!

谢谢!

【问题讨论】:

    标签: python machine-learning neural-network logical-operators


    【解决方案1】:

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

    例如:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多