Course_1_Week3 5.2 Test the Model on the Planar Datase

My all test before this passes successfully. but for 5.2 section I am getting the below error. If really there is an error in computing cost, so why the earlier cases were passed?
I am getting the below error while testing:


ValueError Traceback (most recent call last)
in
1 # Build a model with a n_h-dimensional hidden layer
----> 2 parameters = nn_model(X, Y, n_h = 4, num_iterations = 10000, print_cost=True)
3 print(parameters)
4 # Plot the decision boundary
5 plot_decision_boundary(lambda x: predict(parameters, x.T), X, Y)

in nn_model(X, Y, n_h, num_iterations, print_cost)
45 # YOUR CODE STARTS HERE
46 A2, cache = forward_propagation(t_X, parameters)
—> 47 cost = compute_cost(A2, Y)
48 grads = backward_propagation(parameters, cache, X, Y)
49 parameters = update_parameters(parameters, grads)

in compute_cost(A2, Y)
24 #cost = - np.sum(logprobs)
25 #cost = np.dot(np.log(A2),Y.T)
—> 26 cost = -np.sum( np.dot(Y, np.log(A2.T)) + np.dot(1-Y, np.log(1-A2.T)) )/m
27 # YOUR CODE ENDS HERE
28 print(cost)

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

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

First, I think it would be useful to take a look at a the “meta-analysis” in a very recent by post by @paulinpaloalto. (The Lede: “If a test case fails, you should not assume that the problem is with the test. You should assume that it is your code that is wrong.”) It’s worth the quick read.

The proximate cause of your difficulties is indicated by the ValueError exception indicating that you are attempting to produce an invalid dot product. I like that your Y is 1 \times 400; these are the “ground truth” output labels (1’s and 0’s for True or False) of which there are 400. Great! Since you are now testing the model on the planar dataset, that is precisely what you would expect. But, the A.T coming in is 3 \times 1, not 400 \times 1. I suspect there is some “hardcoding” of function arguments somewhere up the stack, since the previous unit tests were set up with 3 data points.

Also, you do not need np.sumfunction in your cost function. The dot products are taking care of the summation. The np.sum function is redundant; it is summing a scalar. Take some time to understand this point, though. :nerd_face:

Me again. I would look to where you are producing A2, as a first pass. Hint: forward_propagation(), which in turn, calls your sigmoid() function. Maybe we’ll have some luck there, but no guarantees.

1 Like

Thanks, Kenb for your help.
You caught it right there was a hardcoding for X in forward_propagation() parameters inside nn_model().