Exercise 8 of week 2 -Model

Buenos días, no puedo encontrar el error que cometí en este ejercicio . Me pueden ayudar por favor. Gracias

Hi @Nildo; welcome to the DL Specialization. The error message indicates that your implementation of the cost function is not correct. The output of the cost function should be a scalar, not a vector. In other words, it needs to be single-valued. The cost function is implemented in the propagate(...) function earlier in the notebook. Did that function pass its tests?

{moderator edit - solution code removed}

Hola kenb, la función propagate( ) si pasó la prueba, en la primera imagen se ve como se definió el cost como escalar, pero más adelante en la función optimize(…) dicen que debemos acumular después de cada 100 iteraciones el costo en el array “costs” (segunda imagen), entonces “costs” seria un array cierto?. La tercera imagen muestra como usé la función optimize(…) en la funciòn model, sospecho que por ahí esta el error. Saludos

The individual cost values are scalars, but the model function returns an array of them: one per every 100 iterations. That is the variable costs. If the number of elements in that array is incorrect (which is what this error message is telling you), it means your code did not run the correct number of iterations that were requested by the parameters being passed into model at the top level. So how could that happen? The most usual cause is “hard-coding” the number of iterations on the call from model to optimize. There should not be any “=” equal signs in the parameter list that you pass to optimize, right? Because if there are, it means you are ignoring the requested values.

Actually you can see the bug in the code you showed. You pass num_iterations=2000, so you will always do 2000 iterations and get 20 elements in the costs array regardless of what the arguments request. You’ve made the same mistake with respect to the other optional parameters to optimize as well.

Muchas gracias Kenb y muchas gracias paulinpaloalto, ya corrió el código. Saludos

Hi, I am getting the same problem. I understand what you mean, but the num_iterations=2000 is present while defining the function model, as shown below:

Similarly when the optimize was defined, num_iterations=100 is given, as shown below.

So should we go ahead and remove the part where we assign it a value of 100 or 2000? Or something else? Or am I misunderstanding entirely?

Yes, I think you must be relatively new to python. Those assignments in the declarations of the two functions just declare the default values of that parameter that will be used if you don’t pass a value for it. If you actually pass a value to model or to optimize, then they use the value you pass. You should not change the definitions of those functions. What you need to do is call them correctly.

I suggest you look at some tutorials about “optional” or “named” parameters in python. Here’s an article that I found on that topic.

I went through that. And yes, I am relatively new to python.
I added a bit of new code. It is showing that the len(['costs]) = 1, and costs = [array(0.15900538)], as shown below:

This is what my approach is. I am hard-stuck here. I am unable to understand how to rectify it. I am sorry, for my persistent lack of understanding.

You have made the same mistake that was described earlier on this thread. Did you read this reply from me earlier?

When you call optimize the way you do, what you are saying is that you are ignoring whatever value of num_iterations was passed into model and always passing the value 2000 for that parameter to optimize. Did you read the article from UC Berkeley that I linked that explains how “optional” parameters work in python?

You are making other errors in the way you call optimize from model as well. Notice that you are using the variables X and Y to pass to optimize, but those are not defined within the “scope” of the model function. So you will end up referencing global variables for those, which will not end well.

The top level point is that calling a function in python is a completely different thing than defining a function in python. You can’t just “copy/paste” the definition of the function as the invocation of the function.

Also note that this specialization is not a beginning python course: it is assumed you already have reasonable competence as a python programmer before you start here. Here’s a thread from a while back that discusses the prerequisites for DLS. If you have good experience in other languages, then you can probably learn python on the fly here, but it will require that you go off and read some tutorials when you hit something new that trips you up like this case. But if the “learn on the fly” strategy is not working for you, then you might want to consider pausing this course and taking a python course first.