优达学城深度学习之五——卷积神经网络

举报
小小谢先生 发表于 2022/04/14 01:35:47 2022/04/14
【摘要】 梯度下降算法推导与实现 import matplotlib.pyplot as pltimport numpy as npimport pandas as pd #Some helper functions for plotting and drawing lines def plot_points(X, y): admit...

梯度下降算法推导与实现


  
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import pandas as pd
  4. #Some helper functions for plotting and drawing lines
  5. def plot_points(X, y):
  6. admitted = X[np.argwhere(y==1)]
  7. rejected = X[np.argwhere(y==0)]
  8. plt.scatter([s[0][0] for s in rejected], [s[0][1] for s in rejected], s = 25, color = 'blue', edgecolor = 'k')
  9. plt.scatter([s[0][0] for s in admitted], [s[0][1] for s in admitted], s = 25, color = 'red', edgecolor = 'k')
  10. def display(m, b, color='g--'):
  11. plt.xlim(-0.05,1.05)
  12. plt.ylim(-0.05,1.05)
  13. x = np.arange(-10, 10, 0.1)
  14. plt.plot(x, m*x+b, color)
  15. #读取与绘制数据
  16. data = pd.read_csv('data.csv', header=None)
  17. X = np.array(data[[0,1]])
  18. y = np.array(data[2])
  19. plot_points(X,y)
  20. plt.show()
  21. # Implement the following functions
  22. # Activation (sigmoid) function
  23. def sigmoid(x):
  24. return 1/(1+np.exp(-x))
  25. # Output (prediction) formula
  26. def output_formula(features, weights, bias):
  27. sigmoid(np.dot(features, weights) + bias)
  28. # Error (log-loss) formula
  29. def error_formula(y, output):
  30. return - y*np.log(output) - (1 - y) * np.log(1-output)
  31. # Gradient descent step
  32. def update_weights(x, y, weights, bias, learnrate):
  33. output = output_formula(x, weights, bias)
  34. d_error = -(y - output)
  35. weights -= learnrate * d_error * x
  36. bias -= learnrate * d_error
  37. return weights, bias
  38. np.random.seed(44)
  39. epochs = 100
  40. learnrate = 0.01
  41. def train(features, targets, epochs, learnrate, graph_lines=False):
  42. errors = []
  43. n_records, n_features = features.shape
  44. last_loss = None
  45. weights = np.random.normal(scale=1 / n_features**.5, size=n_features)
  46. bias = 0
  47. for e in range(epochs):
  48. del_w = np.zeros(weights.shape)
  49. for x, y in zip(features, targets):
  50. output = output_formula(x, weights, bias)
  51. error = error_formula(y, output)
  52. weights, bias = update_weights(x, y, weights, bias, learnrate)
  53. # Printing out the log-loss error on the training set
  54. out = output_formula(features, weights, bias)
  55. loss = np.mean(error_formula(targets, out))
  56. errors.append(loss)
  57. if e % (epochs / 10) == 0:
  58. print("\n========== Epoch", e,"==========")
  59. if last_loss and last_loss < loss:
  60. print("Train loss: ", loss, " WARNING - Loss Increasing")
  61. else:
  62. print("Train loss: ", loss)
  63. last_loss = loss
  64. predictions = out > 0.5
  65. accuracy = np.mean(predictions == targets)
  66. print("Accuracy: ", accuracy)
  67. if graph_lines and e % (epochs / 100) == 0:
  68. display(-weights[0]/weights[1], -bias/weights[1])
  69. # Plotting the solution boundary
  70. plt.title("Solution boundary")
  71. display(-weights[0]/weights[1], -bias/weights[1], 'black')
  72. # Plotting the data
  73. plot_points(features, targets)
  74. plt.show()
  75. # Plotting the error
  76. plt.title("Error Plot")
  77. plt.xlabel('Number of epochs')
  78. plt.ylabel('Error')
  79. plt.plot(errors)
  80. plt.show()
  81. #训练算法
  82. train(X, y, epochs, learnrate, True)

反向传播

反向传播流程如下:

  • 进行前向反馈运算。
  • 将模型的输出与期望的输出进行比较。
  • 计算误差。
  • 向后运行前向反馈运算(反向传播),将误差分散到每个权重上。
  • 更新权重,并获得更好的模型。
  • 继续此流程,直到获得很好的模型。

实战演练:利用神经网络来预测学生录取情况

数据集来源: http://www.ats.ucla.edu/


  
  1. # Importing pandas and numpy
  2. import pandas as pd
  3. import numpy as np
  4. # Reading the csv file into a pandas DataFrame
  5. data = pd.read_csv('student_data.csv')
  6. # Printing out the first 10 rows of our data
  7. data[:10]
  8. #绘制数据
  9. # Importing matplotlib
  10. import matplotlib.pyplot as plt
  11. %matplotlib inline
  12. # Function to help us plot
  13. def plot_points(data):
  14. X = np.array(data[["gre","gpa"]])
  15. y = np.array(data["admit"])
  16. admitted = X[np.argwhere(y==1)]
  17. rejected = X[np.argwhere(y==0)]
  18. plt.scatter([s[0][0] for s in rejected], [s[0][1] for s in rejected], s = 25, color = 'red', edgecolor = 'k')
  19. plt.scatter([s[0][0] for s in admitted], [s[0][1] for s in admitted], s = 25, color = 'cyan', edgecolor = 'k')
  20. plt.xlabel('Test (GRE)')
  21. plt.ylabel('Grades (GPA)')
  22. # Plotting the points
  23. plot_points(data)
  24. plt.show()
  25. # Separating the ranks
  26. data_rank1 = data[data["rank"]==1]
  27. data_rank2 = data[data["rank"]==2]
  28. data_rank3 = data[data["rank"]==3]
  29. data_rank4 = data[data["rank"]==4]
  30. # Plotting the graphs
  31. plot_points(data_rank1)
  32. plt.title("Rank 1")
  33. plt.show()
  34. plot_points(data_rank2)
  35. plt.title("Rank 2")
  36. plt.show()
  37. plot_points(data_rank3)
  38. plt.title("Rank 3")
  39. plt.show()
  40. plot_points(data_rank4)
  41. plt.title("Rank 4")
  42. plt.show()
  43. #将评级进行one-shot编码
  44. # TODO: Make dummy variables for rank
  45. one_hot_data = pd.concat([data, pd.get_dummies(data['rank'], prefix='rank')], axis=1)
  46. # TODO: Drop the previous rank column
  47. one_hot_data = one_hot_data.drop('rank', axis=1)
  48. # Print the first 10 rows of our data
  49. one_hot_data[:10]
  50. #缩放数据
  51. # Making a copy of our data
  52. processed_data = one_hot_data[:]
  53. # TODO: Scale the columns
  54. processed_data['gre']=processed_data['gre']/800
  55. processed_data['gpa']=processed_data['gpa']/4.0
  56. # Printing the first 10 rows of our procesed data
  57. processed_data[:10]
  58. #将数据分成训练集和测试集
  59. sample = np.random.choice(processed_data.index, size=int(len(processed_data)*0.9), replace=False)
  60. train_data, test_data = processed_data.iloc[sample], processed_data.drop(sample)
  61. print("Number of training samples is", len(train_data))
  62. print("Number of testing samples is", len(test_data))
  63. print(train_data[:10])
  64. print(test_data[:10])
  65. #将数据分成特征和目标
  66. features = train_data.drop('admit', axis=1)
  67. targets = train_data['admit']
  68. features_test = test_data.drop('admit', axis=1)
  69. targets_test = test_data['admit']
  70. print(features[:10])
  71. print(targets[:10])
  72. #训练二层神经网络
  73. Activation (sigmoid) function
  74. def sigmoid(x):
  75. return 1 / (1 + np.exp(-x))
  76. def sigmoid_prime(x):
  77. return sigmoid(x) * (1-sigmoid(x))
  78. def error_formula(y, output):
  79. return - y*np.log(output) - (1 - y) * np.log(1-output)
  80. #误差反向传播
  81. # TODO: Write the error term formula
  82. def error_term_formula(y, output):
  83. return (y-output)*sigmoid_prime(x)
  84. def error_term_formula(x, y, output):
  85. return (y-output) * output * (1 - output)
  86. # Neural Network hyperparameters
  87. epochs = 1000
  88. learnrate = 0.5
  89. # Training function
  90. def train_nn(features, targets, epochs, learnrate):
  91. # Use to same seed to make debugging easier
  92. np.random.seed(42)
  93. n_records, n_features = features.shape
  94. last_loss = None
  95. # Initialize weights
  96. weights = np.random.normal(scale=1 / n_features**.5, size=n_features)
  97. for e in range(epochs):
  98. del_w = np.zeros(weights.shape)
  99. for x, y in zip(features.values, targets):
  100. # Loop through all records, x is the input, y is the target
  101. # Activation of the output unit
  102. # Notice we multiply the inputs and the weights here
  103. # rather than storing h as a separate variable
  104. output = sigmoid(np.dot(x, weights))
  105. # The error, the target minus the network output
  106. error = error_formula(y, output)
  107. # The error term
  108. # Notice we calulate f'(h) here instead of defining a separate
  109. # sigmoid_prime function. This just makes it faster because we
  110. # can re-use the result of the sigmoid function stored in
  111. # the output variable
  112. error_term = error_term_formula(x,y, output)
  113. # The gradient descent step, the error times the gradient times the inputs
  114. del_w += error_term * x
  115. # Update the weights here. The learning rate times the
  116. # change in weights, divided by the number of records to average
  117. weights += learnrate * del_w / n_records
  118. # Printing out the error on the training set
  119. if e % (epochs / 10) == 0:
  120. out = sigmoid(np.dot(features, weights))
  121. loss = np.mean((out - targets) ** 2)
  122. print("Epoch:", e)
  123. if last_loss and last_loss < loss:
  124. print("Train loss: ", loss, " WARNING - Loss Increasing")
  125. else:
  126. print("Train loss: ", loss)
  127. last_loss = loss
  128. print("=========")
  129. print("Finished training!")
  130. return weights
  131. weights = train_nn(features, targets, epochs, learnrate)
  132. #计算测试数据的准确度
  133. # Calculate accuracy on test data
  134. tes_out = sigmoid(np.dot(features_test, weights))
  135. predictions = tes_out > 0.5
  136. accuracy = np.mean(predictions == targets_test)
  137. print("Prediction accuracy: {:.3f}".format(accuracy))

 

文章来源: blog.csdn.net,作者:小小谢先生,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/xiewenrui1996/article/details/89856069

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。