@@ -44,8 +44,8 @@ def neuralNetwork(input_layer_size,hidden_layer_size,out_put_layer):
4444 #np.savetxt("testTheta.csv",initial_nn_params,delimiter=",")
4545 start = time .time ()
4646 result = optimize .fmin_cg (nnCostFunction , initial_nn_params , fprime = nnGradient , args = (input_layer_size ,hidden_layer_size ,out_put_layer ,X ,y ,Lambda ))
47- print u'执行时间:' ,time .time ()- start
48- print result
47+ print ( u'执行时间:' ,time .time ()- start )
48+ print ( result )
4949 '''可视化 Theta1'''
5050 length = result .shape [0 ]
5151 Theta1 = result [0 :hidden_layer_size * (input_layer_size + 1 )].reshape (hidden_layer_size ,input_layer_size + 1 )
@@ -54,7 +54,7 @@ def neuralNetwork(input_layer_size,hidden_layer_size,out_put_layer):
5454 display_data (Theta2 [:,1 :length ])
5555 '''预测'''
5656 p = predict (Theta1 ,Theta2 ,X )
57- print u"预测准确度为:%f%%" % np .mean (np .float64 (p == y .reshape (- 1 ,1 ))* 100 )
57+ print ( u"预测准确度为:%f%%" % np .mean (np .float64 (p == y .reshape (- 1 ,1 ))* 100 ) )
5858 res = np .hstack ((p ,y .reshape (- 1 ,1 )))
5959 np .savetxt ("predict.csv" , res , delimiter = ',' )
6060
@@ -81,8 +81,12 @@ def display_data(imgData):
8181 display_array = - np .ones ((pad + rows_count * (height + pad ),pad + cols_count * (width + pad )))
8282 for i in range (rows_count ):
8383 for j in range (cols_count ):
84+ if sum >= m : #超过了行数,退出当前循环
85+ break ;
8486 display_array [pad + i * (height + pad ):pad + i * (height + pad )+ height ,pad + j * (width + pad ):pad + j * (width + pad )+ width ] = imgData [sum ,:].reshape (height ,width ,order = "F" ) # order=F指定以列优先,在matlab中是这样的,python中需要指定,默认以行
8587 sum += 1
88+ if sum >= m : #超过了行数,退出当前循环
89+ break ;
8690
8791 plt .imshow (display_array ,cmap = 'gray' ) #显示灰度图像
8892 plt .axis ('off' )
@@ -219,7 +223,7 @@ def checkGradient(Lambda = 0):
219223 step [i ]= 0
220224 # 显示两列比较
221225 res = np .hstack ((num_grad .reshape (- 1 ,1 ),grad .reshape (- 1 ,1 )))
222- print res
226+ print ( res )
223227
224228# 初始化调试的theta权重
225229def debugInitializeWeights (fan_in ,fan_out ):
0 commit comments