使用现有门创建异或门

我遇到了一个逻辑门的问题。我试图通过组合现有的门如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

所以目标是创建两个异或函数。

  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函数的权重吗?我如何在异或函数中使用已经创建的门?希望能得到正确的指导!

谢谢!


回答:

单层感知器无法实现异或操作。因此需要多层感知器

例如:

tmp1 = f(x1+x2-0.5), tmp2 = f(x1-x2+1.5)out = f(tmp1 + tmp2 - 1.5)其中f()是sigmoid函数

enter image description here

enter image description here

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注