問題

有人可以幫忙嗎? 我正在深入學習deeplearning.ai 我在我的第二個星期當然是第一個星期 我的傳播函式如下 前傳:

你得到X 你計算A = Jony(wtx b)=(a(1),a(2),...,a(m-1),a(m) 您計算的成本函式:j = - 1m namm mi = 1y(i) log(a(i)) +(1-y(i)) log(1-a(i) J = - 1m nam i = 1m(i) log under(a(i))(1-y(i)) underlog(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應用到一個引數(w.T,X)+b.反過來,這由元組(w.T, X)和floatingpoint b組成,您嘗試將它新增在一起(這是不可能的).

問題在於括號.您想用兩個引數w.TX呼叫函式,然後將b新增到結果:np.dot(w.T,X)+b.

  相同標籤的其他問題

pythondeep-learninglogistic-regression