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

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

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