📌  相关文章
📜  2位二进制输入异或逻辑门感知器算法的实现

📅  最后修改于: 2022-05-13 01:55:45.407000             🧑  作者: Mango

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


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

    \[ \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{\hat{y}}$对于相应的输入向量$\boldsymbol{x}$ .

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

$\boldsymbol{x_{1}}$$\boldsymbol{x_{2}}$$\boldsymbol{y}$
000
011
101
110

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

  1. Step1:现在为对应的权重向量$\boldsymbol{w} : (\boldsymbol{w_{1}}, \boldsymbol{w_{2}})$输入向量的$\boldsymbol{x} : (\boldsymbol{x_{1}}, \boldsymbol{x_{2}})$到 AND 和 OR 节点,相关的感知函数可以定义为:

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

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

  2. 第二步:输出($\boldsymbol{\hat{y}}_{1}$)来自AND节点的将被输入到具有权重的NOT节点$\boldsymbol{w_{NOT}}$并且相关的感知函数可以定义为:

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

  3. 第三步:输出($\boldsymbol{\hat{y}}_{2}$)从 OR 节点和输出($\boldsymbol{\hat{y}}_{3}$)步骤2中提到的来自NOT节点将被输入到带有权重的AND节点$(\boldsymbol{w_{AND1}}, \boldsymbol{w_{AND2}})$ .然后对应的输出$\boldsymbol{\hat{y}}$是 XOR 逻辑函数的最终输出。相关的感知函数可以定义为:

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


为了实现,权重参数被认为是$\boldsymbol{w_{1}} = 1, \boldsymbol{w_{2}} = 1, \boldsymbol{w_{NOT}} = -1, \boldsymbol{w_{AND1}} = 1, \boldsymbol{w_{AND2}} = 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
# here w1 = wAND1 = 1, 
# w2 = wAND2 = 1, bAND = -1.5
def AND_logicFunction(x):
    w = np.array([1, 1])
    bAND = -1.5
    return perceptronModel(x, w, bAND)
  
# OR Logic Function
# w1 = 1, w2 = 1, bOR = -0.5
def OR_logicFunction(x):
    w = np.array([1, 1])
    bOR = -0.5
    return perceptronModel(x, w, bOR)
  
# XOR Logic Function
# with AND, OR and NOT  
# function calls in sequence
def XOR_logicFunction(x):
    y1 = AND_logicFunction(x)
    y2 = OR_logicFunction(x)
    y3 = NOT_logicFunction(y1)
    final_x = np.array([y2, y3])
    finalOutput = AND_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("XOR({}, {}) = {}".format(0, 1, XOR_logicFunction(test1)))
print("XOR({}, {}) = {}".format(1, 1, XOR_logicFunction(test2)))
print("XOR({}, {}) = {}".format(0, 0, XOR_logicFunction(test3)))
print("XOR({}, {}) = {}".format(1, 0, XOR_logicFunction(test4)))
输出:
XOR(0, 1) = 1
XOR(1, 1) = 0
XOR(0, 0) = 0
XOR(1, 0) = 1

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