2 位二进制输入 XNOR 逻辑门感知器算法的实现

原文:https://www . geeksforgeeks . org/2 位二进制输入 xnor 逻辑门的感知器算法实现/

在机器学习领域,感知器是一种用于二进制分类器的监督学习算法。感知器模型实现以下功能:

[ \begin{array}{c} \hat{y}=\Theta\left(w_{1} x_{1}+w_{2} x_{2}+\ldots+w_{n} x_{n}+b\right) \ =\Theta(\mathbf{w} \cdot \mathbf{x}+b) \ \text { where } \Theta(v)=\left{\begin{array}{cc} 1 & \text { if } v \geqslant 0 \ 0 & \text { otherwise } \end{array}\right. \end{array} ]

对于权重向量$\boldsymbol{w}$和偏差参数$\boldsymbol{b}$的特定选择,模型预测相应输入向量$\boldsymbol{x}$的输出$\boldsymbol{\hat{y}}$

XNOR 逻辑函数真值表为 2 位二进制变量 ,即输入向量$\boldsymbol{x} : (\boldsymbol{x_{1}}, \boldsymbol{x_{2}})$和对应的输出$\boldsymbol{y}$

$\boldsymbol{x_{1}}$ $\boldsymbol{x_{2}}$ $\boldsymbol{y}$
Zero Zero one
Zero one Zero
one Zero Zero
one one one

我们可以观察到,$XNOR(\boldsymbol{x_{1}}, \boldsymbol{x_{2}}) = OR(NOT(OR(\boldsymbol{x_{1}}, \boldsymbol{x_{2}})), AND(\boldsymbol{x_{1}}, \boldsymbol{x_{2}}))$ 设计感知器网络:

  1. Step1: Now for the corresponding weight vector $\boldsymbol{w} : (\boldsymbol{w_{1}}, \boldsymbol{w_{2}})$ of the input vector $\boldsymbol{x} : (\boldsymbol{x_{1}}, \boldsymbol{x_{2}})$ to the OR and AND node, the associated Perceptron Function can be defined as:

    [$\boldsymbol{\hat{y}_{1}} = \Theta\left(w_{1} x_{1}+w_{2} x_{2}+b_{OR}\right)$ ]

    [$\boldsymbol{\hat{y}_{2}} = \Theta\left(w_{1} x_{1}+w_{2} x_{2}+b_{AND}\right)$ ]

  2. Step2: The output ($\boldsymbol{\hat{y}}_{1}$) from the OR node will be inputed to the NOT node with weight $\boldsymbol{w_{NOT}}$ and the associated Perceptron Function can be defined as:

    [$\boldsymbol{\hat{y}_{3}} = \Theta\left(w_{NOT}  \boldsymbol{\hat{y}_{1}}+b_{NOT}\right)$]

  3. Step3: The output ($\boldsymbol{\hat{y}}_{2}$) from the AND node and the output ($\boldsymbol{\hat{y}}_{3}$) from NOT node as mentioned in Step2 will be inputed to the OR node with weight $(\boldsymbol{w_{OR1}}, \boldsymbol{w_{OR2}})$. Then the corresponding output $\boldsymbol{\hat{y}}$ is the final output of the XNOR logic function. The associated Perceptron Function can be defined as:

    [$\boldsymbol{\hat{y}} = \Theta\left(w_{OR1}  \boldsymbol{\hat{y}_{3}}+w_{OR2}  \boldsymbol{\hat{y}_{2}}+b_{OR}\right)$]

实施时,权重参数考虑为$\boldsymbol{w_{1}} = 1, \boldsymbol{w_{2}} = 1, \boldsymbol{w_{NOT}} = -1, \boldsymbol{w_{OR1}} = 1, \boldsymbol{w_{OR2}} = 1$,偏差参数为$\boldsymbol{b_{AND}} = -1.5, \boldsymbol{b_{OR}} = -0.5, \boldsymbol{b_{NOT}} = 0.5$

Python 实现:

# importing Python library
import numpy as np

# define Unit Step Function
def unitStep(v):
    if v >= 0:
        return 1
    else:
        return 0

# design Perceptron Model
def perceptronModel(x, w, b):
    v = np.dot(w, x) + b
    y = unitStep(v)
    return y

# NOT Logic Function
# wNOT = -1, bNOT = 0.5
def NOT_logicFunction(x):
    wNOT = -1
    bNOT = 0.5
    return perceptronModel(x, wNOT, bNOT)

# AND Logic Function
# w1 = 1, w2 = 1, bAND = -1.5
def AND_logicFunction(x):
    w = np.array([1, 1])
    bAND = -1.5
    return perceptronModel(x, w, bAND)

# OR Logic Function
# here w1 = wOR1 = 1, 
# w2 = wOR2 = 1, bOR = -0.5
def OR_logicFunction(x):
    w = np.array([1, 1])
    bOR = -0.5
    return perceptronModel(x, w, bOR)

# XNOR Logic Function
# with AND, OR and NOT  
# function calls in sequence
def XNOR_logicFunction(x):
    y1 = OR_logicFunction(x)
    y2 = AND_logicFunction(x)
    y3 = NOT_logicFunction(y1)
    final_x = np.array([y2, y3])
    finalOutput = OR_logicFunction(final_x)
    return finalOutput

# testing the Perceptron Model
test1 = np.array([0, 1])
test2 = np.array([1, 1])
test3 = np.array([0, 0])
test4 = np.array([1, 0])

print("XNOR({}, {}) = {}".format(0, 1, XNOR_logicFunction(test1)))
print("XNOR({}, {}) = {}".format(1, 1, XNOR_logicFunction(test2)))
print("XNOR({}, {}) = {}".format(0, 0, XNOR_logicFunction(test3)))
print("XNOR({}, {}) = {}".format(1, 0, XNOR_logicFunction(test4)))

Output:

XNOR(0, 1) = 0
XNOR(1, 1) = 1
XNOR(0, 0) = 1
XNOR(1, 0) = 0

这里,根据真值表,每个测试输入的模型预测输出($\boldsymbol{\hat{y}}$)与 XNOR 逻辑门常规输出($\boldsymbol{y}$)精确匹配。 由此验证了 XNOR 逻辑门的感知器算法是正确实现的。