DLS Course #1 --- Stuck on assignment --Excercise 8

Assignment #1 – I am stuck on following:

Exercise 8 - model

{moderator edit - solution code removed}

Getting following Error:


ValueError Traceback (most recent call last)
in
1 from public_tests import *
2
----> 3 model_test(model)

~/work/release/W2A2/public_tests.py in model_test(target)
123 y_test = np.array([0, 1, 0])
124
→ 125 d = target(X, Y, x_test, y_test, num_iterations=50, learning_rate=0.01)
126
127 assert type(d[‘costs’]) == list, f"Wrong type for d[‘costs’]. {type(d[‘costs’])} != list"

in model(X_train, Y_train, X_test, Y_test, num_iterations, learning_rate, print_cost)
34 #w,b = initialize_with_zeros(dim)
35 w,b = initialize_with_zeros(X_train.size)
—> 36 param, grads, cost = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)
37 w = params[“w”]
38 b = params[“b”]

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

in propagate(w, b, X, Y)
31 # YOUR CODE STARTS HERE
32 W_T = np.transpose(w)
—> 33 A = sigmoid(np.dot(W_T,X)+b)
34 #cost = -np.sum(np.dot(np.log(A).reshape(A.shape[1],A.shape[0]),Y)+np.dot(np.log(np.subtract(1,A)),np.subtract(1,Y).reshape(Y.shape[1],Y.shape[0])))/m
35 cost = -np.sum(np.dot(np.log(A),Y.reshape(Y.shape[1],Y.shape[0]))+np.dot(np.log(np.subtract(1,A)),np.subtract(1,Y).reshape(Y.shape[1],Y.shape[0])))/m

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

ValueError: shapes (1,28) and (4,7) not aligned: 28 (dim 1) != 4 (dim 0)

Not sure what I am missing here .
Can anyone help me …

Your w is the wrong shape. So where is the shape of w determined? By the argument you pass to the initialize routine, right? What do you get from the size attribute of a numpy array? Give it a try and watch what happens:

np.random.seed(42)
A = np.random.randn(3,5)
print(f"A = {A}")
print(f"A.size = {A.size}")
A = [[ 0.49671415 -0.1382643   0.64768854  1.52302986 -0.23415337]
 [-0.23413696  1.57921282  0.76743473 -0.46947439  0.54256004]
 [-0.46341769 -0.46572975  0.24196227 -1.91328024 -1.72491783]]
A.size = 15

That’s not what you want, right?

1 Like

Thanks Paul for your quick response
. You are right i was passing wrong argument to intialise_tozeros subroutine .
I had to pass X.shape[0] .

Thanks
Vinod

Hello,
After so much struggling with this error “ValueError: shapes (1,7) and (4,7) not aligned: 7 (dim 1) != 4 (dim 0)” I finally found the solution.
I don’t know if what I am about to say is 100% right, but if you are also getting this error (Or something similar) while passing all the other tests, don’t waste your time looking at the previous steps. There is something wrong with the model part, at least it was for me. I had a typo in my code and it was not a syntax error so it worked but did not generate the correct output. It was true in my head but when writing it, I might have mistyped it.
Bottom line is, CHECK YOUR CODE BEFORE THINKING ABOUT ANY OTHER SOLUTION BECAUSE YOU ARE GONNA END UP GOUGING YOUR EYES OUT LIKE ME. :slight_smile:

Happy Coding and Analysing Everyone.