SoFunction
Updated on 2024-11-16

Python implementation of perceptron model, two-layer neural network

In this article, we have shared an example of Python implementation of perceptron model, two-layer neural network for your reference, the details are as follows

python 3.4 because it uses numpy.

Here we first implement a perceptron model to realize the following correspondence

[[0,0,1], ——- 0
[0,1,1], ——- 1
[1,0,1], ——- 0
[1,1,1]] ——- 1

As you can see from the data above: the inputs are three channels and the outputs are single channels.

For the activation function here we use the sigmoid function f(x)=1/(1+exp(-x))

The derivatives are derived as shown below.

L0=W*X;
z=f(L0);
error=y-z;
delta =error * f'(L0) * X;
W=W+delta;

The python code is as follows:

import numpy as np

#sigmoid function

def nonlin(x, deriv = False):
  if(deriv==True):
    return x*(1-x)
  return 1/(1+(-x))


# input dataset

X=([[0,0,1],
      [0,1,1],
      [1,0,1],
      [1,1,1]])

# output dataset

y=([[0,1,0,1]]).T

#seed( ) is used to specify the integer value at the beginning of the algorithm used for random number generation.
# If the same seed( ) value is used, the same number of followers is generated each time.
# If this value is not set, the system chooses this value itself based on the time of day.
# At this point the random number generated each time varies due to time differences.
(1)  

# init weight value with mean 0

syn0 = 2*((3,1))-1   

for iter in range(1000):
  # forward propagation
  L0=X
  L1=nonlin((L0,syn0))

  # error
  L1_error=y-L1

  L1_delta = L1_error*nonlin(L1,True)

  # updata weight
  syn0+=(,L1_delta)

print("Output After Training:")
print(L1)

From the output it can be seen that the correspondence is basically realized.

The following two-layer network is used again to realize the above task, here a hidden layer is added which contains 4 neurons.

import numpy as np

def nonlin(x, deriv = False):
  if(deriv == True):
    return x*(1-x)
  else:
    return 1/(1+(-x))

#input dataset
X = ([[0,0,1],
       [0,1,1],
       [1,0,1],
       [1,1,1]])

#output dataset
y = ([[0,1,1,0]]).T

#the first-hidden layer weight value
syn0 = 2*((3,4)) - 1 

#the hidden-output layer weight value
syn1 = 2*((4,1)) - 1 

for j in range(60000):
  l0 = X      
  #the first layer,and the input layer 
  l1 = nonlin((l0,syn0)) 
  #the second layer,and the hidden layer
  l2 = nonlin((l1,syn1)) 
  #the third layer,and the output layer


  l2_error = y-l2    
  #the hidden-output layer error

  if(j%10000) == 0:
    print "Error:"+str((l2_error))

  l2_delta = l2_error*nonlin(l2,deriv = True)

  l1_error = l2_delta.dot()   
  #the first-hidden layer error

  l1_delta = l1_error*nonlin(l1,deriv = True)

  syn1 += (l2_delta)
  syn0 += (l1_delta)

print "outout after Training:"
print l2

This is the whole content of this article.