当前位置:   article > 正文

吴恩达机器学习ex4代码_吴恩达机器学习课程代码

吴恩达机器学习课程代码
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import scipy.io as sio
  4. from scipy.io import loadmat
  5. import scipy.optimize as opt
  6. from sklearn.metrics import classification_report
  7. '''========================================函数部分========================================'''
  8. '''数据图像化'''
  9. def mat2img(X):
  10. sample_index = np.random.choice(len(X),100)
  11. images = X[sample_index,:]
  12. # print('image_shape:',images.shape) #100*400
  13. fig,ax = plt.subplots(ncols=10,nrows=10,figsize=(5,5),sharex=True,sharey=True)
  14. plt.xticks([])
  15. plt.yticks([])
  16. for r in range(10):
  17. for c in range(10):
  18. ax[r,c].imshow(images[10*r+c,:].reshape(20,20).T,cmap='gray_r') #reshape是将像素点数据重新转换为像素点
  19. plt.show()
  20. '''参数拍平'''
  21. def flatten_connect(t1,t2):
  22. return np.concatenate((t1.flatten(),t2.flatten())) #先拍平,再连起来。等同于 np.r_[t1.flatten(),t2.flatten()]
  23. '''参数复原'''
  24. def reshape(seq):
  25. t1 = seq[:25*401].reshape(25,401)
  26. t2 = seq[25*401:].reshape(10,26)
  27. return t1, t2
  28. '''前向传播函数'''
  29. def feed_forward(theta,X):
  30. Theta1, Theta2 = reshape(theta)
  31. a1 = X
  32. z2 = a1 @ Theta1.T
  33. a2 = sigmoid(z2)
  34. a2 = np.insert(a2,0,1,axis=1)
  35. z3 = a2 @ Theta2.T
  36. a3 = sigmoid(z3)
  37. return a1,z2,a2,z3,a3
  38. '''激活函数'''
  39. def sigmoid(z):
  40. return 1 / (1 + np.exp(-z))
  41. '''代价函数(带正则)'''
  42. def costReg(theta,X,y,lbd):
  43. a1, z2, a2, z3, h = feed_forward(theta,X) #h即a3,预测值
  44. # print('h:',h) #h是推测的具体的数,不是概率
  45. # print(h.shape) #5000*10
  46. J = 0
  47. for i in range(X.shape[0]): #range(5000)
  48. ans=np.multiply(-y[i],np.log(h[i]))-np.multiply((1-y[i]),np.log(1-h[i])) #每个样本的cost分别求,再加起来
  49. sum = np.sum(ans)
  50. J += sum
  51. Theta1, Theta2 = reshape(theta)
  52. J = J/(X.shape[0]) + lbd / (2 * X.shape[0]) * (np.sum(np.power(Theta1[:, 1:], 2)) + np.sum(np.power(Theta2[:, 1:], 2))) #注意正则不要带theta1和theta2的首项
  53. return J
  54. '''随机初始化'''
  55. def random_init(size):
  56. return np.random.uniform(-0.12,0.12,size) #随机生成指定范围的浮点数,从一个均匀分布[low,high)中随机采样,定义域是左闭右开,ndarray类型,其形状与size中描述一致.
  57. '''反向传播求梯度gradient(不带正则)'''
  58. def Gradient(theta, X, y):
  59. a1, z2, a2, z3, h = feed_forward(theta,X) #h即a3,预测值
  60. Theta1, Theta2 = reshape(theta)
  61. d3 = h - y #5000*10
  62. d2 = np.multiply( d3 @ Theta2[:, 1:], (sigmoid(z2)*(1-sigmoid(z2))) ) #5000*25。尤其注意矩阵乘法和对位相乘的区别!
  63. D2 = d3.T @ a2 #10*26
  64. D1 = d2.T @ a1 #25*401
  65. return D1, D2
  66. '''反向传播求梯度gradient(带正则)'''
  67. def Regularized_gradient(theta,X,y,lbd):
  68. a1, z2, a2, z3, h = feed_forward(theta,X)
  69. D1, D2 = Gradient(theta, X, y)
  70. Theta1, Theta2 = reshape(theta)
  71. Theta1[:,0]=0 #当j=0时,不惩罚,所以干脆将第一列变成0
  72. Theta2[:,0]=0
  73. reg_D1=D1+(lbd/X.shape[0])*Theta1
  74. reg_D2=D2+(lbd/X.shape[0])*Theta2
  75. return np.concatenate((reg_D1.flatten(),reg_D2.flatten()))
  76. '''梯度检测,正式运行时全部忽略'''
  77. # def Gradient_checking(theta,X,y):
  78. # numgrad = np.zeros(theta.shape) #(10280,)
  79. # perturb = np.zeros(theta.shape) #(10280,)
  80. # e = 1e-4
  81. # for i in range(len(theta)):
  82. # perturb[i] = e
  83. # left = cost_alternative(theta + perturb,X,y,1)
  84. # right = cost_alternative(theta - perturb,X,y,1)
  85. # #计算数值梯度
  86. # numgrad[i] = (left - right) / (2*e)
  87. # perturb[i] = 0
  88. # return numgrad
  89. '''准确度'''
  90. def accuracy(theta,X,y):
  91. a1, z2, a2, z3, h = feed_forward(theta, X)
  92. y_predict = np.argmax(h, axis=1) + 1
  93. accuracy = classification_report(y, y_predict)
  94. return accuracy
  95. def plot_hidden(theta):
  96. Theta1,Theta2=reshape(theta)
  97. Theta1=Theta1[:,1:] #去掉偏置项的theta
  98. fix,ax_array=plt.subplots(nrows=5,ncols=5,sharex=True,sharey=True,figsize=(8,8))
  99. for r in range(5): #这里的5和下面的5,是因为隐藏层里一共有25个神经元(去掉一个偏置项)
  100. for c in range(5):
  101. ax_array[r,c].matshow(Theta1[r*5+c].reshape(20,20),cmap='gray_r')
  102. plt.xticks([])
  103. plt.yticks([])
  104. plt.show()
  105. '''========================================计算部分========================================'''
  106. '''导入数据X,y'''
  107. data = loadmat('D:\新大陆\吴恩达机器学习\ex4\ex4data1.mat')
  108. # print(data)
  109. # print(type(data)) #类型是字典
  110. X = data['X']
  111. y = data['y']
  112. # print(X)
  113. print('X_shape:',X.shape) #array 5000*400,还没有加X0
  114. # print(y)
  115. print('y_shape:',y.shape) #array 5000*1
  116. ''' 数据图像化'''
  117. mat2img(X)
  118. '''导入θ'''
  119. weight = loadmat('D:\新大陆\吴恩达机器学习\ex4\ex4weights.mat')
  120. # print(weight)
  121. Theta1 = weight['Theta1']
  122. Theta2 = weight['Theta2']
  123. print('Theta1_shape:',Theta1.shape) #25*401,已经加了偏置神经元
  124. print('Theta2_shape:',Theta2.shape) #10*26,已经加了偏置神经元
  125. # print('Theta1',Theta1)
  126. '''X数据处理:插入X0'''
  127. X = np.insert(X,0,values=np.ones(X.shape[0]),axis=1)
  128. print('new_X_shape:',X.shape) #5000*401
  129. '''y数据处理:拍平,扩展'''
  130. y = y.flatten() #(5000,),type是list,拍平之前是array
  131. result = []
  132. for it in y: #这种写法指的是y作为一个list,其中每一个数的数值
  133. y_array = np.zeros(10)
  134. y_array[it - 1] = 1
  135. result.append(y_array) #直到这里为止,result都是list
  136. y = np.array(result)
  137. print('new_y_shape:',y.shape) #5000*10
  138. '''算代价cost'''
  139. theta = flatten_connect(Theta1,Theta2)
  140. print('regularized original cost:',costReg(theta,X,y,1))
  141. '''================================================梯度检测,正式运行时全部关闭=============================================='''
  142. '''梯度检测'''
  143. # print(Gradient_checking(theta,X,y))
  144. '''
  145. 结果1(代价函数正则,梯度下降不正则)
  146. numgrad = [ 6.18712742e-05 -1.94289029e-12 5.55111512e-13 ... 4.70513167e-05 -5.01718606e-04 5.07825786e-04]
  147. '''
  148. '''====================================================================================================================='''
  149. '''优化参数theta'''
  150. theta_init = random_init(10285)
  151. min = opt.minimize(fun=costReg, x0=theta, args=(X, y, 1), method='TNC', jac=Regularized_gradient)
  152. print(min)
  153. '''计算准确度'''
  154. theta = min.x #优化后的theta
  155. y = data['y']
  156. print('accuracy:',accuracy(theta,X,y)) #X(5000,401),y(5000,1),theta(10285,)
  157. '''显示隐藏层'''
  158. plot_hidden(theta)
  159. '''
  160. 当lbd = 0.1时
  161. fun: 0.11077798874560327 现在的代价
  162. jac: array([-2.53133449e+00, -2.11247519e-13, 4.38827691e-14, ...,
  163. -1.18178528e-01, -1.34341584e+00, 7.39197308e-01])
  164. message: 'Converged (|f_n-f_(n-1)| ~= 0)'
  165. nfev: 174
  166. nit: 6
  167. status: 1
  168. success: True
  169. x: array([-5.39307423e-02, -1.05623759e-08, 2.19413846e-09, ...,
  170. -2.95529819e-01, 3.38539600e+00, -2.61742190e+00])
  171. accuracy: precision recall f1-score support
  172. 1 0.99 1.00 1.00 500
  173. 2 0.99 1.00 1.00 500
  174. 3 1.00 0.99 1.00 500
  175. 4 1.00 1.00 1.00 500
  176. 5 1.00 1.00 1.00 500
  177. 6 1.00 1.00 1.00 500
  178. 7 1.00 0.99 0.99 500
  179. 8 1.00 1.00 1.00 500
  180. 9 1.00 0.99 0.99 500
  181. 10 1.00 1.00 1.00 500
  182. accuracy 1.00 5000
  183. macro avg 1.00 1.00 1.00 5000
  184. weighted avg 1.00 1.00 1.00 5000
  185. 要注意,这里的学习率是0.1,也就是不怎么惩罚,所以这个很高的准确度是过拟合的结果,对新的样本,准确率将大大降低(我们是将所有5000个样本都用上了的,相当于没有验证集。这里的高准确度也只是针对这5000个样本)。
  186. '''
  1. X_shape: (5000, 400)
  2. y_shape: (5000, 1)
  3. Theta1_shape: (25, 401)
  4. Theta2_shape: (10, 26)
  5. new_X_shape: (5000, 401)
  6. new_y_shape: (5000, 10)
  7. regularized original cost: 0.38376985909092354
  8. fun: 0.3504970874968651
  9. jac: array([ 6.28581456e-01, -2.11247727e-12, 4.38828123e-13, ...,
  10. -8.25089493e-01, -4.03046149e+00, 2.34724368e+00])
  11. message: 'Converged (|f_n-f_(n-1)| ~= 0)'
  12. nfev: 661
  13. nit: 18
  14. status: 1
  15. success: True
  16. x: array([-2.70981783e-02, -1.05623863e-08, 2.19414062e-09, ...,
  17. -2.52479302e-01, 1.47852395e+00, -1.47648711e+00])
  18. accuracy: precision recall f1-score support
  19. 1 0.97 0.98 0.98 500
  20. 2 0.98 0.98 0.98 500
  21. 3 0.98 0.97 0.97 500
  22. 4 0.97 0.97 0.97 500
  23. 5 0.98 0.99 0.99 500
  24. 6 0.99 0.99 0.99 500
  25. 7 0.98 0.97 0.98 500
  26. 8 0.98 0.99 0.98 500
  27. 9 0.97 0.97 0.97 500
  28. 10 0.99 0.99 0.99 500
  29. accuracy 0.98 5000
  30. macro avg 0.98 0.98 0.98 5000
  31. weighted avg 0.98 0.98 0.98 5000

数据图像化(抽样100个):

隐藏层图像化:

部分代码参考大佬:Phoenix_ZengHao。感谢所有在机器学习路上的引路人。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/807444
推荐阅读
相关标签
  

闽ICP备14008679号