负梯度方向计算机编程,编程作业: Regularization梯度值计算出错

4a98ce3d3958b3c906dd41f6bb0b5a35.png

32016db9342d251ef5094d8849f841ed.png

# GRADED FUNCTION: gradient_check

def gradient_check(x, theta, epsilon = 1e-7):

"""

Implement the backward propagation presented in Figure 1.

Arguments:

x -- a real-valued input

theta -- our parameter, a real number as well

epsilon -- tiny shift to the input to compute approximated gradient with formula(1)

Returns:

difference -- difference (2) between the approximated gradient and the backward propagation gradient

"""

# Compute gradapprox using left side of formula (1). epsilon is small enough, you don't need to worry about the limit.

### START CODE HERE ### (approx. 5 lines)

thetaplus = theta+0.01                               # Step 1

thetaminus = theta-0.01                              # Step 2

J_plus = forward_propagation(x,thetaplus)                                  # Step 3

J_minus = forward_propagation(x,thetaminus)                                 # Step 4

gradapprox = (J_plus-J_minus)/(2*0.01)                              # Step 5

### END CODE HERE ###

# Check if gradapprox is close enough to the output of backward_propagation()

### START CODE HERE ### (approx. 1 line)

grad = backward_propagation(x,theta)

### END CODE HERE ###

### START CODE HERE ### (approx. 1 line)

numerator = np.linalg.norm(gradapprox-grad)                               # Step 1'

denominator = np.linalg.norm(grad)+np.linalg.norm(gradapprox)                             # Step 2'

difference = numerator/denominator                              # Step 3'

### END CODE HERE ###

if difference 

print ("The gradient is correct!")

else:

print ("The gradient is wrong!")

return difference

dcb1526e331ba027cf67a4ffaa8c2588.png

我怀疑是下面图的地方出错了,但不知道怎么修改呀?

a80fb0ba4858efa660847a554159681f.png

解决:

epsilon = 1e-7应该被使用,而不是0.01

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值