【发布时间】:2018-01-31 14:38:10
【问题描述】:
我一直在关注深度学习的在线教程。它有一个关于梯度下降和成本计算的实际问题,一旦将其转换为 python 代码,我一直在努力获得给定的答案。希望您能帮我得到正确的答案
有关使用的方程式,请参阅以下链接 Click here to see the equations used for the calculations
以下是计算梯度下降、成本等的函数。需要在不使用 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
以下是测试上述功能的数据
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))
以下是上述的预期输出
Expected Output:
dw [[ 0.99993216] [ 1.99980262]]
db 0.499935230625
cost 6.000064773192205
对于上面的传播函数,我使用了下面的替换,但输出不是预期的。请帮助如何获得预期的输出
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
以下是用于计算 Activation 的 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
希望有人可以帮助我理解如何解决这个问题,因为如果不理解这一点,我就无法继续学习其余的教程。非常感谢
【问题讨论】:
-
sigmoid: 1/(1 + np.exp(-x) 注意:你在 sigmoid 函数之外有“return s”(Python 在函数 def 下使用选项卡行来表示它们属于function). sigmoid 的导数:sigmoid(x) * (1 - sigmoid(x)) 您可以通过注意输出已经被 sigmoid 化来加速 sigmoid(x):dSigmoid = output * (1 - output) 在任何情况下,这是您可以使用的激活函数之一。看起来您的其余部分都在正确的轨道上。对于成本(您的意思是错误吗?)您可以从目标样本中减去输出。
标签: neural-network deep-learning backpropagation gradient-descent propagation