国产精品久久久aaaa,日日干夜夜操天天插,亚洲乱熟女香蕉一区二区三区少妇,99精品国产高清一区二区三区,国产成人精品一区二区色戒,久久久国产精品成人免费,亚洲精品毛片久久久久,99久久婷婷国产综合精品电影,国产一区二区三区任你鲁

0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

【連載】深度學習筆記11:利用numpy搭建一個卷積神經網絡

人工智能實訓營 ? 2018-10-30 18:50 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

前兩個筆記筆者集中探討了卷積神經網絡中的卷積原理,對于二維卷積和三維卷積的原理進行了深入的剖析,對 CNN 的卷積、池化、全連接、濾波器、感受野等關鍵概念進行了充分的理解。本節內容將繼續秉承之前 DNN 的學習路線,在利用 Tensorflow 搭建神經網絡之前,先嘗試利用 numpy 手動搭建卷積神經網絡,以期對卷積神經網絡的卷積機制、前向傳播和反向傳播的原理和過程有更深刻的理解。

單步卷積過程

在正式搭建 CNN 之前,我們先依據前面筆記提到的卷積機制的線性計算的理解,利用 numpy 定義一個單步卷積過程。代碼如下:

def conv_single_step(a_slice_prev, W, b):
  s = a_slice_prev * W  # Sum over all entries of the volume s.
  Z = np.sum(s)  # Add bias b to Z. Cast b to a float() so that Z results in a scalar value.
  Z = float(Z + b)  
return Z

在上述的單步卷積定義中,我們傳入了一個前一層輸入的要進行卷積的區域,即感受野 a_slice_prev ,濾波器 W,即卷積層的權重參數,偏差 b,對其執行 Z=Wx+b 的線性計算即可實現一個單步的卷積過程。

CNN前向傳播過程:卷積

正如 DNN 中一樣,CNN 即使多了卷積和池化過程,模型仍然是前向傳播和反向傳播的訓練過程。CNN 的前向傳播包括卷積和池化兩個過程,我們先來看如何利用 numpy 基于上面定義的單步卷積實現完整的卷積過程。卷積計算并不難,我們在單步卷積中就已經實現了,難點在于如何實現濾波器在輸入圖像矩陣上的的掃描和移動過程。


這其中我們需要搞清楚一些變量和參數,以及每一個輸入輸出的 shape,這對于我們執行卷積和矩陣相乘至關重要。首先我們的輸入是原始圖像矩陣,也可以是前一層經過激活后的圖像輸出矩陣,這里以前一層的激活輸出為準,輸入像素的 shape 我們必須明確,然后是濾波器矩陣和偏差,還需要考慮步幅和填充,在此基礎上我們基于濾波器移動和單步卷積搭建定義如下前向卷積過程:

def conv_forward(A_prev, W, b, hparameters):  
""" Arguments: A_prev -- output activations of the previous layer, numpy array of shape (m, n_H_prev, n_W_prev, n_C_prev) W -- Weights, numpy array of shape (f, f, n_C_prev, n_C) b -- Biases, numpy array of shape (1, 1, 1, n_C) hparameters -- python dictionary containing "stride" and "pad" Returns: Z -- conv output, numpy array of shape (m, n_H, n_W, n_C) cache -- cache of values needed for the conv_backward() function """ # 前一層輸入的shape (m, n_H_prev, n_W_prev, n_C_prev) = A_prev.shape

# 濾波器權重的shape (f, f, n_C_prev, n_C) = W.shape
# 步幅參數 stride = hparameters['stride']
# 填充參數 pad = hparameters['pad']
# 計算輸出圖像的高寬 n_H = int((n_H_prev + 2 * pad - f) / stride + 1) n_W = int((n_W_prev + 2 * pad - f) / stride + 1)
# 初始化輸出 Z = np.zeros((m, n_H, n_W, n_C))
# 對輸入執行邊緣填充 A_prev_pad = zero_pad(A_prev, pad)
for i in range(m): a_prev_pad = A_prev_pad[i, :, :, :] for h in range(n_H): for w in range(n_W): for c in range(n_C): # 濾波器在輸入圖像上掃描 vert_start = h * stride vert_end = vert_start + f horiz_start = w * stride horiz_end = horiz_start + f
# 定義感受野 a_slice_prev = a_prev_pad[vert_start : vert_end, horiz_start : horiz_end, :] # 對感受野執行單步卷積 Z[i, h, w, c] = conv_single_step(a_slice_prev, W[:,:,:,c], b[:,:,:,c])
assert(Z.shape == (m, n_H, n_W, n_C)) cache = (A_prev, W, b, hparameters)
return Z, cache

這樣,卷積神經網絡前向傳播中一個完整的卷積計算過程就被我們定義好了。通常而言,我們也會對卷積后輸出加一個 relu 激活操作,正如前面的圖2所示,這里我們就省略不加了。

CNN前向傳播過程:池化

池化簡單而言就是取局部區域最大值,池化的前向傳播跟卷積過程類似,但相對簡單一點,無需執行單步卷積那樣的乘積運算。同樣需要注意的是各參數和輸入輸出的 shape,因此我們定義如下前向傳播池化過程:

def pool_forward(A_prev, hparameters, mode = "max"):  
""" Arguments: A_prev -- Input data, numpy array of shape (m, n_H_prev, n_W_prev, n_C_prev) hparameters -- python dictionary containing "f" and "stride" mode -- the pooling mode you would like to use, defined as a string ("max" or "average") Returns: A -- output of the pool layer, a numpy array of shape (m, n_H, n_W, n_C) cache -- cache used in the backward pass of the pooling layer, contains the input and hparameters """ # 前一層輸入的shape (m, n_H_prev, n_W_prev, n_C_prev) = A_prev.shape
# 步幅和權重參數 f = hparameters["f"] stride = hparameters["stride"]
# 計算輸出圖像的高寬 n_H = int(1 + (n_H_prev - f) / stride) n_W = int(1 + (n_W_prev - f) / stride) n_C = n_C_prev
# 初始化輸出 A = np.zeros((m, n_H, n_W, n_C)) for i in range(m): for h in range(n_H): for w in range(n_W): for c in range (n_C): # 樹池在輸入圖像上掃描 vert_start = h * stride vert_end = vert_start + f horiz_start = w * stride horiz_end = horiz_start + f
# 定義池化區域 a_prev_slice = A_prev[i, vert_start:vert_end, horiz_start:horiz_end, c]
# 選擇池化類型 if mode == "max": A[i, h, w, c] = np.max(a_prev_slice)
elif mode == "average": A[i, h, w, c] = np.mean(a_prev_slice) cache = (A_prev, hparameters)
assert(A.shape == (m, n_H, n_W, n_C))
return A, cache

由上述代碼結構可以看出,前向傳播的池化過程的代碼結構和卷積過程非常類似。

CNN反向傳播過程:卷積

定義好前向傳播之后,難點和關鍵點就在于如何給卷積和池化過程定義反向傳播過程。卷積層的反向傳播向來是個復雜的過程,Tensorflow 中我們只要定義好前向傳播過程,反向傳播會自動進行計算。但利用 numpy 搭建 CNN 反向傳播就還得我們自己定義了。其關鍵還是在于準確的定義損失函數對于各個變量的梯度:
640?wx_fmt=png

640?wx_fmt=png

640?wx_fmt=png
由上述梯度計算公式和卷積的前向傳播過程,我們定義如下卷積的反向傳播函數:

def conv_backward(dZ, cache):  """
  Arguments:
  dZ -- gradient of the cost with respect to the output of the conv layer (Z), numpy array of shape (m, n_H, n_W, n_C)
  cache -- cache of values needed for the conv_backward(), output of conv_forward()

  Returns:
  dA_prev -- gradient of the cost with respect to the input of the conv layer (A_prev),
        numpy array of shape (m, n_H_prev, n_W_prev, n_C_prev)
  dW -- gradient of the cost with respect to the weights of the conv layer (W)
     numpy array of shape (f, f, n_C_prev, n_C)
  db -- gradient of the cost with respect to the biases of the conv layer (b)
     numpy array of shape (1, 1, 1, n_C)
  """
  # 獲取前向傳播中存儲的cache
  (A_prev, W, b, hparameters) = cache  
# 前一層輸入的shape (m, n_H_prev, n_W_prev, n_C_prev) = A_prev.shape
# 濾波器的 shape (f, f, n_C_prev, n_C) = W.shape
# 步幅和權重參數 stride = hparameters['stride'] pad = hparameters['pad']
# dZ 的shape (m, n_H, n_W, n_C) = dZ.shape
# 初始化 dA_prev, dW, db dA_prev = np.zeros((m, n_H_prev, n_W_prev, n_C_prev)) dW = np.zeros((f, f, n_C_prev, n_C)) db = np.zeros((1, 1, 1, n_C))
# 對A_prev 和 dA_prev 執行零填充 A_prev_pad = zero_pad(A_prev, pad) dA_prev_pad = zero_pad(dA_prev, pad)
for i in range(m): # select ith training example from A_prev_pad and dA_prev_pad a_prev_pad = A_prev_pad[i,:,:,:] da_prev_pad = dA_prev_pad[i,:,:,:]
for h in range(n_H): for w in range(n_W): for c in range(n_C): # 獲取當前感受野 vert_start = h * stride vert_end = vert_start + f horiz_start = w * stride horiz_end = horiz_start + f
# 獲取當前濾波器矩陣 a_slice = a_prev_pad[vert_start:vert_end, horiz_start:horiz_end, :]
# 梯度更新 da_prev_pad[vert_start:vert_end, horiz_start:horiz_end, :] += W[:,:,:,c] * dZ[i, h, w, c] dW[:,:,:,c] += a_slice * dZ[i, h, w, c] db[:,:,:,c] += dZ[i, h, w, c] dA_prev[i, :, :, :] = da_prev_pad[pad:-pad, pad:-pad, :]
assert(dA_prev.shape == (m, n_H_prev, n_W_prev, n_C_prev))
return dA_prev, dW, db
CNN反向傳播過程:池化

反向傳播中的池化操作跟卷積也是類似的。再此之前,我們需要根據濾波器為最大池化和平均池化分別創建一個 mask 和一個 distribute_value :

def create_mask_from_window(x):  
""" Creates a mask from an input matrix x, to identify the max entry of x. Arguments: x -- Array of shape (f, f) Returns: mask -- Array of the same shape as window, contains a True at the position corresponding to the max entry of x. """ mask = (x == np.max(x))
return mask
def distribute_value(dz, shape):  
""" Distributes the input value in the matrix of dimension shape Arguments: dz -- input scalar shape -- the shape (n_H, n_W) of the output matrix for which we want to distribute the value of dz Returns: a -- Array of size (n_H, n_W) for which we distributed the value of dz """ (n_H, n_W) = shape
# Compute the value to distribute on the matrix average = dz / (n_H * n_W)
# Create a matrix where every entry is the "average" value a = np.full(shape, average)
return a

然后整合封裝最大池化的反向傳播過程:

def pool_backward(dA, cache, mode = "max"):  
""" Arguments: dA -- gradient of cost with respect to the output of the pooling layer, same shape as A cache -- cache output from the forward pass of the pooling layer, contains the layer's input and hparameters mode -- the pooling mode you would like to use, defined as a string ("max" or "average") Returns: dA_prev -- gradient of cost with respect to the input of the pooling layer, same shape as A_prev """ # Retrieve information from cache (A_prev, hparameters) = cache
# Retrieve hyperparameters from "hparameters" stride = hparameters['stride'] f = hparameters['f']
# Retrieve dimensions from A_prev's shape and dA's shape m, n_H_prev, n_W_prev, n_C_prev = A_prev.shape m, n_H, n_W, n_C = dA.shape
# Initialize dA_prev with zeros dA_prev = np.zeros((m, n_H_prev, n_W_prev, n_C_prev))
for i in range(m): # select training example from A_prev a_prev = A_prev[i,:,:,:]
for h in range(n_H): for w in range(n_W): for c in range(n_C): # Find the corners of the current "slice" vert_start = h * stride vert_end = vert_start + f horiz_start = w * stride horiz_end = horiz_start + f
# Compute the backward propagation in both modes. if mode == "max": a_prev_slice = a_prev[vert_start:vert_end, horiz_start:horiz_end, c] mask = create_mask_from_window(a_prev_slice) dA_prev[i, vert_start: vert_end, horiz_start: horiz_end, c] += np.multiply(mask, dA[i,h,w,c]) elif mode == "average": # Get the value a from dA da = dA[i,h,w,c]
# Define the shape of the filter as fxf shape = (f,f)
# Distribute it to get the correct slice of dA_prev. i.e. Add the distributed value of da. dA_prev[i, vert_start: vert_end, horiz_start: horiz_end, c] += distribute_value(da, shape)
# Making sure your output shape is correct assert(dA_prev.shape == A_prev.shape)
return dA_prev

這樣卷積神經網絡的整個前向傳播和反向傳播過程我們就搭建好了。可以說是非常費力的操作了,但我相信,經過這樣一步步的根據原理的手寫,你一定會對卷積神經網絡的原理理解更加深刻了。

本文由《自興動腦人工智能》項目部 凱文 投稿。



聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • 神經網絡
    +關注

    關注

    42

    文章

    4838

    瀏覽量

    107801
  • 人工智能
    +關注

    關注

    1817

    文章

    50098

    瀏覽量

    265423
  • 機器學習
    +關注

    關注

    66

    文章

    8553

    瀏覽量

    136962
  • 深度學習
    +關注

    關注

    73

    文章

    5599

    瀏覽量

    124400
收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關推薦
    熱點推薦

    穿孔機頂頭檢測儀 機器視覺深度學習

    頂頭狀態。 檢測頂頭算法 引入人工智深度學習技術,通過Keras實現卷積神經網絡(CNN),用Numpy實現采集數據的訓練,得到符合現場需求
    發表于 12-22 14:33

    神經網絡的初步認識

    日常生活中的智能應用都離不開深度學習,而深度學習則依賴于神經網絡的實現。什么是神經網絡
    的頭像 發表于 12-17 15:05 ?335次閱讀
    <b class='flag-5'>神經網絡</b>的初步認識

    自動駕駛中常提的卷積神經網絡啥?

    在自動駕駛領域,經常會聽到卷積神經網絡技術。卷積神經網絡,簡稱為CNN,是種專門用來處理網格狀數據(比如圖像)的
    的頭像 發表于 11-19 18:15 ?2079次閱讀
    自動駕駛中常提的<b class='flag-5'>卷積</b><b class='flag-5'>神經網絡</b>是<b class='flag-5'>個</b>啥?

    CNN卷積神經網絡設計原理及在MCU200T上仿真測試

    數的提出很大程度的解決了BP算法在優化深層神經網絡時的梯度耗散問題。當x&gt;0 時,梯度恒為1,無梯度耗散問題,收斂快;當x&lt;0 時,該層的輸出為0。 CNN
    發表于 10-29 07:49

    NMSIS神經網絡庫使用介紹

    (q7_t) 和 16 位整數 (q15_t)。 卷積神經網絡示例: 本示例中使用的 CNN 基于來自 Caffe 的 CIFAR-10 示例。神經網絡由 3
    發表于 10-29 06:08

    構建CNN網絡模型并優化的般化建議

    整個模型非常巨大。所以要想實現輕量級的CNN神經網絡模型,首先應該避免嘗試單層神經網絡。 2)減少卷積核的大小:CNN神經網絡是通過權值共享的方式,
    發表于 10-28 08:02

    卷積運算分析

    的數據,故設計了ConvUnit模塊實現單個感受域規模的卷積運算. 卷積運算:不同于數學當中提及到的卷積概念,CNN神經網絡中的卷積嚴格意義
    發表于 10-28 07:31

    從零開始利用NMSIS庫搭建神經網絡

    環境:Vivado2021.1、NucleiStudio_IDE_202102-win64 內容:從零開始利用NMSIS庫搭建神經網絡,這節主講基本的NMSIS庫
    發表于 10-24 13:47

    在Ubuntu20.04系統中訓練神經網絡模型的些經驗

    模型。 我們使用MNIST數據集,訓練卷積神經網絡(CNN)模型,用于手寫數字識別。旦模型被訓練并保存,就可以用于對新圖像進行推理和預
    發表于 10-22 07:03

    CICC2033神經網絡部署相關操作

    在完成神經網絡量化后,需要將神經網絡部署到硬件加速器上。首先需要將所有權重數據以及輸入數據導入到存儲器內。 在仿真環境下,可將其存于文件,并在 Verilog 代碼中通過 read
    發表于 10-20 08:00

    液態神經網絡(LNN):時間連續性與動態適應性的神經網絡

    1.算法簡介液態神經網絡(LiquidNeuralNetworks,LNN)是種新型的神經網絡架構,其設計理念借鑒自生物神經系統,特別是秀麗隱桿線蟲的
    的頭像 發表于 09-28 10:03 ?1226次閱讀
    液態<b class='flag-5'>神經網絡</b>(LNN):時間連續性與動態適應性的<b class='flag-5'>神經網絡</b>

    如何在機器視覺中部署深度學習神經網絡

    圖 1:基于深度學習的目標檢測可定位已訓練的目標類別,并通過矩形框(邊界框)對其進行標識。 在討論人工智能(AI)或深度學習時,經常會出現“神經網絡
    的頭像 發表于 09-10 17:38 ?902次閱讀
    如何在機器視覺中部署<b class='flag-5'>深度</b><b class='flag-5'>學習</b><b class='flag-5'>神經網絡</b>

    卷積神經網絡如何監測皮帶堵料情況 #人工智能

    卷積神經網絡
    jf_60804796
    發布于 :2025年07月01日 17:08:42

    神經網絡專家系統在電機故障診斷中的應用

    摘要:針對傳統專家系統不能進行自學習、自適應的問題,本文提出了基于種經網絡專家系統的并步電機故障診斷方法。本文將小波神經網絡和專家系統相結合,充分發揮了二者故障診斷的優點,很大程度上降低了對電機
    發表于 06-16 22:09

    基于FPGA搭建神經網絡的步驟解析

    本文的目的是在神經網絡已經通過python或者MATLAB訓練好的神經網絡模型,將訓練好的模型的權重和偏置文件以TXT文件格式導出,然后通過python程序將txt文件轉化為coe
    的頭像 發表于 06-03 15:51 ?1218次閱讀
    基于FPGA<b class='flag-5'>搭建</b><b class='flag-5'>神經網絡</b>的步驟解析