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_1.py 2.6 kB

6 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #
  2. # BP demo code
  3. #
  4. # reference:
  5. # https://www.2cto.com/kf/201612/543750.html
  6. #
  7. import numpy as np
  8. from sklearn import datasets, linear_model
  9. import matplotlib.pyplot as plt
  10. class Config:
  11. nn_input_dim = 2
  12. nn_output_dim = 2
  13. epsilon = 0.01
  14. reg_lambda = 0.01
  15. def generate_data():
  16. np.random.seed(0)
  17. X, y = datasets.make_moons(200, noise=0.20)
  18. return X, y
  19. def visualize(X, y, model):
  20. plot_decision_boundary(lambda x:predict(model,x), X, y)
  21. plt.title("Logistic Regression")
  22. def plot_decision_boundary(pred_func, X, y):
  23. x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
  24. y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
  25. h = 0.01
  26. xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
  27. Z = pred_func(np.c_[xx.ravel(), yy.ravel()])
  28. Z = Z.reshape(xx.shape)
  29. plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
  30. plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Spectral)
  31. plt.show()
  32. def predict(model, x):
  33. W1, b1, W2, b2 = model['W1'], model['b1'], model['W2'], model['b2']
  34. z1 = x.dot(W1) + b1
  35. a1 = np.tanh(z1)
  36. z2 = a1.dot(W2) + b2
  37. exp_scores = np.exp(z2)
  38. probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True)
  39. return np.argmax(probs, axis=1)
  40. def build_model(X, y, nn_hdim, num_passes=20000, print_loss=False):
  41. num_examples = len(X)
  42. np.random.seed(0)
  43. W1 = np.random.randn(Config.nn_input_dim, nn_hdim) / np.sqrt(Config.nn_input_dim)
  44. b1 = np.zeros((1, nn_hdim))
  45. W2 = np.random.randn(nn_hdim, Config.nn_output_dim) / np.sqrt(nn_hdim)
  46. b2 = np.zeros((1, Config.nn_output_dim))
  47. model = {}
  48. for i in range(0, num_passes):
  49. z1 = X.dot(W1) + b1
  50. a1 = np.tanh(z1)
  51. z2 = a1.dot(W2) + b2
  52. exp_scores = np.exp(z2)
  53. probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True)
  54. delta3 = probs
  55. delta3[range(num_examples), y] -= 1
  56. dW2 = (a1.T).dot(delta3)
  57. db2 = np.sum(delta3, axis=0, keepdims=True)
  58. delta2 = delta3.dot(W2.T) * (1 - np.power(a1, 2))
  59. dW1 = np.dot(X.T, delta2)
  60. db1 = np.sum(delta2, axis=0)
  61. dW2 += Config.reg_lambda * W2
  62. dW1 += Config.reg_lambda * W1
  63. W1 += -Config.epsilon * dW1
  64. b1 += -Config.epsilon * db1
  65. W2 += -Config.epsilon * dW2
  66. b2 += -Config.epsilon * db2
  67. model = {'W1': W1, 'b1': b1, 'W2': W2, 'b2': b2}
  68. return model
  69. def main():
  70. X, y = generate_data()
  71. model = build_model(X, y, 3)
  72. visualize(X, y, model)
  73. if __name__ == "__main__":
  74. main()

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