You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

nn_4.py 3.2 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #% matplotlib inline
  2. import numpy as np
  3. from sklearn import datasets, linear_model
  4. from sklearn.metrics import accuracy_score
  5. import matplotlib.pyplot as plt
  6. # define sigmod
  7. def sigmod(X):
  8. return 1.0/(1+np.exp(-X))
  9. # generate the NN model
  10. class NN_Model:
  11. def __init__(self, nodes=None):
  12. self.epsilon = 0.01 # learning rate
  13. self.n_epoch = 1000 # iterative number
  14. if not nodes:
  15. self.nodes = [2, 4, 2] # default nodes size (from input -> output)
  16. else:
  17. self.nodes = nodes
  18. def init_weight(self):
  19. W = []
  20. B = []
  21. n_layer = len(self.nodes)
  22. for i in range(n_layer-1):
  23. w = np.random.randn(self.nodes[i], self.nodes[i+1]) / np.sqrt(self.nodes[i])
  24. b = np.random.randn(1, self.nodes[i+1])
  25. W.append(w)
  26. B.append(b)
  27. self.W = W
  28. self.B = B
  29. def forward(self, X):
  30. Z = []
  31. x0 = X
  32. for i in range(len(self.nodes)-1):
  33. z = sigmod(np.dot(x0, self.W[i]) + self.B[i])
  34. x0 = z
  35. Z.append(z)
  36. self.Z = Z
  37. # back-propagation
  38. def backpropagation(self, X, y, n_epoch=None, epsilon=None):
  39. if not n_epoch: n_epoch = self.n_epoch
  40. if not epsilon: epsilon = self.epsilon
  41. self.X = X
  42. self.Y = y
  43. for i in range(n_epoch):
  44. # forward to calculate each node's output
  45. self.forward(X)
  46. self.evaluate()
  47. # calc weights update
  48. W = self.W
  49. B = self.B
  50. Z = self.Z
  51. D = []
  52. d0 = y
  53. n_layer = len(self.nodes)
  54. for j in range(n_layer-1, 0, -1):
  55. jj = j - 1
  56. z = self.Z[jj]
  57. if j == n_layer - 1:
  58. d = z*(1-z)*(d0 - z)
  59. else:
  60. d = z*(1-z)*np.dot(d0, W[j].T)
  61. d0 = d
  62. D.insert(0, d)
  63. # update weights
  64. for j in range(n_layer-1, 0, -1):
  65. jj = j - 1
  66. if jj != 0:
  67. W[jj] += epsilon * np.dot(Z[jj-1].T, D[jj])
  68. else:
  69. W[jj] += epsilon * np.dot(X.T, D[jj])
  70. B[jj] += epsilon * np.sum(D[jj], axis=0)
  71. def evaluate(self):
  72. z = self.Z[-1]
  73. # print loss, accuracy
  74. L = np.sum((z - self.Y)**2)
  75. y_pred = np.argmax(z, axis=1)
  76. y_true = np.argmax(self.Y, axis=1)
  77. acc = accuracy_score(y_true, y_pred)
  78. print("L = %f, acc = %f" % (L, acc))
  79. # generate sample data
  80. np.random.seed(0)
  81. X, y = datasets.make_moons(200, noise=0.20)
  82. # generate nn output target
  83. t = np.zeros((X.shape[0], 2))
  84. t[np.where(y==0), 0] = 1
  85. t[np.where(y==1), 1] = 1
  86. # plot data
  87. #plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Spectral)
  88. #plt.show()
  89. nn = NN_Model([2, 3, 2])
  90. nn.init_weight()
  91. nn.backpropagation(X, t)

机器学习越来越多应用到飞行器、机器人等领域,其目的是利用计算机实现类似人类的智能,从而实现装备的智能化与无人化。本课程旨在引导学生掌握机器学习的基本知识、典型方法与技术,通过具体的应用案例激发学生对该学科的兴趣,鼓励学生能够从人工智能的角度来分析、解决飞行器、机器人所面临的问题和挑战。本课程主要内容包括Python编程基础,机器学习模型,无监督学习、监督学习、深度学习基础知识与实现,并学习如何利用机器学习解决实际问题,从而全面提升自我的《综合能力》。