简体   繁体   English

使用现有门创建 XOR 门

[英]Creating an XOR gate with existing gates

So I'm stuck with a logic gate problem.所以我遇到了一个逻辑门问题。 I'm trying to create an XOR gate by combining existing gates like OR,AND,NOR or NAND.我正在尝试通过组合现有的门(如 OR、AND、NOR 或 NAND)来创建一个 XOR 门。

I have 2 helper functions:我有 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))}")

I have already defined the other gates:我已经定义了其他门:

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


So the goal is to create 2 XOR functions.所以目标是创建 2 个 XOR 函数。

  1. (not a and b) or (a and not b) (¬∧)∨(∧¬) or a somewhat simplified version (not a and b) or (a and not b) (¬∧)∨(∧¬) 或稍微简化的版本

  2. (a or b) and not (a and b) (∨)∧¬(∧) (a or b) 而不是 (a and b) (∨)∧¬(∧)

the functions are like this:功能是这样的:

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

test(xor_gate_1)



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


test(xor_gate_2)

I'm struggling to find out what a and b means as input.我正在努力找出 a 和 b 作为输入的含义。 Should they be like the weights of the logic_gate functions?它们应该像 logic_gate 函数的权重吗? How do I use the already created gates in the XOR function?如何使用 XOR function 中已经创建的门? A push into the right direction would be appreciated!朝着正确的方向推进将不胜感激!

Thanks!谢谢!

single layer perceptron can't achieve XOR op.单层感知器无法实现异或运算。 therefore need a multi-layer perceptron因此需要一个多层感知器

eg:例如:

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

where f() is sigmoid

在此处输入图像描述

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM