<s> import scipy . io as sio <EOL> import numpy as np <EOL> from scipy . optimize import minimize <EOL> from common_functions import add_zero_feature , sigmoid , matrix_args_array_only <EOL> def sigmoid_gradient ( z ) : <EOL> return np . multiply ( sigmoid ( z ) , <NUM_LIT:1> - sigmoid ( z ) ) <EOL> @ matrix_args_array_only <EOL> def cf_nn ( nn_params , input_layer_size , hidden_layer_size , num_labels , X , Y , lambda_coef ) : <EOL> Theta1 = nn_params [ <NUM_LIT:0> , : hidden_layer_size * ( input_layer_size + <NUM_LIT:1> ) ] . reshape ( ( hidden_layer_size , ( input_layer_size + <NUM_LIT:1> ) ) ) <EOL> Theta2 = nn_params [ <NUM_LIT:0> , hidden_layer_size * ( input_layer_size + <NUM_LIT:1> ) : ] . reshape ( ( num_labels , ( hidden_layer_size + <NUM_LIT:1> ) ) ) <EOL> m = Y . shape [ <NUM_LIT:1> ] <EOL> Y = Y . A <EOL> A_1 = X <EOL> Z_2 = Theta1 * A_1 . T <EOL> A_2 = sigmoid ( Z_2 ) <EOL> A_2 = add_zero_feature ( A_2 , axis = <NUM_LIT:0> ) <EOL> Z_3 = Theta2 * A_2 <EOL> A_3 = sigmoid ( Z_3 ) <EOL> H = A_3 . A <EOL> J = np . sum ( - Y * np . log ( H ) - ( <NUM_LIT:1> - Y ) * np . log ( <NUM_LIT:1> - H ) ) / m <EOL> reg_J = <NUM_LIT:0.0> <EOL> reg_J += np . sum ( np . power ( Theta1 , <NUM_LIT:2> ) [ : , <NUM_LIT:1> : ] ) <EOL> reg_J += np . sum ( np . power ( Theta2 , <NUM_LIT:2> ) [ : , <NUM_LIT:1> : ] ) <EOL> J += reg_J * ( float ( lambda_coef ) / ( <NUM_LIT:2> * m ) ) <EOL> return J <EOL> @ matrix_args_array_only <EOL> def gf_nn ( nn_params , input_layer_size , hidden_layer_size , num_labels , X , Y , lambda_coef ) : <EOL> Theta1 = nn_params [ <NUM_LIT:0> , : hidden_layer_size * ( input_layer_size + <NUM_LIT:1> ) ] . reshape ( ( hidden_layer_size , ( input_layer_size + <NUM_LIT:1> ) ) ) <EOL> Theta2 = nn_params [ <NUM_LIT:0> , hidden_layer_size * ( input_layer_size + <NUM_LIT:1> ) : ] . reshape ( ( num_labels , ( hidden_layer_size + <NUM_LIT:1> ) ) ) <EOL> m = Y . shape [ <NUM_LIT:1> ] <EOL> A_1 = X <EOL> Z_2 = Theta1 * A_1 . T <EOL> A_2 = sigmoid ( Z_2 ) <EOL> A_2 = add_zero_feature ( A_2 , axis = <NUM_LIT:0> ) <EOL> Z_3 = Theta2 * A_2 <EOL> A_3 = sigmoid ( Z_3 ) <EOL> DELTA_3 = A_3 - Y <EOL> DELTA_2 = np . multiply ( ( Theta2 . T * DELTA_3 ) [ <NUM_LIT:1> : , : ] , sigmoid_gradient ( Z_2 ) ) <EOL> Theta1_grad = ( DELTA_2 * A_1 ) / m <EOL> Theta2_grad = ( DELTA_3 * A_2 . T ) / m <EOL> lambda_coef = float ( lambda_coef ) <EOL> Theta1_grad [ : , <NUM_LIT:1> : ] += ( lambda_coef / m ) * Theta1 [ : , <NUM_LIT:1> : ] <EOL> Theta2_grad [ : , <NUM_LIT:1> : ] += ( lambda_coef / m ) * Theta2 [ : , <NUM_LIT:1> : ] <EOL> return np . concatenate ( ( Theta1_grad . A1 , Theta2_grad . A1 ) ) <EOL> def rand_initialize_weights ( L_in , L_out ) : <EOL> epsilon_init = <NUM_LIT> <EOL> return np . random . rand ( L_out , <NUM_LIT:1> + L_in ) * <NUM_LIT:2> * epsilon_init - epsilon_init <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> data = sio . loadmat ( '<STR_LIT>' ) <EOL> y = data [ '<STR_LIT:y>' ] <EOL> X = data [ '<STR_LIT:X>' ] <EOL> X = add_zero_feature ( X ) <EOL> data = sio . loadmat ( '<STR_LIT>' ) <EOL> Theta1 = data [ '<STR_LIT>' ] <EOL> Theta2 = data [ '<STR_LIT>' ] <EOL> nn_params = np . concatenate ( ( Theta1 . ravel ( ) , Theta2 . ravel ( ) ) ) <EOL> input_layer_size = <NUM_LIT> <EOL> hidden_layer_size = <NUM_LIT> <EOL> num_labels = <NUM_LIT:10> <EOL> m = len ( y ) <EOL> Y = ( np . arange ( num_labels ) [ : , np . newaxis ] == ( y . T - <NUM_LIT:1> ) ) . astype ( float ) <EOL> for lambda_coef in ( <NUM_LIT:0> , <NUM_LIT:1> ) : <EOL> print '<STR_LIT>' . format ( cf_nn ( nn_params , input_layer_size , hidden_layer_size , num_labels , X , Y , lambda_coef ) , lambda_coef ) <EOL> initial_Theta1 = rand_initialize_weights ( input_layer_size , hidden_layer_size ) <EOL> initial_Theta2 = rand_initialize_weights ( hidden_layer_size , num_labels ) <EOL> initial_nn_params = np . concatenate ( ( initial_Theta1 . ravel ( ) , initial_Theta2 . ravel ( ) ) ) <EOL> res = minimize ( cf_nn , initial_nn_params , method = '<STR_LIT>' , jac = gf_nn , options = { '<STR_LIT>' : True , '<STR_LIT>' : <NUM_LIT:100> } , <EOL> args = ( input_layer_size , hidden_layer_size , num_labels , X , Y , lambda_coef ) ) . x <EOL> Theta1 = res [ : hidden_layer_size * ( input_layer_size + <NUM_LIT:1> ) ] . reshape ( ( hidden_layer_size , ( input_layer_size + <NUM_LIT:1> ) ) ) <EOL> Theta2 = res [ hidden_layer_size * ( input_layer_size + <NUM_LIT:1> ) : ] . reshape ( ( num_labels , ( hidden_layer_size + <NUM_LIT:1> ) ) ) <EOL> h1 = sigmoid ( np . dot ( X , Theta1 . T ) ) <EOL> h2 = sigmoid ( np . dot ( add_zero_feature ( h1 ) , Theta2 . T ) ) <EOL> y_pred = np . argmax ( h2 , axis = <NUM_LIT:1> ) + <NUM_LIT:1> <EOL> print '<STR_LIT>' . format ( np . mean ( y_pred == y . ravel ( ) , ) * <NUM_LIT:100> ) </s>
