欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python構(gòu)建深度神經(jīng)網(wǎng)絡(luò)(DNN)

 更新時(shí)間:2021年10月11日 11:19:15   作者:Ychan_cc  
這篇文章主要為大家詳細(xì)介紹了python構(gòu)建深度神經(jīng)網(wǎng)絡(luò)DNN,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文學(xué)習(xí)Neural Networks and Deep Learning 在線免費(fèi)書(shū)籍,用python構(gòu)建神經(jīng)網(wǎng)絡(luò)識(shí)別手寫(xiě)體的一個(gè)總結(jié)。

代碼主要包括兩三部分:

1)、數(shù)據(jù)調(diào)用和預(yù)處理

2)、神經(jīng)網(wǎng)絡(luò)類構(gòu)建和方法建立

3)、代碼測(cè)試文件

1)數(shù)據(jù)調(diào)用:

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
# @Time  : 2017-03-12 15:11 
# @Author : CC 
# @File  : net_load_data.py 
# @Software: PyCharm Community Edition 
 
from numpy import * 
import numpy as np 
import cPickle 
def load_data(): 
  """載入解壓后的數(shù)據(jù),并讀取""" 
  with open('data/mnist_pkl/mnist.pkl','rb') as f: 
    try: 
      train_data,validation_data,test_data = cPickle.load(f) 
      print " the file open sucessfully" 
      # print train_data[0].shape #(50000,784) 
      # print train_data[1].shape  #(50000,) 
      return (train_data,validation_data,test_data) 
    except EOFError: 
      print 'the file open error' 
      return None 
 
def data_transform(): 
  """將數(shù)據(jù)轉(zhuǎn)化為計(jì)算格式""" 
  t_d,va_d,te_d = load_data() 
  # print t_d[0].shape # (50000,784) 
  # print te_d[0].shape # (10000,784) 
  # print va_d[0].shape # (10000,784) 
  # n1 = [np.reshape(x,784,1) for x in t_d[0]] # 將5萬(wàn)個(gè)數(shù)據(jù)分別逐個(gè)取出化成(784,1),逐個(gè)排列 
  n = [np.reshape(x, (784, 1)) for x in t_d[0]] # 將5萬(wàn)個(gè)數(shù)據(jù)分別逐個(gè)取出化成(784,1),逐個(gè)排列 
  # print 'n1',n1[0].shape 
  # print 'n',n[0].shape 
  m = [vectors(y) for y in t_d[1]] # 將5萬(wàn)標(biāo)簽(50000,1)化為(10,50000) 
  train_data = zip(n,m) # 將數(shù)據(jù)與標(biāo)簽打包成元組形式 
  n = [np.reshape(x, (784, 1)) for x in va_d[0]] # 將5萬(wàn)個(gè)數(shù)據(jù)分別逐個(gè)取出化成(784,1),排列 
  validation_data = zip(n,va_d[1])  # 沒(méi)有將標(biāo)簽數(shù)據(jù)矢量化 
  n = [np.reshape(x, (784, 1)) for x in te_d[0]] # 將5萬(wàn)個(gè)數(shù)據(jù)分別逐個(gè)取出化成(784,1),排列 
  test_data = zip(n, te_d[1]) # 沒(méi)有將標(biāo)簽數(shù)據(jù)矢量化 
  # print train_data[0][0].shape #(784,) 
  # print "len(train_data[0])",len(train_data[0]) #2 
  # print "len(train_data[100])",len(train_data[100]) #2 
  # print "len(train_data[0][0])", len(train_data[0][0]) #784 
  # print "train_data[0][0].shape", train_data[0][0].shape #(784,1) 
  # print "len(train_data)", len(train_data) #50000 
  # print train_data[0][1].shape #(10,1) 
  # print test_data[0][1] # 7 
  return (train_data,validation_data,test_data) 
def vectors(y): 
  """賦予標(biāo)簽""" 
  label = np.zeros((10,1)) 
  label[y] = 1.0 #浮點(diǎn)計(jì)算 
  return label 

2)網(wǎng)絡(luò)構(gòu)建

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
# @Time  : 2017-03-12 16:07 
# @Author : CC 
# @File  : net_network.py 
 
import numpy as np 
import random 
class Network(object):  #默認(rèn)為基類?用于繼承:print isinstance(network,object) 
  def __init__(self,sizes): 
    self.num_layers = len(sizes) 
    self.sizes = sizes 
    # print 'num_layers', self.num_layers 
    self.weight = [np.random.randn(a1, a2) for (a1, a2) in zip(sizes[1:], sizes[:-1])] #產(chǎn)生一個(gè)個(gè)數(shù)組 
    self.bias = [np.random.randn(a3,1) for a3 in sizes[1:]] 
    # print self.weight[0].shape #(20,10) 
 
  def SGD(self,train_data,min_batch_size,epoches,eta,test_data=False): 
    """ 1) 打亂樣本,將訓(xùn)練數(shù)據(jù)劃分成小批次 
      2)計(jì)算出反向傳播梯度 
      3) 獲得權(quán)重更新""" 
    if test_data: n_test = len(test_data) 
    n = len(train_data)  #50000 
    random.shuffle(train_data) # 打亂 
    min_batches = [train_data[k:k+min_batch_size] for k in xrange(0,n,min_batch_size)] #提取批次數(shù)據(jù) 
    for k in xrange(0,epoches):  #利用更新后的權(quán)值繼續(xù)更新 
      random.shuffle(train_data) # 打亂 
      for min_batch in min_batches: #逐個(gè)傳入,效率很低 
        self.updata_parameter(min_batch,eta) 
      if test_data: 
        num = self.evaluate(test_data) 
        print "the {0}th epoches: {1}/{2}".format(k,num,len(test_data)) 
      else: 
        print 'epoches {0} completed'.format(k) 
 
  def forward(self,x): 
    """獲得各層激活值""" 
    for w,b in zip(self.weight,self.bias): 
      x = sigmoid(np.dot(w, x)+b) 
    return x 
 
  def updata_parameter(self,min_batch,eta): 
    """1) 反向傳播計(jì)算每個(gè)樣本梯度值 
      2) 累加每個(gè)批次樣本的梯度值 
      3) 權(quán)值更新""" 
    ndeltab = [np.zeros(b.shape) for b in self.bias] 
    ndeltaw = [np.zeros(w.shape) for w in self.weight] 
    for x,y in min_batch: 
      deltab,deltaw = self.backprop(x,y) 
      ndeltab = [nb +db for nb,db in zip(ndeltab,deltab)] 
      ndeltaw = [nw + dw for nw,dw in zip(ndeltaw,deltaw)] 
    self.bias = [b - eta * ndb/len(min_batch) for ndb,b in zip(ndeltab,self.bias)] 
    self.weight = [w - eta * ndw/len(min_batch) for ndw,w in zip(ndeltaw,self.weight)] 
 
 
  def backprop(self,x,y): 
    """執(zhí)行前向計(jì)算,再進(jìn)行反向傳播,返回deltaw,deltab""" 
    # [w for w in self.weight] 
    # print 'len',len(w) 
    # print "self.weight",self.weight[0].shape 
    # print w[0].shape 
    # print w[1].shape 
    # print w.shape 
    activation = x 
    activations = [x] 
    zs = [] 
    # feedforward 
    for w, b in zip(self.weight, self.bias): 
      # print w.shape,activation.shape,b.shape 
      z = np.dot(w, activation) +b 
      zs.append(z)  #用于計(jì)算f(z)導(dǎo)數(shù) 
      activation = sigmoid(z) 
      # print 'activation',activation.shape 
      activations.append(activation) # 每層的輸出結(jié)果 
    delta = self.top_subtract(activations[-1],y) * dsigmoid(zs[-1]) #最后一層的delta,np.array乘,相同維度乘 
    deltaw = [np.zeros(w1.shape) for w1 in self.weight] #每一次將獲得的值作為列表形式賦給deltaw 
    deltab = [np.zeros(b1.shape) for b1 in self.bias] 
    # print 'deltab[0]',deltab[-1].shape 
    deltab[-1] = delta 
    deltaw[-1] = np.dot(delta,activations[-2].transpose()) 
    for k in xrange(2,self.num_layers): 
      delta = np.dot(self.weight[-k+1].transpose(),delta) * dsigmoid(zs[-k]) 
      deltab[-k] = delta 
      deltaw[-k] = np.dot(delta,activations[-k-1].transpose()) 
    return (deltab,deltaw) 
 
  def evaluate(self,test_data): 
    """評(píng)估驗(yàn)證集和測(cè)試集的精度,標(biāo)簽直接一個(gè)數(shù)作為比較""" 
    z = [(np.argmax(self.forward(x)),y) for x,y in test_data] 
    zs = np.sum(int(a == b) for a,b in z) 
    # zk = sum(int(a == b) for a,b in z) 
    # print "zs/zk:",zs,zk 
    return zs 
 
  def top_subtract(self,x,y): 
    return (x - y) 
 
def sigmoid(x): 
  return 1.0/(1.0+np.exp(-x)) 
 
def dsigmoid(x): 
  z = sigmoid(x) 
  return z*(1-z) 

3)網(wǎng)絡(luò)測(cè)試

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
# @Time  : 2017-03-12 15:24 
# @Author : CC 
# @File  : net_test.py 
 
import net_load_data 
# net_load_data.load_data() 
train_data,validation_data,test_data = net_load_data.data_transform() 
 
import net_network as net 
net1 = net.Network([784,30,10]) 
min_batch_size = 10 
eta = 3.0 
epoches = 30 
net1.SGD(train_data,min_batch_size,epoches,eta,test_data) 
print "complete" 

4)結(jié)果

the 9th epoches: 9405/10000 
the 10th epoches: 9420/10000 
the 11th epoches: 9385/10000 
the 12th epoches: 9404/10000 
the 13th epoches: 9398/10000 
the 14th epoches: 9406/10000 
the 15th epoches: 9396/10000 
the 16th epoches: 9413/10000 
the 17th epoches: 9405/10000 
the 18th epoches: 9425/10000 
the 19th epoches: 9420/10000 

總體來(lái)說(shuō)這本書(shū)的實(shí)例,用來(lái)熟悉python和神經(jīng)網(wǎng)絡(luò)非常好。

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

相關(guān)文章

  • Python使用Matplotlib實(shí)現(xiàn)雨點(diǎn)圖動(dòng)畫(huà)效果的方法

    Python使用Matplotlib實(shí)現(xiàn)雨點(diǎn)圖動(dòng)畫(huà)效果的方法

    這篇文章主要介紹了Python使用Matplotlib實(shí)現(xiàn)雨點(diǎn)圖動(dòng)畫(huà)效果的方法,結(jié)合實(shí)例形式分析了win10安裝ffmpeg及animation函數(shù)的使用相關(guān)操作技巧,需要的朋友可以參考下
    2017-12-12
  • Python爬取網(wǎng)頁(yè)中的圖片(搜狗圖片)詳解

    Python爬取網(wǎng)頁(yè)中的圖片(搜狗圖片)詳解

    沒(méi)想到python是如此強(qiáng)大,令人著迷,以前看見(jiàn)圖片總是一張一張復(fù)制粘貼,現(xiàn)在好了,學(xué)會(huì)python就可以用程序?qū)⒁粡垙垐D片,保存下來(lái)。下面這篇文章主要給大家介紹了利用Python3.6爬取搜狗圖片網(wǎng)頁(yè)中圖片的相關(guān)資料,需要的朋友可以參考下。
    2017-03-03
  • python matplotlib包圖像配色方案分享

    python matplotlib包圖像配色方案分享

    這篇文章主要介紹了python matplotlib包圖像配色方案分享,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • Python中常見(jiàn)的矩陣運(yùn)算詳解

    Python中常見(jiàn)的矩陣運(yùn)算詳解

    這篇文章主要介紹了Python中常見(jiàn)的矩陣運(yùn)算詳解,所謂的數(shù)據(jù)處理,其本質(zhì)大都可以歸為矩陣運(yùn)算,因?yàn)樾枰幚淼臄?shù)據(jù)大都是矩陣或向量的形式,一個(gè)工具適不適合做數(shù)據(jù)處理,一個(gè)重要的指標(biāo)的就是支不支持矩陣運(yùn)算,需要的朋友可以參考下
    2023-08-08
  • python爬取企查查企業(yè)信息之selenium自動(dòng)模擬登錄企查查

    python爬取企查查企業(yè)信息之selenium自動(dòng)模擬登錄企查查

    這篇文章主要介紹了python爬取企查查企業(yè)信息之自動(dòng)模擬登錄企查查以及selenium獲取headers,selenium獲取cookie,需要的朋友可以參考下
    2021-04-04
  • python實(shí)現(xiàn)簡(jiǎn)單遺傳算法

    python實(shí)現(xiàn)簡(jiǎn)單遺傳算法

    這篇文章主要介紹了python如何實(shí)現(xiàn)簡(jiǎn)單遺傳算法,幫助大家更好的利用python進(jìn)行數(shù)據(jù)分析,感興趣的朋友可以了解下
    2020-09-09
  • python ctypes庫(kù)2_指定參數(shù)類型和返回類型詳解

    python ctypes庫(kù)2_指定參數(shù)類型和返回類型詳解

    今天小編就為大家分享一篇python ctypes庫(kù)2_指定參數(shù)類型和返回類型詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • 如何利用Playwright庫(kù)進(jìn)行電影網(wǎng)站數(shù)據(jù)的獲取

    如何利用Playwright庫(kù)進(jìn)行電影網(wǎng)站數(shù)據(jù)的獲取

    playwright庫(kù)是微軟開(kāi)源的一個(gè)庫(kù),這個(gè)庫(kù)的功能更加的強(qiáng)大,除了可以實(shí)現(xiàn)同步操作,同樣也可以實(shí)現(xiàn)異步的操作,這篇文章主要介紹了如何利用Playwright庫(kù)進(jìn)行電影網(wǎng)站數(shù)據(jù)的獲取,需要的朋友可以參考下
    2023-05-05
  • Python Selenium中等待設(shè)置的實(shí)現(xiàn)

    Python Selenium中等待設(shè)置的實(shí)現(xiàn)

    本文主要介紹了Python Selenium中等待設(shè)置的實(shí)現(xiàn),過(guò)詳實(shí)的示例代碼,深入介紹了顯式等待、隱式等待、自定義等待條件、多重等待條件、頁(yè)面加載狀態(tài)的等待、元素存在與可見(jiàn)性等待、Fluent等待以及異步JavaScript加載的等待,感興趣的可以了解一下
    2023-12-12
  • Python網(wǎng)絡(luò)爬蟲(chóng)之Web網(wǎng)頁(yè)基礎(chǔ)

    Python網(wǎng)絡(luò)爬蟲(chóng)之Web網(wǎng)頁(yè)基礎(chǔ)

    我們?cè)趯W(xué)習(xí)爬蟲(chóng)之前,要先了解網(wǎng)頁(yè)的組成,只有我們了解其組成嗎,才可以方能百戰(zhàn)百勝,文章中有詳細(xì)的代碼示例,需要的朋友可以參考一下
    2023-04-04

最新評(píng)論