生活随笔
收集整理的這篇文章主要介紹了
构建神经网络- 手写字体识别案例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
神經網絡構建:
Multilayer_Perceptron.py:
import numpy
as np
from utils
.features
import prepare_for_training
from utils
.hypothesis
import sigmoid
, sigmoid_gradient
class MultilayerPerceptron:def __init__(self
,data
,labels
,layers
,normalize_data
=False):data_processed
= prepare_for_training
(data
,normalize_data
= normalize_data
)[0]self
.data
= data_processedself
.labels
= labelsself
.layers
= layers self
.normalize_data
= normalize_dataself
.thetas
= MultilayerPerceptron
.thetas_init
(layers
)def predict(self
,data
):data_processed
= prepare_for_training
(data
,normalize_data
= self
.normalize_data
)[0]num_examples
= data_processed
.shape
[0]predictions
= MultilayerPerceptron
.feedforward_propagation
(data_processed
,self
.thetas
,self
.layers
)return np
.argmax
(predictions
,axis
=1).reshape
((num_examples
,1))def train(self
,max_iterations
=1000,alpha
=0.1):unrolled_theta
= MultilayerPerceptron
.thetas_unroll
(self
.thetas
)(optimized_theta
,cost_history
) = MultilayerPerceptron
.gradient_descent
(self
.data
,self
.labels
,unrolled_theta
,self
.layers
,max_iterations
,alpha
)self
.thetas
= MultilayerPerceptron
.thetas_roll
(optimized_theta
,self
.layers
)return self
.thetas
,cost_history
@staticmethoddef thetas_init(layers
):num_layers
= len(layers
)thetas
= {}for layer_index
in range(num_layers
- 1):"""會執行兩次,得到兩組參數矩陣:25*785 , 10*26"""in_count
= layers
[layer_index
]out_count
= layers
[layer_index
+1]thetas
[layer_index
] = np
.random
.rand
(out_count
,in_count
+1)*0.05 return thetas
@staticmethoddef thetas_unroll(thetas
):num_theta_layers
= len(thetas
)unrolled_theta
= np
.array
([])for theta_layer_index
in range(num_theta_layers
):unrolled_theta
= np
.hstack
((unrolled_theta
,thetas
[theta_layer_index
].flatten
()))return unrolled_theta
@staticmethoddef gradient_descent(data
,labels
,unrolled_theta
,layers
,max_iterations
,alpha
):optimized_theta
= unrolled_thetacost_history
= []for _
in range(max_iterations
):cost
= MultilayerPerceptron
.cost_function
(data
,labels
,MultilayerPerceptron
.thetas_roll
(optimized_theta
,layers
),layers
)cost_history
.append
(cost
)theta_gradient
= MultilayerPerceptron
.gradient_step
(data
,labels
,optimized_theta
,layers
)optimized_theta
= optimized_theta
- alpha
* theta_gradient
return optimized_theta
,cost_history
@staticmethod def gradient_step(data
,labels
,optimized_theta
,layers
):theta
= MultilayerPerceptron
.thetas_roll
(optimized_theta
,layers
)thetas_rolled_gradients
= MultilayerPerceptron
.back_propagation
(data
,labels
,theta
,layers
)thetas_unrolled_gradients
= MultilayerPerceptron
.thetas_unroll
(thetas_rolled_gradients
)return thetas_unrolled_gradients
@staticmethod def back_propagation(data
,labels
,thetas
,layers
):num_layers
= len(layers
)(num_examples
,num_features
) = data
.shapenum_label_types
= layers
[-1]deltas
= {}for layer_index
in range(num_layers
-1 ):in_count
= layers
[layer_index
]out_count
= layers
[layer_index
+1]deltas
[layer_index
] = np
.zeros
((out_count
,in_count
+1)) for example_index
in range(num_examples
):layers_inputs
= {}layers_activations
= {}layers_activation
= data
[example_index
,:].reshape
((num_features
,1))layers_activations
[0] = layers_activation
for layer_index
in range(num_layers
- 1):layer_theta
= thetas
[layer_index
] layer_input
= np
.dot
(layer_theta
,layers_activation
) layers_activation
= np
.vstack
((np
.array
([[1]]),sigmoid
(layer_input
)))layers_inputs
[layer_index
+ 1] = layer_input layers_activations
[layer_index
+ 1] = layers_activation output_layer_activation
= layers_activation
[1:,:]delta
= {}bitwise_label
= np
.zeros
((num_label_types
,1))bitwise_label
[labels
[example_index
][0]] = 1delta
[num_layers
- 1] = output_layer_activation
- bitwise_label
for layer_index
in range(num_layers
- 2,0,-1):layer_theta
= thetas
[layer_index
]next_delta
= delta
[layer_index
+1]layer_input
= layers_inputs
[layer_index
]layer_input
= np
.vstack
((np
.array
((1)),layer_input
))delta
[layer_index
] = np
.dot
(layer_theta
.T
,next_delta
)*sigmoid_gradient
(layer_input
)delta
[layer_index
] = delta
[layer_index
][1:,:]for layer_index
in range(num_layers
-1):layer_delta
= np
.dot
(delta
[layer_index
+1],layers_activations
[layer_index
].T
)deltas
[layer_index
] = deltas
[layer_index
] + layer_delta
for layer_index
in range(num_layers
-1):deltas
[layer_index
] = deltas
[layer_index
] * (1/num_examples
)return deltas
@staticmethod def cost_function(data
,labels
,thetas
,layers
):num_layers
= len(layers
)num_examples
= data
.shape
[0]num_labels
= layers
[-1]predictions
= MultilayerPerceptron
.feedforward_propagation
(data
,thetas
,layers
)bitwise_labels
= np
.zeros
((num_examples
,num_labels
))for example_index
in range(num_examples
):bitwise_labels
[example_index
][labels
[example_index
][0]] = 1bit_set_cost
= np
.sum(np
.log
(predictions
[bitwise_labels
== 1]))bit_not_set_cost
= np
.sum(np
.log
(1-predictions
[bitwise_labels
== 0]))cost
= (-1/num_examples
) *(bit_set_cost
+bit_not_set_cost
)return cost
@staticmethod def feedforward_propagation(data
,thetas
,layers
): num_layers
= len(layers
)num_examples
= data
.shape
[0]in_layer_activation
= data
for layer_index
in range(num_layers
- 1):theta
= thetas
[layer_index
]out_layer_activation
= sigmoid
(np
.dot
(in_layer_activation
,theta
.T
))out_layer_activation
= np
.hstack
((np
.ones
((num_examples
,1)),out_layer_activation
))in_layer_activation
= out_layer_activation
return in_layer_activation
[:,1:]@staticmethod def thetas_roll(unrolled_thetas
,layers
): num_layers
= len(layers
)thetas
= {}unrolled_shift
= 0for layer_index
in range(num_layers
- 1):in_count
= layers
[layer_index
]out_count
= layers
[layer_index
+1]thetas_width
= in_count
+ 1thetas_height
= out_countthetas_volume
= thetas_width
* thetas_heightstart_index
= unrolled_shiftend_index
= unrolled_shift
+ thetas_volumelayer_theta_unrolled
= unrolled_thetas
[start_index
:end_index
]thetas
[layer_index
] = layer_theta_unrolled
.reshape
((thetas_height
,thetas_width
))unrolled_shift
= unrolled_shift
+thetas_volume
return thetas
數據集訓練:
minist.py:
import numpy
as np
import pandas
as pd
import matplotlib
.pyplot
as plt
import matplotlib
.image
as mping
import math
from multilayer_perceptron
import MultilayerPerceptrondata
= pd
.read_csv
('../neural_network/data/mnist-demo.csv')
numbers_to_display
= 25
num_cells
= math
.ceil
(math
.sqrt
(numbers_to_display
))
plt
.figure
(figsize
=(10,10))
for plot_index
in range(numbers_to_display
):digit
= data
[plot_index
:plot_index
+1].valuesdigit_label
= digit
[0][0]digit_pixels
= digit
[0][1:]image_size
= int(math
.sqrt
(digit_pixels
.shape
[0]))frame
= digit_pixels
.reshape
((image_size
,image_size
))plt
.subplot
(num_cells
,num_cells
,plot_index
+1)plt
.imshow
(frame
,cmap
='Purples')plt
.title
(digit_label
)
plt
.subplots_adjust
(wspace
=0.5,hspace
=0.5)
plt
.show
()train_data
= data
.sample
(frac
= 0.8)
test_data
= data
.drop
(train_data
.index
)train_data
= train_data
.values
test_data
= test_data
.valuesnum_training_examples
= 5000x_train
= train_data
[:num_training_examples
,1:]
y_train
= train_data
[:num_training_examples
,[0]]x_test
= test_data
[:,1:]
y_test
= test_data
[:,[0]]layers
=[784,25,10]normalize_data
= True
max_iterations
= 500
alpha
= 0.1multilayer_perceptron
= MultilayerPerceptron
(x_train
,y_train
,layers
,normalize_data
)
(thetas
,costs
) = multilayer_perceptron
.train
(max_iterations
,alpha
)
plt
.plot
(range(len(costs
)),costs
)
plt
.xlabel
('Grident steps')
plt
.ylabel
('costs')
plt
.show
()y_train_predictions
= multilayer_perceptron
.predict
(x_train
)
y_test_predictions
= multilayer_perceptron
.predict
(x_test
)train_p
= np
.sum(y_train_predictions
== y_train
)/y_train
.shape
[0] * 100
test_p
= np
.sum(y_test_predictions
== y_test
)/y_test
.shape
[0] * 100
print ('訓練集準確率:',train_p
)
print ('測試集準確率:',test_p
)numbers_to_display
= 64num_cells
= math
.ceil
(math
.sqrt
(numbers_to_display
))plt
.figure
(figsize
=(15, 15))for plot_index
in range(numbers_to_display
):digit_label
= y_test
[plot_index
, 0]digit_pixels
= x_test
[plot_index
, :]predicted_label
= y_test_predictions
[plot_index
][0]image_size
= int(math
.sqrt
(digit_pixels
.shape
[0]))frame
= digit_pixels
.reshape
((image_size
, image_size
))color_map
= 'Greens' if predicted_label
== digit_label
else 'Reds'plt
.subplot
(num_cells
, num_cells
, plot_index
+ 1)plt
.imshow
(frame
, cmap
=color_map
)plt
.title
(predicted_label
)plt
.tick_params
(axis
='both', which
='both', bottom
=False, left
=False, labelbottom
=False, labelleft
=False)plt
.subplots_adjust
(hspace
=0.5, wspace
=0.5)
plt
.show
()
結果顯示:
總結
以上是生活随笔為你收集整理的构建神经网络- 手写字体识别案例的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。