필기 신경망 학습 파이썬 깊이

수입 NumPy와의
 수입 scipy.special
 수입 PLT의 같은 matplotlib.pyplot 

클래스 : 신경망
   데프  __init__ (자기, inputnodes, hiddennodes, outputnodes, learningrate) : 
    self.inodes = inputnodes 
    self.hnodes = hiddennodes 
    self.onodes = outputnodes 

    self.wih = NumPy와. random.normal (0.0, POW (self.inodes, -0.5 ), (self.hnodes, self.inodes)) 
    self.who = numpy.random.normal (0.0, POW (self.hnodes, -0.5 ) (자체 .onodes, self.hnodes)) 
    
    self.lr = learningrate

    self.activation_function = 람다 X (X) scipy.special.expit # 激活函数

  DEF 열차 (자기, inputs_list, targets_list) 
    입력 = numpy.array (inputs_list, ndmin = 2 ) .T의 
    대상 = numpy.array (targets_list, ndmin = 2 ) .T 

    hidden_inputs = numpy.dot (self.wih, 입력) 
    hidden_outputs = self.activation_function (hidden_inputs) 

    final_inputs = numpy.dot (self.who, hidden_outputs) 
    final_outputs = self.activation_function (final_inputs) 

    output_errors의 = 타겟 - final_outputs
    hidden_errors numpy.dot (self.who, hidden_outputs)= numpy.dot (self.who.T, output_errors) 

    self.who + = self.lr numpy.dot * ((output_errors final_outputs * * (1.0 - final_outputs)) numpy.transpose (hidden_outputs)) 
    self.wih + = self.lr numpy.dot * ((hidden_errors hidden_outputs * * (1.0 - hidden_outputs)) numpy.transpose (입력)) 

  데프 쿼리 (자기, inputs_list) 
    입력 = numpy.array (inputs_list, ndmin = 2 ) .T 

    hidden_inputs = numpy.dot (self.wih, 입력) 
    hidden_outputs = self.activation_function (hidden_inputs) 
    final_inputs = 
    final_outputs = self.activation_function (final_inputs)

    반환 final_outputs 
  

input_nodes = 784 
hidden_nodes = 200 
output_nodes = 10 
learing_rate = 0.1 
N = 신경망 (input_nodes, hidden_nodes, output_nodes, learing_rate) 

train_data_file = 개방 ( ' mnist_train.csv ' , ' R ' ) 
train_data_list = train_data_file.readlines () 
train_data_file.close을 () 

에폭 = 5
 에 대한 E 범위 (에포크)
   에 대한 기록 train_data_list : 
    all_valuesrecord.split = ( ' , ' )
     # image_array = numpy.asfarray (all_values [1]). 바꿀 ((28, 28)) 
    #에 plt.imshow (image_array, cmap를 = '회색'보간 = '없음') 
    #의 plt.show () 
    입력이 = (numpy.asfarray (all_values [1]) * 0.99 / 255.0) +0.01 
    대상 = numpy.zeros (output_nodes) + 0.01 
    타겟 [INT (all_values [0])] = 0.99 
    N. 열차 (입력, 목표) 


test_data_file = 개방 ( ' mnist_test.csv ' , ' R ' ) 
test_data_list = test_data_file.readlines () 
test_data_file.close ()
# all_values test_data_list = [0] .split ( '') 

# # image_array = numpy.asfarray (all_values [1]). 바꿀 ((28, 28)) 
#의 #의 plt.imshow (image_array, cmap를 = '회색' 보간 = '없음') 
#의 #의 plt.show () 

#의 출력 n.query = ((numpy.asfarray (all_values [1]) / 255.0 * 0.99) +0.01) 


득점표 = []
 에 대한 기록 test_data_list : 
  all_values record.split = ( ' , ' ) 
  correct_label = INT (all_values [0])
   #에 인쇄 (correct_label 'correct_label')
  입력이 = (numpy.asfarray (all_values [1]) / 255.0 * 0.99) +0.01
  출력 = n.query (입력) 
  레이블 = numpy.argmax (출력)
   #에 인쇄 (라벨 '네트워크 않음 ") 
  경우 (라벨 == correct_label) 
    scorecard.append ( 1 )
   다른 : 
    scorecard.append (0) 
scorecard_array = numpy.asarray (득점표)
 인쇄 ( " 성능 = " , scorecard_array.sum () / scorecard_array.size)

 

추천

출처www.cnblogs.com/Erick-L/p/11785905.html