当前位置:网站首页>Self learning neural network sequence -- 2 perceptron

Self learning neural network sequence -- 2 perceptron

2022-06-26 09:09:00 ML_ python_ get√

2.1 What is a perceptron

  • Accept multiple input signals , Output a signal
  • Neuron : Weighting the input signal , If the weighted number meets a certain condition, enter 1, Otherwise output 0
  • The weight represents the importance of the signal

2.2 And or not

  • And gate : Only two inputs are 1 When the output 1, Other situation output 0
  • NAND gate : Invert the and gate output , Only two inputs are 1 When the output 0, Otherwise output 1
  • Or gate : As long as one input is 1, Then output 1, Only all inputs are 0, Just output 0
  • As long as the parameters of the sensor are adjusted, the switch between different doors can be realized
  • Parameter adjustment is left to the computer , Let the computer decide what kind of door
  • Exclusive OR gate : Only one input is 1 when , Will enter 1, The two inputs are 1 when , Output 0
# python  Implement and gate 


def AND(x1,x2):
    ''' Implementation of and gate '''
    w1,w2,theta = 0.5,0.5,0.7
    tmp = x1*w1+x2*w2
    if tmp<=theta:
        return 0
    elif tmp>theta:
        return 1


#  Test functions 
AND(1,1)
AND(0,0)
AND(1,0)
AND(0,1)
#  Use offset   and numpy Realization 

def AND(x1,x2):
    import numpy as np

    x = np.array([x1,x2])
    w = np.array([0.5,0.5])
    b = -0.7  #  threshold , Adjust how easily neurons are activated 
    tmp = np.sum(w*x) +b
    if tmp<=0:
        return 0
    elif tmp>0:
        return 1

#  test 
AND(1,1)
AND(1,0)
AND(0,1)
AND(0,0)
#  NAND gate 
#  The output is just the opposite , The weights and offsets are opposite to each other 

def NAND(x1,x2):
    ''' NAND gate '''
    import numpy as np

    x = np.array([x1,x2])
    w = np.array([-0.5,-0.5])
    b = 0.7
    tmp = np.sum(w*x)+b
    if tmp <=0:
        return 0
    elif tmp>0:
        return 1

#  test 
NAND(1,1)
NAND(1,0)
NAND(0,1)
NAND(0,0)
#  Or gate : The absolute value of the offset is less than 0.5 that will do 
def OR(x1,x2):
    import numpy as np
    
    x = np.array([x1,x2])
    w = np.array([0.5,0.5])   #  As long as not both inputs go 0,  It outputs 1
    b = -0.2 
    tmp = np.sum(w*x)+b
    if tmp<=0:
        return 0
    else:
        return 1


OR(1,1)
OR(0,1)
OR(1,0)
OR(1,1)
#  Exclusive OR gate : Cannot be separated by a straight line 
#  Introduce nonlinearity : Overlay layer perceptron 
#  Through the NAND gate, we get S1  Or door access S2  You can see the result of the XOR gate that can be reached through the and gate 
#  Multilayer perceptron , There are multiple linear classifiers , The nonlinear fitting can be realized through and gate 
def XOR(x1,x2):
    s1 = NAND(x1,x2)
    s2 = OR(x1,x2)
    y = AND(s1,s2)
    return y

#  test 
XOR(1,1)
XOR(0,0)
XOR(1,0)
XOR(0,1)
原网站

版权声明
本文为[ML_ python_ get√]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170553131692.html