Advertisement
Guest User

NEURAAAAAALLL

a guest
Mar 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.09 KB | None | 0 0
  1. import numpy as np
  2.  
  3. class NeuralNetwork:
  4.     def __init__(self, eta, numrounds, hidden_nodes):
  5.         self.numrounds = numrounds
  6.         '''
  7.        fit roda até que numrounds
  8.        seja alcançado
  9.        '''
  10.        
  11.         self.eta = eta #learning rate
  12.         self.hidden_nodes = hidden_nodes
  13.         '''
  14.        Quantidade de nós no hidden layer
  15.        '''
  16.        
  17.     def fit(self, X, y):
  18.         cols = len(X[0])
  19.         syn0 = 2*np.random.random((cols, self.hidden_nodes)) - 1  # 3x4 matrix of weights ((2 inputs + 1 bias) x 4 nodes in the hidden layer)
  20.         syn1 = 2*np.random.random((self.hidden_nodes,1)) - 1  # 4x1 matrix of weights. (4 nodes x 1 output) - no bias term in the hidden layer.
  21.        
  22.         for i in xrange(self.numrounds):
  23.             l0 = X
  24.             l1 = self.logistic(np.dot(l0, syn0))
  25.             l2 = self.logistic(np.dot(l1, syn1))
  26.  
  27.             l2_error = (y - l2)**2
  28.             l2_delta = -l2_error*self.logistic(np.dot(l1, syn1), True)
  29.             l1_error = l2_delta.dot(syn1.T)
  30.             l1_delta = l1_error*self.logistic(np.dot(l0, syn0), True)
  31.  
  32.             syn1 -= self.eta*l1.T.dot(l2_delta)
  33.             syn0 -= self.eta*l0.T.dot(l1_delta)
  34.        
  35.         print("Output after training")
  36.         print(l2)
  37.        
  38.         #fit não deve possuir retorno.
  39.         '''
  40.        o fit deve descobrir a dimensão do problema
  41.        e o número de classes.
  42.        '''
  43.                
  44.     def predict(self, X):
  45.         pass
  46.         #predict deve retornar as classes para X
  47.        
  48.     def logistic(self, x, deriv=False):  # Note: there is a typo on this line in the video
  49.         if(deriv==True):
  50.             return self.logistic(x)*(1-self.logistic(x))
  51.  
  52.         return 1/(1+np.exp(-x))  # Note: there is a typo on this line in the video
  53.  
  54. X = np.array([[0,0,1],  
  55.             [0,1,1],
  56.             [1,0,1],
  57.             [1,1,1]])
  58.  
  59.  
  60. # The output of the exclusive OR function follows.
  61.  
  62. #output data
  63. y = np.array([[0],
  64.              [1],
  65.              [1],
  66.              [0]])
  67.  
  68. nn = NeuralNetwork(0.01, 120000, 4)
  69. nn.fit(X, y)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement