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_from_scratch.py 5.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # ---
  2. # jupyter:
  3. # jupytext_format_version: '1.2'
  4. # kernelspec:
  5. # display_name: Python 3
  6. # language: python
  7. # name: python3
  8. # ---
  9. # %% 1
  10. # Package imports
  11. import matplotlib.pyplot as plt
  12. import numpy as np
  13. import sklearn
  14. import sklearn.datasets
  15. import sklearn.linear_model
  16. import matplotlib
  17. # Display plots inline and change default figure size
  18. # %matplotlib inline
  19. matplotlib.rcParams['figure.figsize'] = (10.0, 8.0)
  20. # %% 2
  21. np.random.seed(3)
  22. X, y = sklearn.datasets.make_moons(200, noise=0.20)
  23. plt.scatter(X[:,0], X[:,1], s=40, c=y, cmap=plt.cm.Spectral)
  24. # %% 3
  25. # Train the logistic rgeression classifier
  26. clf = sklearn.linear_model.LogisticRegressionCV()
  27. clf.fit(X, y)
  28. # %% 4
  29. # Helper function to plot a decision boundary.
  30. # If you don't fully understand this function don't worry, it just generates the contour plot below.
  31. def plot_decision_boundary(pred_func):
  32. # Set min and max values and give it some padding
  33. x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
  34. y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
  35. h = 0.01
  36. # Generate a grid of points with distance h between them
  37. xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
  38. # Predict the function value for the whole gid
  39. Z = pred_func(np.c_[xx.ravel(), yy.ravel()])
  40. Z = Z.reshape(xx.shape)
  41. # Plot the contour and training examples
  42. plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
  43. plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Spectral)
  44. # %% 12
  45. # Plot the decision boundary
  46. plot_decision_boundary(lambda x: clf.predict(x))
  47. plt.title("Logistic Regression")
  48. # %% 15
  49. num_examples = len(X) # training set size
  50. nn_input_dim = 2 # input layer dimensionality
  51. nn_output_dim = 2 # output layer dimensionality
  52. # Gradient descent parameters (I picked these by hand)
  53. epsilon = 0.01 # learning rate for gradient descent
  54. reg_lambda = 0.01 # regularization strength
  55. # %% 7
  56. # Helper function to evaluate the total loss on the dataset
  57. def calculate_loss(model):
  58. W1, b1, W2, b2 = model['W1'], model['b1'], model['W2'], model['b2']
  59. # Forward propagation to calculate our predictions
  60. z1 = X.dot(W1) + b1
  61. a1 = np.tanh(z1)
  62. z2 = a1.dot(W2) + b2
  63. exp_scores = np.exp(z2)
  64. probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True)
  65. # Calculating the loss
  66. corect_logprobs = -np.log(probs[range(num_examples), y])
  67. data_loss = np.sum(corect_logprobs)
  68. # Add regulatization term to loss (optional)
  69. data_loss += reg_lambda/2 * (np.sum(np.square(W1)) + np.sum(np.square(W2)))
  70. return 1./num_examples * data_loss
  71. # %% 8
  72. # Helper function to predict an output (0 or 1)
  73. def predict(model, x):
  74. W1, b1, W2, b2 = model['W1'], model['b1'], model['W2'], model['b2']
  75. # Forward propagation
  76. z1 = x.dot(W1) + b1
  77. a1 = np.tanh(z1)
  78. z2 = a1.dot(W2) + b2
  79. exp_scores = np.exp(z2)
  80. probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True)
  81. return np.argmax(probs, axis=1)
  82. # %% 16
  83. # This function learns parameters for the neural network and returns the model.
  84. # - nn_hdim: Number of nodes in the hidden layer
  85. # - num_passes: Number of passes through the training data for gradient descent
  86. # - print_loss: If True, print the loss every 1000 iterations
  87. def build_model(nn_hdim, num_passes=20000, print_loss=False):
  88. # Initialize the parameters to random values. We need to learn these.
  89. np.random.seed(0)
  90. W1 = np.random.randn(nn_input_dim, nn_hdim) / np.sqrt(nn_input_dim)
  91. b1 = np.zeros((1, nn_hdim))
  92. W2 = np.random.randn(nn_hdim, nn_output_dim) / np.sqrt(nn_hdim)
  93. b2 = np.zeros((1, nn_output_dim))
  94. # This is what we return at the end
  95. model = {}
  96. # Gradient descent. For each batch...
  97. for i in range(0, num_passes):
  98. # Forward propagation
  99. z1 = X.dot(W1) + b1
  100. a1 = np.tanh(z1)
  101. z2 = a1.dot(W2) + b2
  102. exp_scores = np.exp(z2)
  103. probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True)
  104. # Backpropagation
  105. delta3 = probs
  106. delta3[range(num_examples), y] -= 1
  107. dW2 = (a1.T).dot(delta3)
  108. db2 = np.sum(delta3, axis=0, keepdims=True)
  109. delta2 = delta3.dot(W2.T) * (1 - np.power(a1, 2))
  110. dW1 = np.dot(X.T, delta2)
  111. db1 = np.sum(delta2, axis=0)
  112. # Add regularization terms (b1 and b2 don't have regularization terms)
  113. dW2 += reg_lambda * W2
  114. dW1 += reg_lambda * W1
  115. # Gradient descent parameter update
  116. W1 += -epsilon * dW1
  117. b1 += -epsilon * db1
  118. W2 += -epsilon * dW2
  119. b2 += -epsilon * db2
  120. # Assign new parameters to the model
  121. model = { 'W1': W1, 'b1': b1, 'W2': W2, 'b2': b2}
  122. # Optionally print the loss.
  123. # This is expensive because it uses the whole dataset, so we don't want to do it too often.
  124. if print_loss and i % 1000 == 0:
  125. print("Loss after iteration %i: %f" %(i, calculate_loss(model)))
  126. return model
  127. # %% 17
  128. # Build a model with a 3-dimensional hidden layer
  129. model = build_model(3, print_loss=True)
  130. # Plot the decision boundary
  131. plot_decision_boundary(lambda x: predict(model, x))
  132. plt.title("Decision Boundary for hidden layer size 3")
  133. # %% 14
  134. plt.figure(figsize=(16, 32))
  135. hidden_layer_dimensions = [1, 2, 3, 4, 5, 20, 50]
  136. for i, nn_hdim in enumerate(hidden_layer_dimensions):
  137. plt.subplot(5, 2, i+1)
  138. plt.title('Hidden Layer size %d' % nn_hdim)
  139. model = build_model(nn_hdim)
  140. plot_decision_boundary(lambda x: predict(model, x))
  141. plt.show()

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