Advertisement
Guest User

nn

a guest
Apr 4th, 2020
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.92 KB | None | 0 0
  1. import numpy as np
  2. import random
  3.  
  4. def MinMax(x,Min,Max,sMin,sMax):
  5.     return ((x - Min)/(Max - Min) * (sMax - sMin)) + sMin
  6.  
  7. class NeuralNetwork():
  8.  
  9.  
  10.     def __init__(self,inputs,hidden,outputs,rate=0.1,hiddenLayer=1,func = "sigmoid"):
  11.         self.weightsInputToHidden   = np.random.rand(inputs,hidden)
  12.         self.weightsHiddenTooutput  = np.random.rand(hidden,outputs)
  13.         self.hiddenLArr = []
  14.         self.hiddenBArr = []
  15.         self.hiddenLayer = hiddenLayer
  16.         for i in range(hiddenLayer):
  17.              if i == 0:
  18.                 tmp = np.random.rand(inputs,hidden)
  19.                 self.hiddenLArr.append(tmp)
  20.                 tmp = np.random.rand(1,hidden)
  21.                 self.hiddenBArr.append(tmp)
  22.              else:
  23.                 tmp =np.random.rand(hidden,hidden)
  24.                 self.hiddenLArr.append(tmp)
  25.                 tmp = np.random.rand(1,hidden)
  26.                 self.hiddenBArr.append(tmp)  
  27.         if func == "relu":
  28.              self.ActiveFunction = self.ReLU
  29.         elif func == "tanh":
  30.              self.ActiveFunction = self.TanH
  31.         else:
  32.             self.ActiveFunction = self.Sigmoid
  33.  
  34.         self.biosH    = np.random.rand(1,hidden)  
  35.         self.biosH.shape = (1,hidden)
  36.         self.biosO    = np.random.rand(1,outputs)  * np.sqrt(2/(inputs + outputs))
  37.         self.biosO.shape = (1,outputs)
  38.         self.rate =rate
  39.        
  40.     def SoftMax(self,output):
  41.         l = []
  42.         sumOfExp = 0
  43.         for i in output:
  44.             sumOfExp +=np.exp(i)
  45.         for i in output:
  46.             l.append(np.exp(i)/sumOfExp)
  47.         return l
  48.  
  49.     def SetLearningRate(self,x):
  50.         self.rate = x
  51.  
  52.     def TanH(self,x):
  53.         try:
  54.             a = 2
  55.             b = 1 + np.exp(-2 * x)
  56.             return (a / b)-1
  57.         except Exception as identifier:
  58.             return 0
  59.  
  60.     def ReLU(self,x):
  61.         return x * (x > 0)
  62.  
  63.     def Sigmoid(self,x):
  64.         return 1.0 /(1+np.exp(-x))
  65.  
  66.     def DSigmoid(self,x):
  67.         return x *(1-x)
  68.  
  69.     def Train(self,inputs,targets,iter):
  70.         for i in range(iter):
  71.             index = random.randint(0,len(inputs)-1)
  72.             input = np.array(inputs[index])
  73.             input.shape = (1,len(input))
  74.             hidden = np.dot(input,self.weightsInputToHidden)
  75.             hidden = np.add(hidden,self.biosH)
  76.             hidden = self.Sigmoid(hidden)
  77.  
  78.             output = np.dot(hidden,self.weightsHiddenTooutput)
  79.             output = np.add(output,self.biosO)
  80.             output = self.Sigmoid(output)
  81.  
  82.             error = targets[index] - output
  83.             gradients = self.DSigmoid(output)
  84.             cost =gradients * error  * self.rate
  85.             dCost = np.dot(np.transpose(hidden),cost)
  86.             self.weightsHiddenTooutput+= dCost
  87.             self.biosO+= cost
  88.             #
  89.             error = np.dot(error,  np.transpose(self.weightsHiddenTooutput))
  90.             gradients = self.DSigmoid(hidden)
  91.             cost = gradients * error  * self.rate
  92.             dCost = np.dot(np.transpose(input),cost)
  93.             self.weightsInputToHidden+= dCost
  94.             self.biosH+= cost
  95.  
  96.     def Predict(self,inputs,func = "sigmoid"):    
  97.         input = np.array(inputs)
  98.         input.shape = (1,len(input))
  99.         if func == "sigmoid":
  100.             hidden = np.dot(input,self.weightsInputToHidden)
  101.             hidden = np.add(hidden,self.biosH)
  102.             hidden = self.Sigmoid(hidden)
  103.  
  104.             output = np.dot(hidden,self.weightsHiddenTooutput)
  105.             output = np.add(output,self.biosO)
  106.             output = self.Sigmoid(output)
  107.         elif func == "relu":
  108.             hidden = np.dot(input,self.weightsInputToHidden)
  109.             hidden = np.add(hidden,self.biosH)
  110.             hidden = self.ReLU(hidden)
  111.  
  112.             output = np.dot(hidden,self.weightsHiddenTooutput)
  113.             output = np.add(output,self.biosO)
  114.             output = self.ReLU(output)
  115.         elif func == "tanH":
  116.             hidden = np.dot(input,self.weightsInputToHidden)
  117.             hidden = np.add(hidden,self.biosH)
  118.             hidden = self.TanH(hidden)
  119.  
  120.             output = np.dot(hidden,self.weightsHiddenTooutput)
  121.             output = np.add(output,self.biosO)
  122.             output = self.TanH(output)
  123.                        
  124.         return self.SoftMax(output.tolist()[0])
  125.  
  126.     def Predict2(self,inputs,softMax=True):    
  127.         input = np.array(inputs)
  128.         input.shape = (1,len(input))
  129.         hidden = np.dot(input,self.hiddenLArr[0])
  130.         hidden = np.add(hidden,self.hiddenBArr[0])
  131.         hidden = self.ActiveFunction(hidden)
  132.         for i in range(self.hiddenLayer-1):
  133.              hidden = np.dot(hidden,self.hiddenLArr[i+1])
  134.              hidden = np.add(hidden,self.hiddenBArr[i+1])
  135.              hidden = self.ActiveFunction(hidden)            
  136.         output = np.dot(hidden,self.weightsHiddenTooutput)
  137.         output = np.add(output,self.biosO)
  138.         output = self.ActiveFunction(output)  
  139.         if softMax:
  140.             return self.SoftMax(output.tolist()[0])
  141.         else:
  142.             return output.tolist()[0]
  143.  
  144.     def mutation(self,n,mutaion = 0.1):
  145.         for i in range(n.hiddenLayer):
  146.             for j in range(len(n.hiddenLArr[i])):
  147.                 for k in range(len(n.hiddenLArr[i][j])):      
  148.                     chance = np.random.uniform(0,1)
  149.                     if chance < mutaion:
  150.                         r = np.random.uniform(-0.5,0.5)
  151.                         self.hiddenLArr[i][j][k] =  n.hiddenLArr[i][j][k] + r
  152.                     else:
  153.                         self.hiddenLArr[i][j][k] = n.hiddenLArr[i][j][k]
  154.  
  155.         for i in range(n.hiddenLayer):
  156.             for j in range(len(n.hiddenBArr[i])):
  157.                 for k in range(len(n.hiddenBArr[i][j])):      
  158.                     chance = np.random.uniform(0,1)
  159.                     if chance < mutaion:
  160.                         r = np.random.uniform(-0.5,0.5)
  161.                         self.hiddenBArr[i][j][k] = n.hiddenBArr[i][j][k] + r
  162.                     else:
  163.                         self.hiddenBArr[i][j][k] = n.hiddenBArr[i][j][k]
  164.                        
  165.         for i in range(len(n.weightsHiddenTooutput)):
  166.             for j in range(len(n.weightsHiddenTooutput[i])):
  167.                 chance = np.random.uniform(0,1)
  168.                 if chance < mutaion:
  169.                     r = np.random.uniform(-0.5,0.5)
  170.                     self.weightsHiddenTooutput[i][j] = n.weightsHiddenTooutput[i][j] + r
  171.                 else:
  172.                      self.weightsHiddenTooutput[i][j] = n.weightsHiddenTooutput[i][j]
  173.  
  174.         for i in range(len(n.biosO)):
  175.             for j in range(len(n.biosO[i])):
  176.                 chance = np.random.uniform(0,1)
  177.                 if chance < mutaion:
  178.                     r = np.random.uniform(-0.5,0.5)
  179.                     self.biosO[i][j] = n.biosO[i][j] + r            
  180.                 else:
  181.                     self.biosO[i][j] = n.biosO[i][j]
  182.         self.rate =n.rate
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement