神經網路實現手寫數字識別

2021-10-16 06:04:51 字數 3032 閱讀 9120

簡單實現3層神經網路識別手寫數字

import numpy

import matplotlib.pyplot as plt

import scipy.special

class network: # 定義神經網路類

definit(self, inputnodes, hiddennodes, outputnodes, learningrate):

self.inodes = inputnodes

self.hnodes = hiddennodes

self.onodes = outputnodes#這段**定義這個淺層神經網路類的輸入層,隱藏層,輸出層的層數

self.i_to_h = numpy.random.normal(0.0, pow(self.hnodes, -0.5), (self.hnodes, self.inodes))

self.h_to_o = numpy.random.normal(0.0, pow(self.onodes, -0.5), (self.onodes, self.hnodes))

self.learningrate = learningrate#定義學習率

self.function = lambda x: scipy.special.expit(x)#用lambda表示式定義啟用函式

def train(self, inputs_list, targets_list):#這個函式用於訓練神經網路的權重

inputs = numpy.array(inputs_list, ndmin=2).t#神經網路的測試集資料

targets = numpy.array(targets_list, ndmin=2).t#測試集的標籤集

hidden_inputs = numpy.dot(self.i_to_h, inputs)

hidden_outputs = self.function(hidden_inputs)#這兩條**表示測試集的資料通過全連線網路,乘以對應權重否對映到隱藏層節點,然後執行啟用函式

final_inputs = numpy.dot(self.h_to_o, hidden_outputs)

final_outputs = self.function(final_inputs)#這兩條**表示測試集的資料通過全連線網路,乘以對應權重否對映到輸出層節點,然後執行啟用函式

output_error = targets - final_outputs#神經網路此次前饋得到的結果與標籤對比產生的誤差

hidden_error = numpy.dot(self.h_to_o.t, output_error)#誤差的反向傳播

self.h_to_o += self.learningrate * numpy.dot((output_error * final_outputs * (1.0 - final_outputs)),

numpy.transpose(hidden_outputs))

self.i_to_h += self.learningrate * numpy.dot((hidden_error * hidden_outputs * (1.0 - hidden_outputs)),

numpy.transpose(inputs))#定義損失函式的求導結果

def query(self, inputs_list):#查詢某個輸入的概率**向量

inputs = numpy.array(inputs_list, ndmin=2).t

hidden_inputs = numpy.dot(self.i_to_h, inputs)

hidden_outputs = self.function(hidden_inputs)

final_inputs = numpy.dot(self.h_to_o, hidden_outputs)

final_outputs = self.function(final_inputs)

return final_outputs

training_data_file = open(「minist.csv/mnist_train.csv」, 『r』)

training_data_list = training_data_file.readlines()

training_data_file.close()

n = network(784, 200, 10, 0.1)

test_data_file = open(「minist.csv/mnist_test.csv」, 『r』)

test_data_list = test_data_file.readlines()

test_data_file.close()

times = 5

for e in range(times):

for record in training_data_list:

all_values = record.split(",")

inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01

targets = numpy.zeros(10) + 0.01

targets[int(all_values[0])] = 0.99

n.train(inputs, targets)

all_value = test_data_list[700].split(』,』)

imagearray = numpy.asfarray(all_value[1:]).reshape((28, 28))

plt.imshow(imagearray, cmap=『greys』, interpolation=『none』)

plt.show()

inputs = (numpy.asfarray(all_value[1:]) / 255.0 * 0.99) + 0.01

output = n.query(inputs)

label = numpy.argmax(output)

print(「經過程式識別後,該手寫數字是」)

print(label)

使用神經網路識別手寫數字

神經網路和深度學習為影象識別 語音識別 自然語言處理等問題提供了目前最好的解決方案。本書主要會介紹神經網路和深度學習背後關鍵的概念。更多關於本書的細節,請參考這裡。或者您可以直接從第一章開始學習。本專案是neural networks and deep learning的中文翻譯,原文作者 mich...

使用神經網路識別手寫數字

最近在看michael nielsen的 neural network and deep learning 嘗試復現書中的 但這本書的 貌似用的python2.0,所以在執行的時候,報了好多錯誤,在這裡進行記錄一下。1 載入mnist資料集出錯 unicodedecodeerror ascii co...

簡單的神經網路識別手寫數字

為了測試,我就先在紙上手寫了一些 工整 的數字,並用 特殊手段 將進行了處理,得到了幾個用於測試手寫數字識別率的 標準的大小為28 28,確保輸入層的節點數為784,好吧,我承認是懶所以只寫了五個 開始測試資料 負責一維陣列的引數 k 0 製作乙個一維陣列負責儲存二維陣列扁平化以後的資料 input...