1. 程式人生 > >吳恩達.深度學習系列-C1神經網路與深度學習-w4-( 作業:建立神經網路)

吳恩達.深度學習系列-C1神經網路與深度學習-w4-( 作業:建立神經網路)

前言

注意:coursera要求不要在網際網路公佈自己的作業。如果你在學習這個課程,建議你進入課程系統自行完成作業。我覺得程式碼有參考和保留的意義。
本週的作業包含兩個部分。Building your Deep Neural Network, Deep Neural Network - Application v8。總體來說作業很簡單,有時都不用審題跟著嚮導、程式碼提示做都能寫出來。尤其是Deep Neural Network - Application v8部分。但我覺得第一部分感覺更重要些。從程式碼層面去實現前向傳播與反向傳播之間的關係。

Building your Deep Neural Network

這裡寫圖片描述
計算交叉熵cross-entropy cost J, 如下等式:

(7)1mi=1m(y(i)log(a[L](i))+(1y(i))log(1a[L](i)))
交叉熵的導數是反向傳播的起點:
Initializing backpropagation:
進行反向傳播,我們知道前向傳播的輸出是A[L]=σ(Z[L]). 我們必須去計算AL的偏導數, dAL =LA[L].
可以推導獲得dAL(偏導)得出如下等式 :
dAL = - (np.divide(Y, AL) - np.divide(1
- Y, 1 - AL)) # derivative of cost with respect to AL

啟用函式的偏導分兩種情況,輸出層的sigmoid,與隱藏層的relu。作業中sigmoid與relu前向,反向(偏導)的程式碼都是系統自己實現的,這裡根據我自己的理解補充說明一下。
1)A=sigmoid(Z)
sigmoid’(Z)=sigmoid(Z)(1-sigmoid(Z))
2)A=Relu(Z)
if(Z>=0){
Relu’(Z)=1
else{
Relu’(Z)=0 #Z<0時,導數為0.
}

The three outputs

(dW[l],db[l],dA[l]) are computed using the input dZ[l].Here are the formulas you need:

(8)dW[l]=LW[l]=1mdZ[l]A[l1]T
(9)db[l]=Lb[l]=1mi=1mdZ[l](i)
(10)dA[l1]=LA[l1]=W[l]TdZ[l]

Deep Neural Network - Application v8

Deep Neural Network for Image Classification: Application

When you finish this, you will have finished the last programming assignment of Week 4, and also the last programming assignment of this course!

You will use use the functions you’d implemented in the previous assignment to build a deep network, and apply it to cat vs non-cat classification. Hopefully, you will see an improvement in accuracy relative to your previous logistic regression implementation.

After this assignment you will be able to:
- Build and apply a deep neural network to supervised learning.

Let’s get started!

1 - Packages

Let’s first import all the packages that you will need during this assignment.
- numpy is the fundamental package for scientific computing with Python.
- matplotlib is a library to plot graphs in Python.
- h5py is a common package to interact with a dataset that is stored on an H5 file.
- PIL and scipy are used here to test your model with your own picture at the end.
- dnn_app_utils provides the functions implemented in the “Building your Deep Neural Network: Step by Step” assignment to this notebook.
- np.random.seed(1) is used to keep all the random function calls consistent. It will help us grade your work.

import time
import numpy as np
import h5py
import matplotlib.pyplot as plt
import scipy
from PIL import Image
from scipy import ndimage
from dnn_app_utils_v3 import *

%matplotlib inline
plt.rcParams['figure.figsize'] = (5.0, 4.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

%load_ext autoreload
%autoreload 2

np.random.seed(1)
/opt/conda/lib/python3.5/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
/opt/conda/lib/python3.5/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')

2 - Dataset

You will use the same “Cat vs non-Cat” dataset as in “Logistic Regression as a Neural Network” (Assignment 2). The model you had built had 70% test accuracy on classifying cats vs non-cats images. Hopefully, your new model will perform a better!

Problem Statement: You are given a dataset (“data.h5”) containing:
- a training set of m_