python-3.x - 为什么推导 db 变为负数?
问题描述
我正在从头开始构建猫和非猫深度学习模型。我已经初始化了参数 w 和 b。现在我被困在方法传播中。
我计算出相对于 b (db) 的损失梯度为 -0.0023151612773,但预期输出为 0.00145557813678。
此外 dw 为 [[ 0.99751791][ 2.39277178]] 但预期为 [[ 0.99845601] [ 2.39507239]]。
成本为 5.80237782517,但预期为 5.801545319394553。如果我犯了任何错误,请检查我的代码。谢谢
def propagate(w, b, X, Y):
"""
Implement the cost function and its gradient for the propagation explained above
Arguments:
w -- weights, a numpy array of size (num_px * num_px * 3, 1)
b -- bias, a scalar
X -- data of size (num_px * num_px * 3, number of examples)
Y -- true "label" vector (containing 0 if non-cat, 1 if cat) of size (1, number of examples)
Return:
cost -- negative log-likelihood cost for logistic regression
dw -- gradient of the loss with respect to w, thus same shape as w
db -- gradient of the loss with respect to b, thus same shape as b
Tips:
- Write your code step by step for the propagation. np.log(), np.dot()
"""
m = X.shape[1]
# FORWARD PROPAGATION (FROM X TO COST)
### START CODE HERE ### (≈ 2 lines of code)
A = 1/(1+np.exp(-np.dot(w.T,X)+b)) # compute activation
cost = -1/m*np.sum(np.dot(Y,np.log(A).T)+np.dot((1-Y),np.log(1-A).T))
# compute cost
### END CODE HERE ###
# BACKWARD PROPAGATION (TO FIND GRAD)
### START CODE HERE ### (≈ 2 lines of code)
dw = 1/m*np.dot(X,(A-Y).T)
db = 1/m*np.sum(A-Y)
### END CODE HERE ###
assert(dw.shape == w.shape)
# assert(db.dtype == float)
cost = np.squeeze(cost)
assert(cost.shape == ())
grads = {"dw": dw,
"db": db}
#print (m)
return grads, cost
w, b, X, Y = np.array([[1.],[2.]]), 2., np.array([[1.,2.,-1.],[3.,4.,-3.2]]), np.array([[1,0,1]])
grads, cost = propagate(w, b, X, Y)
print ("dw = " + str(grads["dw"]))
print ("db = " + str(grads["db"]))
print ("cost = " + str(cost))
解决方案
推荐阅读
- scala - 如何使用宏在 Dotty 中生成一个类?
- python - Phantom-Evasion OSError: [Errno 2] 没有这样的文件或目录
- assembly - 这个汇编指令占用多少ROB单元
- android - 如何在 AndroidManifest.xml 中隐藏 API 密钥
- prolog - 有没有办法在运行时在 prolog 中重新排序 CHR 规则?
- php - 如何防止 phpmailer 发送的带有 GET 参数的链接被另一个文件读取
- r - 合并R中列数奇数的两个数据表的问题
- javascript - 如何使用 JavaScript 在 div 中创建输入标签
- amazon-s3 - 从 JS 访问存储在 AWS S3 上的深度缩放图像
- python - 如何在同一数据集中将数字与 str 隔离