Week 2 Logistic Regression, Exercise 8

I got a dimension error notification, but I have no idea what went wrong. My sigmoid() function and propagate() function passed all tests.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-37-7f17a31b22cb> in <module>
----> 1 model_test(model)

~/work/release/W2A2/public_tests.py in model_test(target)
    109     y_test = np.array([1, 0, 1])
    110 
--> 111     d = target(X, Y, x_test, y_test, num_iterations=50, learning_rate=1e-4)
    112 
    113     assert type(d['costs']) == list, f"Wrong type for d['costs']. {type(d['costs'])} != list"

<ipython-input-36-3858d85e8fbf> in model(X_train, Y_train, X_test, Y_test, num_iterations, learning_rate, print_cost)
     35     # YOUR CODE STARTS HERE
     36     w, b = initialize_with_zeros(X_train.shape[0])
---> 37     params, grads, costs = optimize(w, b, X, Y, num_iterations, learning_rate, print_cost)
     38     w = params["w"]
     39     b = params["b"]

<ipython-input-16-3536c18bba24> in optimize(w, b, X, Y, num_iterations, learning_rate, print_cost)
     35         # grads, cost = ...
     36         # YOUR CODE STARTS HERE
---> 37         grads, cost = propagate(w, b, X, Y)
     38 
     39         # YOUR CODE ENDS HERE

<ipython-input-35-5c816f54635e> in propagate(w, b, X, Y)
     29     # cost = ...
     30     # YOUR CODE STARTS HERE
---> 31     A = sigmoid(np.dot(w.T, X) + b)
     32     cost = (-1/m) * ( np.dot(Y, (np.log(A)).T) + np.dot((1-Y), (np.log(1-A)).T) ).sum(axis=1)
     33     # YOUR CODE ENDS HERE

<__array_function__ internals> in dot(*args, **kwargs)

ValueError: shapes (1,4) and (2,3) not aligned: 4 (dim 1) != 2 (dim 0)

Hi Tom,

Calculations between matrices require knowing their dimensions. Check the dimensions of your matrices well, and look at the dimension of the desired result so that the two coincide.

The problem is that you are passing the wrong X and Y values to optimize, when you call it from model. You are passing global variables, so they don’t match the expected sizes.

3 Likes

Thank you soooooooo much!
I just realized that I copied and pasted the optimize() function without changing its arguments… No idea why i made such a stupid mistake, thank you for pointing it out!

Glad to hear you found the solution. If it’s any comfort, you are very far from being the first person to make that mistake. It’s probably the single most common one on this function.