资源简介

神经网络与深度学习(吴恩达)第三周编程练习重新上传

资源截图

代码片段和文件信息

import numpy as np
import h5py
import matplotlib.pyplot as plt 
from testCases_v2 import *
from dnn_app_utils_v2 import sigmoidsigmoid_backwardrelurelu_backward

plt.rcParams[‘figure.figsize‘]=(5.04.0)
plt.rcParams[‘image.interpolation‘]=‘nearest‘
plt.rcParams[‘image.cmap‘]=‘gray‘

np.random.seed(1)

def initialize_parameters(n_xn_hn_y):

    W1=np.random.randn(n_hn_x)*0.01
    b1=np.zeros((n_h1))
    W2=np.random.randn(n_yn_h)*0.01
    b2=np.zeros((n_y1))

    parameters={‘W1‘:W1
                ‘b1‘:b1
                ‘W2‘:W2
                ‘b2‘:b2}
    return parameters

def initialize_parameters_deep(layer_dims):
    np.random.seed(3)
    parameters={}
    L=len(layer_dims)
    for l in range(1L):
        parameters[‘W‘+str(l)]=np.random.randn(layer_dims[l]layer_dims[l-1])*0.01
        parameters[‘b‘+str(l)]=np.zeros([layer_dims[l]1])
    return parameters

def linear_forward(AWb):
    Z=np.dot(WA)+b
    cache=(AWb)
    return Zcache

def linear_activation_forward(A_prevWbactivation):
    if activation==‘sigmoid‘:
        Zlinear_cache=linear_forward(A_prevWb)
        Aactivation_cache=sigmoid(Z)
    elif activation==‘relu‘:
        Zlinear_cache=linear_forward(A_prevWb)
        Aactivation_cache=relu(Z)

    cache=(linear_cacheactivation_cache)
    return Acache

def L_model_forward(Xparameters):
    caches=[]
    A=X
    L=int(len(parameters)/2)
    for l in range(1L):
        A_prev=A
        Acache=linear_activation_forward(Aparameters[‘W‘+str(l)]parameters[‘b‘+str(l)]‘relu‘)
        caches.append(cache)
    ALcache=linear_activation_forward(Aparameters[‘W‘+str(L)]parameters[‘b‘+str(L)]‘sigmoid‘)
    caches.append(cache)
    return ALcaches

def computer_cost(ALY):
    m=Y.shape[1]
    cost=(-1/m)*np.sum(np.multiply(Ynp.log(AL))+np.multiply((1-Y)np.log(1-AL)))
    cost=np.squeeze(cost)
    return cost

def linear_backward(dZcache):
    A_prevWb=cache
    m=A_prev.shape[1]

    dW=np.dot(dZA_prev.T)/m
    db=np.sum(dZ)/m
    dA_prev=np.dot(W.TdZ)

    return dA_prevdWdb

def linear_activation_backward(dAcacheactivation):
    linear_cacheactivation_cache=cache
    if activation==‘relu‘:
        dZ=relu_backward(dAactivation_cache)
        dA_prevdWdb=linear_backward(dZlinear_cache)
    elif activation==‘sigmoid‘:
        dZ=sigmoid_backward(dAactivation_cache)
        dA_prevdWdb=linear_backward(dZlinear_cache)

    return dA_prevdWdb

def L_model_backward(ALYcaches):
    grads={}
    L=len(caches)
    m=AL.shape[1]
    Y=Y.reshape(AL.shape)

    dAL=-(np.divide(YAL)-np.divide(1-Y1-AL))

    current_cache=caches[L-1]
    grads[‘dA‘+str(L)]grads[‘dW‘+str(L)]grads[‘db‘+str(L)]=linear_activation_backward(dALcurrent_cache‘sigmoid‘)

    for l in reversed(range(L-1)):
        current_cache=caches[l]
        dA_prev_tempdW_tempdb_temp=linear_activation_backward(grads[

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-11-02 07:45  NeuralNetwork_3\
     文件     2572022  2017-10-25 02:21  NeuralNetwork_3\train_catvnoncat.h5
     文件      616958  2017-10-25 02:21  NeuralNetwork_3\test_catvnoncat.h5
     文件        3504  2017-10-25 03:06  NeuralNetwork_3\demo3.py
     文件       15596  2017-10-25 03:26  NeuralNetwork_3\dnn_app_utils_v2.py
     文件        4547  2017-10-25 03:36  NeuralNetwork_3\demo4.py
     文件           0  2017-11-02 08:04  NeuralNetwork_3\asfa
     目录           0  2017-11-02 07:33  NeuralNetwork_3\.vscode\
     文件        1051  2017-11-02 07:33  NeuralNetwork_3\.vscode\launch.json
     目录           0  2017-11-09 08:10  NeuralNetwork_3\images\
     文件        1609  2017-10-25 03:33  NeuralNetwork_3\images\my_image.jpg
     目录           0  2017-11-08 12:28  NeuralNetwork_3\datasets\
     文件      616958  2017-10-25 01:56  NeuralNetwork_3\datasets\test_catvnoncat.h5
     文件           0  2017-11-08 12:28  NeuralNetwork_3\datasets\demo.c
     文件     2572022  2017-10-25 01:53  NeuralNetwork_3\datasets\train_catvnoncat.h5
     目录           0  2017-10-25 03:26  NeuralNetwork_3\__pycache__\
     文件       13517  2017-10-25 03:26  NeuralNetwork_3\__pycache__\dnn_app_utils_v2.cpython-36.pyc
     文件        5983  2017-10-24 06:33  NeuralNetwork_3\__pycache__\testCases_v2.cpython-36.pyc
     文件        3714  2017-10-25 01:48  NeuralNetwork_3\__pycache__\demo3.cpython-36.pyc

评论

共有 条评论