Facebook
From Morose Tapir, 1 Week ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 54
  1. import numpy as np
  2. X = np.array(([2,9], [1,5], [3,6]), dtype = float)
  3. y = np.array(([.92], [.86], [.89]), dtype = float)
  4. X = X/np.amax(X, axis = 0)
  5.  
  6. def sigmoid(x):
  7.     return 1/(1+np.exp(-x))
  8.  
  9. def der_sigmoid(x):
  10.     return x*(1-x)
  11.  
  12. epoch = 5000
  13. lr = 0.01
  14. neurons_i = 2
  15. neurons_h = 3
  16. neurons_o = 1
  17.  
  18. weight_h = np.random.uniform(size=(neurons_i, neurons_h))
  19. bias_h = np.random.uniform(size=(1, neurons_h))
  20. weight_o = np.random.uniform(size=(neurons_h, neurons_o))
  21. bias_o = np.random.uniform(size=(1, neurons_o))
  22.  
  23. for i in range(epoch):
  24.     inp_h = np.dot(X, weight_h) + bias_h
  25.     out_h = sigmoid(inp_h)
  26.    
  27.     inp_o = np.dot(out_h, weight_o) + bias_o
  28.     out_o = sigmoid(inp_o)
  29.    
  30.     err_o = y - out_o
  31.     grad_o = der_sigmoid(out_o)
  32.     delta_o = err_o * grad_o
  33.    
  34.     err_h = delta_o.dot(weight_o.T)
  35.     grad_h = der_sigmoid(out_h)
  36.     delta_h = err_h * grad_h
  37.    
  38.     weight_o += out_h.T.dot(delta_o) * lr
  39.     weight_h += X.T.dot(delta_h) * lr
  40.    
  41. print('Input:\n', X)
  42. print('Actual:\n', y)
  43. print('Predicted:\n', out_o)

Replies to Untitled rss

Title Name Language When
Re: Untitled Soiled Meerkat text 1 Week ago.