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.

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

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.

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.

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.