0

I am working on the Assignment 2 of Prof.Andrew Ng's deep learning course. I have to compute the cost and the gradients (dw,db) of the logistic regression. I am clueless as to what is wrong with my code. All sorts of errors come up on after the other. I am attaching the code.

Thank you

Cost and gradient equations

import numpy as np

def propagate(w, b, X, Y):

    m = X.shape[1]

    X=X.reshape((X.shape[0]*X.shape[1]), 1)

    f=X.shape[0]*X.shape[1]
    g=int(f-(w.shape[0]*w.shape[1]))
    w=np.array(np.append(w, np.zeros(g)))
    w=w.reshape(f, 1)
    assert(w.shape == (f, 1))

    #Sigmoid function
    z=(np.dot(w.T, X) + b)
    A=1/(1 + np.exp(-z))

    # cost calculation   
    cost = -(1/m)*np.sum(np.multiply(Y, np.log(A)) + np.multiply((1-Y), np.log(1-A)), axis=1)

    dw =(1/m)*np.dot((A-Y).T, X)
    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))
3
  • it would be helpful if you provided the errors you are getting Commented Apr 12, 2020 at 12:07
  • the errors are mostly the dimensional mismatch errors for z calculation ( i have tried a work around by zero padding w; not sure whether it is a correct step) ,(A-Y) calculation also throws up dimensional error, then there is the assertion error. The problem is with array/vector dimensions in calculations, my efforts to run the code smoothly are in vain. The expected answers for the problem as given are as follows dw = [[ 0.99845601] [ 2.39507239]] db = 0.00145557813678 cost = 5.801545319394553
    – Joaquim
    Commented Apr 12, 2020 at 18:04
  • Sorry guys, it was my mistake that caused all these dimensional errors - i had unnecessarily reshaped X to a column vector :) Thank you for the patience Joaquim
    – Joaquim
    Commented Apr 18, 2020 at 15:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.