Advertisement
excellent940707

MLP

May 15th, 2024
539
0
3 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.04 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri Jan 12 22:27:44 2024
  4.  
  5. @author: yiwen
  6. """
  7.  
  8. import numpy as np
  9. import matplotlib.pyplot as plt
  10.  
  11. def tanh(x):
  12.     return (1.0-np.exp(-2*x))/(1.0+np.exp(-2*x))
  13.  
  14. def tanh_derivative(x):
  15.     return (1+tanh(x)) * (1-tanh(x))
  16.  
  17. class Neural_Network(object):
  18.     def __init__(self, net_arch):
  19.         self.activity = tanh;
  20.         self.activity_derivative = tanh_derivative;
  21.         self.layers = len(net_arch)
  22.         self.steps_per_epoch = 1000
  23.         self.arch = net_arch
  24.         self.weights = []
  25.         for layer in range(self.layers-1):
  26.             w = 2*np.random.rand(net_arch[layer]+1,
  27.                                  net_arch[layer+1])-1
  28.             self.weights.append(w)
  29.        
  30.     def predict(self, x):
  31.         val = np.concatenate((np.ones(1), np.array(x)))
  32.         for i in range(0, len(self.weights)):
  33.             val = self.activity(np.dot(val, self.weights[i]))
  34.             val = np.concatenate((np.ones(1), np.array(val)))
  35.        
  36.         return val[1]
  37.    
  38.     def fit(self, data, labels, learning_rate=0.1, epochs=100):
  39.         ones = np.ones((1, data.shape[0]))
  40.         z = np.concatenate((ones.T, data), axis=1)
  41.         '''
  42.        a = [[1,2],
  43.             [3,4]]
  44.        b = [[5,6]]
  45.        c = np.concatenate((a,b), axis = 1)
  46.        c = [[1,2,5],
  47.             [3,4,6]]
  48.        '''
  49.         training = epochs*self.steps_per_epoch
  50.         for k in range(training):
  51.            
  52.             if k%self.steps_per_epoch==0:
  53.                 print(f'epochs: {k/self.steps_per_epoch}')
  54.                 for s in data:
  55.                     print(s, self.predict(s))
  56.            
  57.             sample = np.random.randint(data.shape[0])
  58.             y =[z[sample]]
  59.             for i in range(len(self.weights)-1):
  60.                 activation = np.dot(y[i], self.weights[i])
  61.                 activity = self.activity(activation)
  62.                 activity = np.concatenate((np.ones(1),np.array(activity)))
  63.                 y.append(activity)
  64.                
  65.             activation = np.dot(y[-1], self.weights[-1])
  66.             activity = self.activity(activation)
  67.             y.append(activity)
  68.            
  69.             error = labels[sample]- y[-1]
  70.             delta_vec = [error*self.activity_derivative(
  71.                 y[-1])]
  72.             for i in range(self.layers-2, 0 ,-1):
  73.                 error = delta_vec[-1].dot(self.weights[i][1:].T)
  74.                 error = error*self.activity_derivative(y[i][1:])
  75.                 delta_vec.append(error)
  76.             delta_vec.reverse()
  77.             for i in range(len(self.weights)):
  78.                 layer = y[i].reshape(1, self.arch[i]+1)
  79.                 delta = delta_vec[i].reshape(1,self.arch[i+1])
  80.                 self.weights[i]+= learning_rate*layer.T.dot(delta)
  81.                
  82.  
  83. if __name__=='__main__':
  84.     np.random.seed(0)
  85.     model = Neural_Network([2,2,1])
  86.     x = np.array([[0,0],
  87.                   [0,1],
  88.                   [1,0],
  89.                   [1,1]])
  90.     y = np.array([0,1,1,0])
  91.     model.fit(x,y,epochs=1)
  92.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement