Advertisement
here2share

# basic_neural_network.py

Feb 5th, 2019
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. # basic_neural_network.py
  2.  
  3. import time
  4.  
  5. def py_descent(x, d, mu, N_epochs):
  6.     N = len(x)
  7.     f = 2.0 / N
  8.  
  9.     # "Empty" predictions, errors, weights, gradients.
  10.     y = [0] * N
  11.     w = [0, 0]
  12.     grad = [0, 0]
  13.  
  14.     for z in range(N_epochs):
  15.         err = tuple(i - j for i, j in zip(d, y))
  16.         #print err
  17.         grad[0] = f * sum(err)
  18.         grad[1] = f * sum(i * j for i, j in zip(err, x))
  19.         w = [i + mu * j for i, j in zip(w, grad)]
  20.         y = [w[0] + w[1] * i for i in x]
  21.     return ['%f8'%i for i in w]
  22. 0
  23.  
  24. x_list = [0.00009,-0.00052,0.00256,-0.00099,0.00123]
  25. d_list = [-0.00027,-0.00128,0.00027,0.00642,0.00499]
  26.  
  27. # `mu` is a step size, or scaling factor.
  28. mu = 0.001
  29. N_epochs = 10000
  30.  
  31. t0 = time.time()
  32. py_w = py_descent(x_list, d_list, mu, N_epochs)
  33. t1 = time.time()
  34.  
  35. print(py_w)
  36.  
  37. print('Solve time: {:.3f} seconds'.format(t1 - t0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement