TypeError: can only concatenate tuple (not "float") to tuple cousera deeplearnig.ai
TypeError: can only concatenate tuple (not "float") to tuple cousera deeplearnig.ai
有人能帮忙吗?
我正在从 deeplearning.ai 进行深度学习
我在第 2 周当然 1
我的传播功能如下
前向传播:
你得到 X
您计算 A=σ(wTX+b)=(a(1),a(2),...,a(m−1),a(m))A=σ(wTX+b)=(a( 1),a(2),...,a(m−1),a(m))
您计算成本函数:J=−1m∑mi=1y(i)log(a(i))+(1−y(i))log(1−a(i))J=−1m∑i=1my (i)log(a(i))+(1−y(i))log(1−a(i))
# GRADED FUNCTION: propagate
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 = sigmoid(np.dot((w.T,X)+b)) # compute activation
cost = -1/m*np.sum(Y*np.log(A)+(1-Y)*np.log(1-A), axis=1,keepdims=True) # compute cost
### END CODE HERE ###
# BACKWARD PROPAGATION (TO FIND GRAD)
### START CODE HERE ### (≈ 2 lines of code)
dw = 1/m*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}
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))
但是我遇到了以下错误
TypeError Traceback (most recent call last)
----> 3 grads, cost = propagate(w, b, X, Y)
---> 26 A = sigmoid(np.dot((w.T,X)+b)) # compute activation
TypeError: can only concatenate tuple (not "float") to tuple
如何解决?
我的 sigmoid 函数工作正常..
您的错误在表达式 np.dot((w.T,X)+b)
中。在此表达式中,您将函数 np.dot
应用于 one 参数 (w.T,X)+b
。反过来,它由元组 (w.T, X)
和您尝试加在一起的浮点数 b
组成(这是不可能的)。
问题出在你的括号上。您想使用 两个 参数 w.T
和 X
调用该函数,然后将 b
添加到结果中:np.dot(w.T,X)+b
.
有人能帮忙吗? 我正在从 deeplearning.ai 进行深度学习 我在第 2 周当然 1 我的传播功能如下 前向传播:
你得到 X 您计算 A=σ(wTX+b)=(a(1),a(2),...,a(m−1),a(m))A=σ(wTX+b)=(a( 1),a(2),...,a(m−1),a(m)) 您计算成本函数:J=−1m∑mi=1y(i)log(a(i))+(1−y(i))log(1−a(i))J=−1m∑i=1my (i)log(a(i))+(1−y(i))log(1−a(i))
# GRADED FUNCTION: propagate
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 = sigmoid(np.dot((w.T,X)+b)) # compute activation
cost = -1/m*np.sum(Y*np.log(A)+(1-Y)*np.log(1-A), axis=1,keepdims=True) # compute cost
### END CODE HERE ###
# BACKWARD PROPAGATION (TO FIND GRAD)
### START CODE HERE ### (≈ 2 lines of code)
dw = 1/m*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}
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))
但是我遇到了以下错误
TypeError Traceback (most recent call last)
----> 3 grads, cost = propagate(w, b, X, Y)
---> 26 A = sigmoid(np.dot((w.T,X)+b)) # compute activation
TypeError: can only concatenate tuple (not "float") to tuple
如何解决? 我的 sigmoid 函数工作正常..
您的错误在表达式 np.dot((w.T,X)+b)
中。在此表达式中,您将函数 np.dot
应用于 one 参数 (w.T,X)+b
。反过来,它由元组 (w.T, X)
和您尝试加在一起的浮点数 b
组成(这是不可能的)。
问题出在你的括号上。您想使用 两个 参数 w.T
和 X
调用该函数,然后将 b
添加到结果中:np.dot(w.T,X)+b
.