Era*_*oda 1 propagation backpropagation neural-network gradient-descent deep-learning
我一直在关注关于深度学习的在线教程。它有一个关于梯度下降和成本计算的实际问题,一旦将其转换为 python 代码,我就一直在努力获得给定的答案。希望你能帮助我得到正确的答案
请参阅以下链接以了解所使用的方程式 单击此处查看用于计算的方程式
以下是计算梯度下降、成本等的函数。需要在不使用 for 循环但使用矩阵操作操作的情况下找到这些值
import numpy as np
def propagate(w, b, X, Y):
"""
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 = # compute activation
cost = # compute cost
### END CODE HERE ###
# BACKWARD PROPAGATION (TO FIND GRAD)
### START CODE HERE ### (? 2 lines of code)
dw =
db =
### END CODE HERE ###
assert(dw.shape == w.shape)
assert(db.dtype == float)
cost = np.squeeze(cost)
assert(cost.shape == ())
grads = {"dw": dw,
"db": db}
return grads, cost
Run Code Online (Sandbox Code Playgroud)
以下是用于测试上述功能的数据
w, b, X, Y = np.array([[1],[2]]), 2, np.array([[1,2],[3,4]]),
np.array([[1,0]])
grads, cost = propagate(w, b, X, Y)
print ("dw = " + str(grads["dw"]))
print ("db = " + str(grads["db"]))
print ("cost = " + str(cost))
Run Code Online (Sandbox Code Playgroud)
以下是上述的预期输出
Expected Output:
dw [[ 0.99993216] [ 1.99980262]]
db 0.499935230625
cost 6.000064773192205
Run Code Online (Sandbox Code Playgroud)
对于上面的传播函数,我使用了下面的替换,但输出不是预期的。请帮助如何获得预期的输出
A = sigmoid(X)
cost = -1*((np.sum(np.dot(Y,np.log(A))+np.dot((1-Y),(np.log(1-A))),axis=0))/m)
dw = (np.dot(X,((A-Y).T)))/m
db = np.sum((A-Y),axis=0)/m
Run Code Online (Sandbox Code Playgroud)
以下是用于计算激活的 sigmoid 函数:
def sigmoid(z):
"""
Compute the sigmoid of z
Arguments:
z -- A scalar or numpy array of any size.
Return:
s -- sigmoid(z)
"""
### START CODE HERE ### (? 1 line of code)
s = 1 / (1+np.exp(-z))
### END CODE HERE ###
return s
Run Code Online (Sandbox Code Playgroud)
希望有人能帮助我理解如何解决这个问题,因为我无法在不理解这一点的情况下继续学习其余的教程。非常感谢
您可以计算 A,cost,dw,db 如下:
A = sigmoid(np.dot(w.T,X) + b)
cost = -1 / m * np.sum(Y*np.log(A)+(1-Y)*np.log(1-A))
dw = 1/m * np.dot(X,(A-Y).T)
db = 1/m * np.sum(A-Y)
Run Code Online (Sandbox Code Playgroud)
其中 sigmoid 是:
def sigmoid(z):
s = 1 / (1 + np.exp(-z))
return s
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7592 次 |
| 最近记录: |