📜  具有3位二进制输入的逻辑门的感知器算法

📅  最后修改于: 2021-04-16 06:19:51             🧑  作者: Mango

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

    \[\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}$
3位二进制变量(即输入向量)的AND,OR,NAND,NOR门的逻辑函数真值表$\boldsymbol{x} : (\boldsymbol{x_{1}}, \boldsymbol{x_{2}}, \boldsymbol{x_{3}})$和相应的输出$\boldsymbol{y_{AND}}, \boldsymbol{y_{OR}}, \boldsymbol{y_{NAND}}, \boldsymbol{y_{NOR}}$

$\boldsymbol{x_{1}}$ $\boldsymbol{x_{2}}$ $\boldsymbol{x_{3}}$ $\boldsymbol{y_{AND}}$ $\boldsymbol{y_{OR}}$ $\boldsymbol{y_{NAND}}$ $\boldsymbol{y_{NOR}}$
0 0 0 0 0 1 1
0 0 1 0 1 1 0
0 1 0 0 1 1 0
0 1 1 0 1 1 0
1 0 0 0 1 1 0
1 0 1 0 1 1 0
1 1 0 0 1 1 0
1 1 1 1 1 0 0

现在为相应的权重向量$\boldsymbol{w} : (\boldsymbol{w_{1}}, \boldsymbol{w_{2}}, \boldsymbol{w_{3}})$输入向量的$\boldsymbol{x} : (\boldsymbol{x_{1}}, \boldsymbol{x_{2}}, \boldsymbol{x_{3}})$ ,相关的感知器函数可以定义为:

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


为了实现,考虑的权重参数是$\boldsymbol{w_{1}}, \boldsymbol{w_{2}}, \boldsymbol{w_{3}}$偏差参数为$\boldsymbol{b}$对于每个逻辑门

$\boldsymbol{Parameters}$ $\boldsymbol{AND}$ $\boldsymbol{OR}$ $\boldsymbol{NAND}$ $\boldsymbol{NOR}$
$\boldsymbol{w_{1}}$ 1 1 -1 -1
$\boldsymbol{w_{2}}$ 1 1 -1 -1
$\boldsymbol{w_{2}}$ 1 1 -1 -1
$\boldsymbol{b}$ -2 -0.9 3 1

Python实现:

# importing python library
import numpy as np
  
# sigmoid activation function
def activationFunction(model, type ="sigmoid"):
   return {
       "sigmoid": 1 / (1 + np.exp(-model))
   }[type]
  
# designing perceptron model
def perceptronModel(weights, inputs, bias):
   model = np.add(np.dot(inputs, weights), bias)
   logic = activationFunction(model, type ="sigmoid")
   return np.round(logic)
  
# computation model
def compute(data, logicGate, weights, bias):
   weights = np.array(weights)
   output = np.array([ perceptronModel(weights,  
            datum, bias) for datum in data ])
   return output
  
# Print Output
def printOutput(dataset, name, data):
   print("Logic Function: {}".format(name.upper()))
   print("X1\tX2\tX3\tY")
   toPrint = ["{1}\t{2}\t{3}\t{0}".format(output, *datas)  
              for datas, output in zip(dataset, data)]
   for i in toPrint:
       print(i)
  
# main function
def main():
   # 3 bit binary data
   dataset = np.array([
     [0, 0, 0],
     [0, 0, 1],
     [0, 1, 0],
     [0, 1, 1],
     [1, 0, 0],
     [1, 0, 1],
     [1, 1, 0],
     [1, 1, 1]
   ])
  
   # Parameters of every Logic Gates
   # weight parameters: w1, w2, w3
   # bias parameter: b
   logicGate = {
       "and": compute(dataset, "and", [1, 1, 1], -2),
       "or": compute(dataset, "or", [1, 1, 1], -0.9),
       "nand": compute(dataset, "nand", [-1, -1, -1], 3),
       "nor": compute(dataset, "nor", [-1, -1, -1], 1)
   }
   for gate in logicGate:
       printOutput(dataset, gate, logicGate[gate])
  
if __name__ == '__main__':
   main()
输出:
Logic Function: AND
X1    X2    X3    Y
0    0    0    0.0
0    0    1    0.0
0    1    0    0.0
0    1    1    0.0
1    0    0    0.0
1    0    1    0.0
1    1    0    0.0
1    1    1    1.0
Logic Function: OR
X1    X2    X3    Y
0    0    0    0.0
0    0    1    1.0
0    1    0    1.0
0    1    1    1.0
1    0    0    1.0
1    0    1    1.0
1    1    0    1.0
1    1    1    1.0
Logic Function: NAND
X1    X2    X3    Y
0    0    0    1.0
0    0    1    1.0
0    1    0    1.0
0    1    1    1.0
1    0    0    1.0
1    0    1    1.0
1    1    0    1.0
1    1    1    0.0
Logic Function: NOR
X1    X2    X3    Y
0    0    0    1.0
0    0    1    0.0
0    1    0    0.0
0    1    1    0.0
1    0    0    0.0
1    0    1    0.0
1    1    0    0.0
1    1    1    0.0

在此,模型的预测输出( $\boldsymbol{\hat{y}}$ ),因为每个测试输入均与AND,OR,NAND,NOR逻辑门的常规输出( $\boldsymbol{y}$ )根据3位二进制输入的真值表。
因此,可以证明所有这些逻辑门的感知器算法均已正确实现。