Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # basic_neural_network.py
- import time
- def py_descent(x, d, mu, N_epochs):
- N = len(x)
- f = 2.0 / N
- # "Empty" predictions, errors, weights, gradients.
- y = [0] * N
- w = [0, 0]
- grad = [0, 0]
- for z in range(N_epochs):
- err = tuple(i - j for i, j in zip(d, y))
- #print err
- grad[0] = f * sum(err)
- grad[1] = f * sum(i * j for i, j in zip(err, x))
- w = [i + mu * j for i, j in zip(w, grad)]
- y = [w[0] + w[1] * i for i in x]
- return ['%f8'%i for i in w]
- 0
- x_list = [0.00009,-0.00052,0.00256,-0.00099,0.00123]
- d_list = [-0.00027,-0.00128,0.00027,0.00642,0.00499]
- # `mu` is a step size, or scaling factor.
- mu = 0.001
- N_epochs = 10000
- t0 = time.time()
- py_w = py_descent(x_list, d_list, mu, N_epochs)
- t1 = time.time()
- print(py_w)
- print('Solve time: {:.3f} seconds'.format(t1 - t0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement