最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python神經(jīng)網(wǎng)絡(luò)編程實現(xiàn)手寫數(shù)字識別

 更新時間:2020年05月27日 11:39:25   作者:wenmiao_  
這篇文章主要為大家詳細(xì)介紹了python神經(jīng)網(wǎng)絡(luò)編程實現(xiàn)手寫數(shù)字識別,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了python實現(xiàn)手寫數(shù)字識別的具體代碼,供大家參考,具體內(nèi)容如下

import numpy
import scipy.special
#import matplotlib.pyplot
 
class neuralNetwork:
  def __init__(self,inputnodes,hiddennodes,outputnodes,learningrate):
    self.inodes=inputnodes
    self.hnodes=hiddennodes
    self.onodes=outputnodes
    
    self.lr=learningrate
  
    self.wih=numpy.random.normal(0.0,pow(self.hnodes,-0.5),(self.hnodes,self.inodes))
    self.who=numpy.random.normal(0.0,pow(self.onodes,-0.5),(self.onodes,self.hnodes))
    
    self.activation_function=lambda x: scipy.special.expit(x)
    pass
  
  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.wih,inputs)
    hidden_outputs=self.activation_function(hidden_inputs)
    
    final_inputs=numpy.dot(self.who,hidden_outputs)
    final_outputs=self.activation_function(final_inputs)
    
    output_errors=targets-final_outputs
    hidden_errors=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))
    pass
  
  def query(self,input_list):
    inputs=numpy.array(input_list,ndmin=2).T
    
    hidden_inputs=numpy.dot(self.wih,inputs)
    hidden_outputs=self.activation_function(hidden_inputs)
    
    final_inputs=numpy.dot(self.who,hidden_outputs)
    final_outputs=self.activation_function(final_inputs)
    
    return final_outputs
 
 
input_nodes=784
hidden_nodes=100
output_nodes=10
learning_rate=0.1
n=neuralNetwork(input_nodes,hidden_nodes,output_nodes,learning_rate)
 
training_data_file=open(r"C:\Users\lsy\Desktop\nn\mnist_train.csv","r")
training_data_list=training_data_file.readlines()
training_data_file.close()
#print(n.wih)
#print("")
epochs=2
for e in range(epochs):
  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(output_nodes)+0.01
    targets[int(all_values[0])]=0.99
    n.train(inputs,targets)
  
#print(n.wih)
#print(len(training_data_list))
#for i in training_data_list:
#  print(i)
 
test_data_file=open(r"C:\Users\lsy\Desktop\nn\mnist_test.csv","r")
test_data_list=test_data_file.readlines()
test_data_file.close()
 
scorecard=[]
 
 
for record in test_data_list:
  all_values=record.split(",")
  correct_lable=int(all_values[0])
  inputs=(numpy.asfarray(all_values[1:])/255.0*0.99)+0.01
  outputs=n.query(inputs)
  label=numpy.argmax(outputs)
  if(label==correct_lable):
    scorecard.append(1)
  else:
    scorecard.append(0)
 
scorecard_array=numpy.asarray(scorecard)
print(scorecard_array)
print("")
print(scorecard_array.sum()/scorecard_array.size)
#all_value=test_data_list[0].split(",")
#input=(numpy.asfarray(all_value[1:])/255.0*0.99)+0.01
#print(all_value[0])
 
#image_array=numpy.asfarray(all_value[1:]).reshape((28,28))
 
#matplotlib.pyplot.imshow(image_array,cmap="Greys",interpolation="None")
#matplotlib.pyplot.show()
#nn=n.query((numpy.asfarray(all_value[1:])/255.0*0.99)+0.01)
#for i in nn :
#  print(i)

《python神經(jīng)網(wǎng)絡(luò)編程》中代碼,僅做記錄,以備后用。 

image_file_name=r"*.JPG"
img_array=scipy.misc.imread(image_file_name,flatten=True)
 
img_data=255.0-img_array.reshape(784)
image_data=(img_data/255.0*0.99)+0.01

圖片對應(yīng)像素的讀取。因訓(xùn)練集灰度值與實際相反,故用255減取反。 

import numpy
import scipy.special
#import matplotlib.pyplot
import scipy.misc
from PIL import Image
class neuralNetwork:
  def __init__(self,inputnodes,hiddennodes,outputnodes,learningrate):
    self.inodes=inputnodes
    self.hnodes=hiddennodes
    self.onodes=outputnodes
    
    self.lr=learningrate
  
    self.wih=numpy.random.normal(0.0,pow(self.hnodes,-0.5),(self.hnodes,self.inodes))
    self.who=numpy.random.normal(0.0,pow(self.onodes,-0.5),(self.onodes,self.hnodes))
    
    self.activation_function=lambda x: scipy.special.expit(x)
    pass
  
  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.wih,inputs)
    hidden_outputs=self.activation_function(hidden_inputs)
    
    final_inputs=numpy.dot(self.who,hidden_outputs)
    final_outputs=self.activation_function(final_inputs)
    
    output_errors=targets-final_outputs
    hidden_errors=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))
    pass
  
  def query(self,input_list):
    inputs=numpy.array(input_list,ndmin=2).T
    
    hidden_inputs=numpy.dot(self.wih,inputs)
    hidden_outputs=self.activation_function(hidden_inputs)
    
    final_inputs=numpy.dot(self.who,hidden_outputs)
    final_outputs=self.activation_function(final_inputs)
    
    return final_outputs
 
 
input_nodes=784
hidden_nodes=100
output_nodes=10
learning_rate=0.1
n=neuralNetwork(input_nodes,hidden_nodes,output_nodes,learning_rate)
 
training_data_file=open(r"C:\Users\lsy\Desktop\nn\mnist_train.csv","r")
training_data_list=training_data_file.readlines()
training_data_file.close()
#print(n.wih)
#print("")
 
#epochs=2
#for e in range(epochs):
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(output_nodes)+0.01
  targets[int(all_values[0])]=0.99
  n.train(inputs,targets)
 
#image_file_name=r"C:\Users\lsy\Desktop\nn\1000-1.JPG"
'''
img_array=scipy.misc.imread(image_file_name,flatten=True)
img_data=255.0-img_array.reshape(784)
image_data=(img_data/255.0*0.99)+0.01
#inputs=(numpy.asfarray(image_data)/255.0*0.99)+0.01
outputs=n.query(image_data)
label=numpy.argmax(outputs)
print(label)
'''
#print(n.wih)
#print(len(training_data_list))
#for i in training_data_list:
#  print(i)
 
test_data_file=open(r"C:\Users\lsy\Desktop\nn\mnist_test.csv","r")
 
test_data_list=test_data_file.readlines()
test_data_file.close()
 
scorecard=[]
 
total=[0,0,0,0,0,0,0,0,0,0]
rightsum=[0,0,0,0,0,0,0,0,0,0]
 
for record in test_data_list:
  all_values=record.split(",")
  correct_lable=int(all_values[0])
  inputs=(numpy.asfarray(all_values[1:])/255.0*0.99)+0.01
  outputs=n.query(inputs)
  label=numpy.argmax(outputs)
  total[correct_lable]+=1
  if(label==correct_lable):
    scorecard.append(1)
    rightsum[correct_lable]+=1
  else:
    scorecard.append(0)
 
scorecard_array=numpy.asarray(scorecard)
print(scorecard_array)
print("")
print(scorecard_array.sum()/scorecard_array.size)
print("")
print(total)
print(rightsum)
for i in range(10):
  print((rightsum[i]*1.0)/total[i])
 
#all_value=test_data_list[0].split(",")
#input=(numpy.asfarray(all_value[1:])/255.0*0.99)+0.01
#print(all_value[0])
 
#image_array=numpy.asfarray(all_value[1:]).reshape((28,28))
 
#matplotlib.pyplot.imshow(image_array,cmap="Greys",interpolation="None")
#matplotlib.pyplot.show()
#nn=n.query((numpy.asfarray(all_value[1:])/255.0*0.99)+0.01)
#for i in nn :
#  print(i)

嘗試統(tǒng)計了對于各個數(shù)據(jù)測試數(shù)量及正確率。

原本想驗證書后向后查詢中數(shù)字‘9'識別模糊是因為訓(xùn)練數(shù)量不足或錯誤率過高而產(chǎn)生,然最終結(jié)果并不支持此猜想。

另書中只能針對特定像素的圖片進(jìn)行學(xué)習(xí),真正手寫的圖片并不能滿足訓(xùn)練條件,實際用處仍需今后有時間改進(jìn)。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 從零學(xué)Python之入門(五)縮進(jìn)和選擇

    從零學(xué)Python之入門(五)縮進(jìn)和選擇

    空白在Python中是重要的。事實上行首的空白是重要的。它稱為縮進(jìn)。在邏輯行首的空白(空格和制表符)用來決定邏輯行的縮進(jìn)層次,從而用來決定語句的分組。
    2014-05-05
  • 使用DrissionPage控制360瀏覽器的完美解決方案

    使用DrissionPage控制360瀏覽器的完美解決方案

    在網(wǎng)頁自動化領(lǐng)域,經(jīng)常遇到需要保持登錄狀態(tài)、保留Cookie等場景,今天要分享的方案可以完美解決這個問題:使用DrissionPage直接調(diào)用本地360瀏覽器的用戶數(shù)據(jù),實現(xiàn)無縫自動化控制,需要的朋友可以參考下
    2025-03-03
  • Python?Concurrent?Futures解鎖并行化編程的魔法示例

    Python?Concurrent?Futures解鎖并行化編程的魔法示例

    Python的concurrent.futures模塊為并行化編程提供了強(qiáng)大的工具,使得開發(fā)者能夠輕松地利用多核心和異步執(zhí)行的能力,本文將深入探討concurrent.futures的各個方面,從基礎(chǔ)概念到高級用法,為讀者提供全面的了解和實用的示例代碼
    2023-12-12
  • Python如何爬取qq音樂歌詞到本地

    Python如何爬取qq音樂歌詞到本地

    這篇文章主要介紹了Python如何爬取qq音樂歌詞到本地,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • python中的np.argmax() 返回最大值索引號

    python中的np.argmax() 返回最大值索引號

    這篇文章主要介紹了python中的np.argmax() 返回最大值索引號操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Python采集大學(xué)教務(wù)系統(tǒng)成績單實戰(zhàn)示例

    Python采集大學(xué)教務(wù)系統(tǒng)成績單實戰(zhàn)示例

    這篇文章主要為大家介紹了Python采集大學(xué)教務(wù)系統(tǒng)成績單實戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • 用python實現(xiàn)爬取奧特曼圖片實例

    用python實現(xiàn)爬取奧特曼圖片實例

    大家好,本篇文章主要講的是用python實現(xiàn)爬取奧特曼圖片實例,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-02-02
  • Python函數(shù)中apply、map、applymap的區(qū)別

    Python函數(shù)中apply、map、applymap的區(qū)別

    這篇文章主要介紹了 Python函數(shù)中apply、map、applymap的區(qū)別 ,文章圍繞 Python函數(shù)中apply、map、applymap的相關(guān)資料展開詳細(xì)內(nèi)容,需要的朋友可以參考一下
    2021-11-11
  • Django把SQLite數(shù)據(jù)庫轉(zhuǎn)換為Mysql數(shù)據(jù)庫的過程

    Django把SQLite數(shù)據(jù)庫轉(zhuǎn)換為Mysql數(shù)據(jù)庫的過程

    之前我們默認(rèn)使用的是SQLite數(shù)據(jù)庫,我們開發(fā)完成之后,里面有許多數(shù)據(jù),如果我們想轉(zhuǎn)換成Mysql數(shù)據(jù)庫,那我們先得把舊數(shù)據(jù)從SQLite導(dǎo)出,然后再導(dǎo)入到新的Mysql數(shù)據(jù)庫里去,這篇文章主要介紹了Django如何把SQLite數(shù)據(jù)庫轉(zhuǎn)換為Mysql數(shù)據(jù)庫,需要的朋友可以參考下
    2023-05-05
  • Python網(wǎng)絡(luò)爬蟲神器PyQuery的基本使用教程

    Python網(wǎng)絡(luò)爬蟲神器PyQuery的基本使用教程

    這篇文章主要給大家介紹了關(guān)于Python網(wǎng)絡(luò)爬蟲神器PyQuery的基本使用教程,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)使用PyQuery具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-02-02

最新評論

什邡市| 普兰店市| 治县。| 祁门县| 西畴县| 苏尼特左旗| 青河县| 平罗县| 宝山区| 宝山区| 乌鲁木齐县| 抚远县| 景宁| 白水县| 营山县| 昌乐县| 株洲县| 铁岭县| 昌吉市| 都匀市| 闽侯县| 白城市| 台东市| 沐川县| 乌拉特中旗| 申扎县| 藁城市| 乐陵市| 耿马| 芮城县| 罗江县| 铜梁县| 平陆县| 锡林郭勒盟| 盐城市| 溆浦县| 江油市| 济南市| 鄱阳县| 炎陵县| 胶南市|