当前位置:   article > 正文

吴恩达Coursera深度学习课程 DeepLearning.ai 编程作业——Gradients_check(2-1.3)_gc_utils.py testcases.py

gc_utils.py testcases.py

这里写图片描述

import numpy as np
from testCases import *
from gc_utils import sigmoid,relu,dictionary_to_vector,vector_to_dictionary,gradients_to_vector
from testCases import gradient_check_n_test_case
  • 1
  • 2
  • 3
  • 4

Gradient_check.py

import numpy as np
from testCases import *
from gc_utils import sigmoid,relu,dictionary_to_vector,vector_to_dictionary,gradients_to_vector
from testCases import gradient_check_n_test_case
def gradient_check(x,theta,epsilon= 1e-7):
    J=x*theta
    dtheta=x
    gradapprox=(x*(theta+epsilon)-x*(theta-epsilon))/(2*epsilon)
    grad=dtheta
    numerator=np.linalg.norm(grad-gradapprox)
    denomitor=np.linalg.norm(grad)+np.linalg.norm(gradapprox)
    difference=numerator/denomitor
    if difference < epsilon:
        print "the gradient is correct"
    elif difference >= epsilon:
        print "the gradient is not so ideal"
    return J,difference
    
x,theta=2,4
J,difference = gradient_check(x,theta)
print("difference = "+str(difference))


def forward_propagation_n(X,Y,parameters):
    m=X.shape[1]
    W1=parameters["W1"]
    b1=parameters["b1"]
    W2=parameters["W2"]
    b2=parameters["b2"]  
    W3=parameters["W3"]
    b3=parameters["b3"]
    
    Z1=np.dot(W1,X)+b1
    A1=relu(Z1)
    Z2=np.dot(W2,A1)+b2
    A2=relu(Z2)
    Z3=np.dot(W3,A2)+b3
    A3=sigmoid(Z3)
    cost=(-1.0/m)*(np.sum(Y*np.log(A3)+(1-Y)*np.log(1-A3)))
    cache=(Z1,A1,W1,b1,Z2,A2,W2,b2,Z3,A3,W3,b3)
    return cost,cache

def backward_propagation_n(X,Y,cache):
    (Z1,A1,W1,b1,Z2,A2,W2,b2,Z3,A3,W3,b3)=cache
    m=X.shape[1]
    grads={}
    dZ3=A3-Y
    dW3=(1.0/m)*np.dot(dZ3,A2.T)
    db3=(1.0/m)*np.sum(dZ3,axis=1,keepdims=True)
    
    dA2=np.dot(W3.T,dZ3)   
    dZ2=np.multiply(dA2,np.int64(Z2>0))
    dW2=(1.0/m)*np.dot(dZ2,A1.T)
    db2=(1.0/m)*np.sum(dZ2,axis=1,keepdims=True)
    
    dA1=np.dot(W2.T,dZ2)   
    dZ1=np.multiply(dA1,np.int64(Z1>0))
    dW1=(1.0/m)*np.dot(dZ1,X.T)
    db1=(1.0/m)*np.sum(dZ1,axis=1,keepdims=True)
    
    grads={"dZ3": dZ3, "dW3": dW3, "db3": db3,
                 "dA2": dA2, "dZ2": dZ2, "dW2": dW2, "db2": db2,
                 "dA1": dA1, "dZ1": dZ1, "dW1": dW1, "db1": db1}
    return grads
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/351992
推荐阅读
  

闽ICP备14008679号