日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

构建神经网络- 手写字体识别案例

發(fā)布時(shí)間:2024/1/8 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 构建神经网络- 手写字体识别案例 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

神經(jīng)網(wǎng)絡(luò)構(gòu)建:
Multilayer_Perceptron.py:

import numpy as np from utils.features import prepare_for_training#做歸一化 from utils.hypothesis import sigmoid, sigmoid_gradient#sigmoid函數(shù) 極其導(dǎo)數(shù)class MultilayerPerceptron:#定義初始化函數(shù)def __init__(self,data,labels,layers,normalize_data =False):#數(shù)據(jù)預(yù)處理函數(shù)調(diào)用data_processed = prepare_for_training(data,normalize_data = normalize_data)[0]#初始化賦值操作self.data= data_processedself.labels= labelsself.layers= layers #784 25 10self.normalize_data= normalize_dataself.thetas = MultilayerPerceptron.thetas_init(layers)#權(quán)重參數(shù)初始化#第一層是輸入層,輸入像素點(diǎn)個(gè)數(shù):28*28*1(長(zhǎng) 寬 顏色通道)--不可改#第二層:隱層神經(jīng)元個(gè)數(shù) 25個(gè)(把784特征轉(zhuǎn)化為25維向量) --可改#第三層:分類層,10分類任務(wù)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))#返回最大概率值#定義訓(xùn)練模塊對(duì)參數(shù)進(jìn)行更新,神經(jīng)網(wǎng)絡(luò)也是用優(yōu)化算法去做的(要傳入最大迭代次數(shù)max_iterations 和學(xué)習(xí)率alpha)def train(self,max_iterations=1000,alpha=0.1):#為了方便權(quán)重矩陣參數(shù)進(jìn)行更新,把矩陣?yán)梢粋€(gè)向量,(后面再還原成矩陣)unrolled_theta = MultilayerPerceptron.thetas_unroll(self.thetas)#調(diào)用梯度下降函數(shù)對(duì)參數(shù)進(jìn)行更新 (optimized_theta,cost_history) = MultilayerPerceptron.gradient_descent(self.data,self.labels,unrolled_theta,self.layers,max_iterations,alpha)#還原成矩陣thetas_rollself.thetas = MultilayerPerceptron.thetas_roll(optimized_theta,self.layers)return self.thetas,cost_history#定義權(quán)重矩陣初始化函數(shù)(傳入要更新權(quán)重的層數(shù))@staticmethoddef thetas_init(layers):num_layers = len(layers)#layer是list結(jié)構(gòu)thetas = {}#定義一個(gè)字典 每一層權(quán)重參數(shù)寫里面#用for循環(huán)對(duì)每層權(quán)重參數(shù)進(jìn)行賦值#在此案例中只有3層784 25 10 需要賦值的只有兩層#循環(huán)執(zhí)行兩次得到25*785和10*26兩組矩陣(785 26加上了權(quán)重偏置)for layer_index in range(num_layers - 1):"""會(huì)執(zhí)行兩次,得到兩組參數(shù)矩陣:25*785 , 10*26"""in_count = layers[layer_index]out_count = layers[layer_index+1]# 這里需要考慮到偏置項(xiàng),記住一點(diǎn)偏置的個(gè)數(shù)跟輸出的結(jié)果是一致的thetas[layer_index] = np.random.rand(out_count,in_count+1)*0.05 #隨機(jī)進(jìn)行初始化操作,值盡量小一點(diǎn)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()))#flatten只能適用于numpy對(duì)象,即array或者mat,普通的list列表不適用 a.flatten():a是個(gè)數(shù)組(矩陣),a.flatten()就是把a(bǔ)降到一維,默認(rèn)是按行的方向降 return unrolled_theta#定義梯度下降函數(shù):梯度下降求的是前向和反向傳播的參數(shù)更新#step:計(jì)算loss值 由loss計(jì)算梯度值 梯度值更新 return優(yōu)化完的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)#計(jì)算theta梯度optimized_theta = optimized_theta - alpha* theta_gradient#對(duì)theta進(jìn)行更新return optimized_theta,cost_history#計(jì)算theta梯度函數(shù):@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)#調(diào)用反向傳播函數(shù)thetas_unrolled_gradients = MultilayerPerceptron.thetas_unroll(thetas_rolled_gradients)return thetas_unrolled_gradients#定義反向傳播函數(shù):@staticmethod def back_propagation(data,labels,thetas,layers):num_layers = len(layers)(num_examples,num_features) = data.shape#(1700 875)num_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)) #25*785 10*26for example_index in range(num_examples):layers_inputs = {}layers_activations = {}layers_activation = data[example_index,:].reshape((num_features,1))#785*1layers_activations[0] = layers_activation#逐層計(jì)算for layer_index in range(num_layers - 1):layer_theta = thetas[layer_index] #得到當(dāng)前權(quán)重參數(shù)值 25*785 10*26layer_input = np.dot(layer_theta,layers_activation) #第一次得到25*1 第二次10*1layers_activation = np.vstack((np.array([[1]]),sigmoid(layer_input)))layers_inputs[layer_index + 1] = layer_input #后一層計(jì)算結(jié)果layers_activations[layer_index + 1] = layers_activation #后一層經(jīng)過激活函數(shù)后的結(jié)果output_layer_activation = layers_activation[1:,:]delta = {}#標(biāo)簽處理bitwise_label = np.zeros((num_label_types,1))bitwise_label[labels[example_index][0]] = 1#計(jì)算輸出層和真實(shí)值之間的差異delta[num_layers - 1] = output_layer_activation - bitwise_label#遍歷循環(huán) L L-1 L-2 ...2for 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))#按照公式進(jìn)行計(jì)算delta[layer_index] = np.dot(layer_theta.T,next_delta)*sigmoid_gradient(layer_input)#過濾掉偏置參數(shù)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 #第一次25*785 第二次10*26for layer_index in range(num_layers -1):deltas[layer_index] = deltas[layer_index] * (1/num_examples)return deltas#定義損失函數(shù)@staticmethod def cost_function(data,labels,thetas,layers):num_layers = len(layers)#層數(shù)num_examples = data.shape[0]#樣本個(gè)數(shù)num_labels = layers[-1]#label是layers的最后一層#前向傳播走一次predictions = MultilayerPerceptron.feedforward_propagation(data,thetas,layers)#制作標(biāo)簽,每一個(gè)樣本的標(biāo)簽都得是one-hotbitwise_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#定義前向傳播函數(shù) @staticmethod def feedforward_propagation(data,thetas,layers): num_layers = len(layers)num_examples = data.shape[0]in_layer_activation = data#輸入數(shù)據(jù)# 逐層計(jì)算for layer_index in range(num_layers - 1):theta = thetas[layer_index]out_layer_activation = sigmoid(np.dot(in_layer_activation,theta.T))# 正常計(jì)算完之后是num_examples*25,但是要考慮偏置項(xiàng) 變成num_examples*26out_layer_activation = np.hstack((np.ones((num_examples,1)),out_layer_activation))in_layer_activation = out_layer_activation#返回輸出層結(jié)果,結(jié)果中不要偏置項(xiàng)了return in_layer_activation[:,1:]#定義矩陣還原函數(shù) @staticmethod def thetas_roll(unrolled_thetas,layers): num_layers = len(layers)thetas = {}#指定一個(gè)字典方便索引那一層矩陣unrolled_shift = 0#指定一個(gè)標(biāo)志位 記錄到那一層for 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_volumereturn thetas

數(shù)據(jù)集訓(xùn)練:
minist.py:

import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotlib.image as mping import mathfrom multilayer_perceptron import MultilayerPerceptrondata = pd.read_csv('../neural_network/data/mnist-demo.csv') numbers_to_display = 25#要展示圖像的數(shù)量 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 ('訓(xùn)練集準(zhǔn)確率:',train_p) print ('測(cè)試集準(zhǔn)確率:',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()

結(jié)果顯示:





總結(jié)

以上是生活随笔為你收集整理的构建神经网络- 手写字体识别案例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。