Guest User

Untitled

a guest
Nov 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. # coding:utf-8
  2.  
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5.  
  6. def plot(x, d, x1, x2, e, w, b):
  7. print np.mean(np.abs(d - p_y_given_x(x, w, b)))
  8. plt.plot(e)
  9. plt.show()
  10. bx = np.arange(-6, 10, 0.1)
  11. by = -b/w[1] - w[0]/w[1]*bx
  12. plt.xlim([-5, 10])
  13. plt.ylim([-5, 9])
  14. plt.plot(bx, by)
  15. plt.scatter(x1[:, 0], x1[:, 1], c='g')
  16. plt.scatter(x2[:, 0], x2[:, 1], c='r')
  17. plt.show()
  18.  
  19. def p_y_given_x(x, w, b):
  20. def sigmoid(a):
  21. return 1.0 / (1.0 + np.exp(-a))
  22. return sigmoid(np.dot(x, w) + b)
  23.  
  24. def grad(x, d, w, b):
  25. error = d - p_y_given_x(x, w, b)
  26. w_grad = -np.mean(x.T * error, axis=1)
  27. b_grad = -np.mean(error)
  28. return w_grad, b_grad
  29.  
  30. def GD(x, d, w, b, e, eta=0.10, iteration=700):
  31. for _ in range(iteration):
  32. w_grad, b_grad = grad(x, d, w, b)
  33. w -= eta * w_grad
  34. b -= eta * b_grad
  35. e.append(np.mean(np.abs(d - p_y_given_x(x, w, b))))
  36. return e, w, b
  37.  
  38. def SGD(x, d, w, b, e, eta=0.10, iteration=5, minibatch_size=10):
  39. for _ in range(iteration):
  40. for index in range(0, x.shape[0], minibatch_size):
  41. _x = x[index:index + minibatch_size]
  42. _d = d[index:index + minibatch_size]
  43. w_grad, b_grad = grad(_x, _d, w, b)
  44. w -= eta * w_grad
  45. b -= eta * b_grad
  46. e.append(np.mean(np.abs(d - p_y_given_x(x, w, b))))
  47. return e, w, b
  48.  
  49. def SGD_momentum(x, d, w, b, e, eta=0.10, mu=0.65, iteration=50, minibatch_size=10):
  50. wlist, blist = [w], [b]
  51. def momentum(mu, list):
  52. return mu * (list[1] - list[0])
  53. for _ in range(iteration):
  54. for index in range(0, x.shape[0], minibatch_size):
  55. _x = x[index:index + minibatch_size]
  56. _d = d[index:index + minibatch_size]
  57. w_grad, b_grad = grad(_x, _d, w, b)
  58.  
  59. if len(wlist) > 1:
  60. w -= eta * w_grad + momentum(mu, wlist)
  61. b -= eta * b_grad + momentum(mu, blist)
  62. wlist.pop(0)
  63. blist.pop(0)
  64. else:
  65. w -= eta * w_grad
  66. b -= eta * b_grad
  67. wlist.append(w)
  68. blist.append(b)
  69. e.append(np.mean(np.abs(d - p_y_given_x(x, w, b))))
  70. return e, w, b
  71.  
  72. def SGD_adagrad(x, d, w, b, e, eta=0.10, iteration=50, minibatch_size=10):
  73. wgrad2sum = np.zeros(x.shape[1])
  74. bgrad2sum = 0
  75. for _ in range(iteration):
  76. for index in range(0, x.shape[0], minibatch_size):
  77. _x = x[index:index + minibatch_size]
  78. _d = d[index:index + minibatch_size]
  79. w_grad, b_grad = grad(_x, _d, w, b)
  80. wgrad2sum += np.power(w_grad, 2)
  81. bgrad2sum += np.power(b_grad, 2)
  82. w -= (eta/np.sqrt(wgrad2sum)) * w_grad
  83. b -= (eta/np.sqrt(bgrad2sum)) * b_grad
  84. e.append(np.mean(np.abs(d - p_y_given_x(x, w, b))))
  85. return e, w, b
  86.  
  87. def main():
  88. m, N = 2, 10000
  89. x1, x2 = np.random.randn(N, m), np.random.randn(N, m) + np.array([5, 5])
  90. x = np.vstack((x1, x2))
  91. d1, d2 = np.zeros(N), np.ones(N)
  92. d = np.hstack((d1, d2))
  93. dataset = np.column_stack((x, d))
  94. np.random.shuffle(dataset)
  95.  
  96. x, d = dataset[:, :2], dataset[:, 2]
  97. w, b = np.random.rand(m), np.random.random()
  98.  
  99. # e, w, b = GD(x, d, w, b, list())
  100. e, w, b = SGD(x, d, w, b, list())
  101. # e, w, b = SGD_momentum(x, d, w, b, list())
  102. # e, w, b = SGD_adagrad(x, d, w, b, list())
  103. plot(x, d, x1, x2, e, w, b)
  104.  
  105. if __name__ == "__main__":
  106. main()
Add Comment
Please, Sign In to add comment