Neural Net and Deep learning week3 "Test the Model on the Planar Dataset"

Cost function works perfectly and passes all the tests but when appling it inside " Test the Model on the Planar Dataset" it gives me this error and i can’t seem to find out the problem.

in compute_cost(A2, Y)
21 # cost = …
22 # YOUR CODE STARTS HERE
—> 23 logprobs = np.multiply(np.log(A2), Y) + np.multiply((1 - Y), np.log(1 - A2))
24 cost = -1/m * np.sum(logprobs)
25

ValueError: operands could not be broadcast together with shapes (1,3) (1,400)

This is the problem i guess. This part of line doesnt seem to work on my case.

logprobs = np.multiply(np.log(A2),Y)

Hi mate, I believe your cost function formula is correct. However, I cannot be sure that the shape of your cost function is correct as well because value error is related to dimension mismatch. For those cases I prefer to apply print statement for A2 and Y so as to check whether they have same shape or not because they have to have same shape as well.

I checked the formula with test cases and it is correct. Also, where is the place of this error showed up? (Compute cost part or nn_model) I believe it is nn_model part

Yeah. It’s the nn_model part (5.2). Cost funtion worked perfectly individually.

So, in those cases as I suggest previously, you have to be sure about the parameters dimensions which are A2 and Y. Apply print statement so that you can narrow down your debugging search.

can i post github link for you to check?

Y.shape = (1,400)
np.log(A2).shape = (1,3)
If it workes on Compute cost part i dont understand why it doesnt work here

Here’s whole error message if it helps


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, num_iterations = 10000, print_cost=True)
3
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, t_X, t_Y)
49 parameters = update_parameters(parameters, grads)

in compute_cost(A2, Y)
22 # YOUR CODE STARTS HERE
23 logprobs = None
—> 24 logprobs=np.multiply(np.log(A2),Y) + np.multiply(np.log(1-A2),1-Y)
25 cost = None
26 cost = (-1/m)*np.sum(logprobs)

ValueError: operands could not be broadcast together with shapes (1,3) (1,400)

That would be violation of honor code. Hence, unfortunately, I could not do this. What I really want is to give you an intuation about a debugging skills.

Comments on Broadcasting
Mate, first of all, you should ask yourself what should you understand from these shapes and how do these shapes related to your error statement? We need to be sure that we Y and A2 cannot broadcast at these shapes because for broadcasting you have certain possibilities. Let me introduce a set up. We have two matrix A and B where A.shape = (a,b) ,B.shape(c,d). To make a broadcasting b=c=1 or b= d= 1 or a=1 when b= c and I can write many possibilities similar to those. One thing to be observed is that you cannot broadcast (1,400) and (1,3) because the idea behind the broadcasting says that we are copy the equivelent rows or columns for missing part of matrix so that apply basic algebraic operation. For example if A = (1,3) and B = (3,1) , (A+B).shape =(3,3) because for matrix a we have missing rows so it copies first rows and write it down so that 3 rows of matrix A can be correspond to 3 rows of matrix B. Same logic works for columns as well. Now, in our case there are no missing rows for A2 because Y and A2 has same number of rows so if we want to make basic algebraic operation their columns should be same as weel but they are not one is 400 and other is 3 and there is no corresponding column for 397 column as well.
Answer of your question:
I hope we are in same page what we should understand when we see the word broadcasting. Y is output provided by test case so please check that you do not change the test case inputs while parametrizing inside the nn_model function. It should be Y_assess not Y inside the function and X_assess as well. Note that Y.shape = (1,400) come from flower data set so you should not call it inside an nn_model function.

1 Like

You can see the problem in the exception trace that you posted: you are passing a global variable t_X as the argument to forward_propagation, instead of the parameter that was passed into nn_model. That is why the shapes don’t match. In these courses it’s always a mistake to reference global instead of local variables within the body of a function. You have the analogous bug when you invoke the backward propagation routine as well. It looks like you must have just “copy/pasted” that code from the test cases. Of course “copy/paste” is a fine technique, but you also have to carefully examine the contents to make sure it is valid as it stands. In this case, you need to adjust the names of the parameters.

3 Likes

Woah sir, Thanks. Can’t belive i was making this kind of mistake :exploding_head: