Section outline

  • Python implementation

    🎙️Marcelo Gutiérrez


    Description

    This section provides a brief introduction to the perceptron algorithm and the AND logic gate dataset that we will be using in this tutorial.


    Network Initialization

    import pandas as pd
    import numpy as np
    
    data = [[-1,-1, 1,-1],
            [-1, 1, 1,-1],
            [ 1,-1, 1,-1],
            [ 1, 1, 1, 1]]
    
    pd.DataFrame(data, index=list(range(len(data))), columns=['X1', 'X2','bias','Y'])

    Output

    ### See secction "2.2 Perceptron evolution"###
    prediction = sign(np.dot(weight, training))
    

    Activation function

    ### See secction "2.2 Perceptron evolution"###
    def sign(x):
        if x > 0:
            return 1.0
        elif x < 0:
            return -1.0
        else:
            return 1.0

    Error of the neural network model

    ### Ver sección "2.4 Loss function"###
    error = desired - prediction
    sse += error**2

    Weight update

    ### Ver sección "2.3 Teach To Learn"###
    weight = weight + error * training
    

    Algorithm

    
    def sign(x):
        if x > 0:
            return 1.0
        elif x < 0:
            return -1.0
        else:
            return 1.0
        
    import pandas as pd
    import numpy as np
    
    data = [[-1,-1, 1,-1],
            [-1, 1, 1,-1],
            [ 1,-1, 1,-1],
            [ 1, 1, 1, 1]]
    
    pd.DataFrame(data, index=list(range(len(data))), columns=['X1', 'X2','bias','Y'])
    train_data = pd.DataFrame(data, index=list(range(len(data))), columns=['X1', 'X2','bias','Y'])
    train_Y = train_data.Y
    train_data = train_data[['X1', 'X2','bias']]
    train_data.head()
    
    weight = np.zeros(train_data.shape[1])
    
    for epoch in range(100):
        sse = 0.0
        for sample in range(train_data.shape[0]):
            training = train_data.loc[sample].values
            desired = train_Y.loc[sample]
            prediction = sign(np.dot(weight, training))
            error = desired - prediction
            sse += error**2
            weight = weight + error * training
        print(f'SSE: {sse}')
        if sse == 0:
            break
    print(f'Weights: {weight}, Epoch: {epoch+1}')