W2 Ex 8 - compiling the model

Hi! When testing the model function in Exercise 8 I get the error shown bellow. My question is: why is there an assertion to make Len(costs) = 1? I would have thought that if we run the model for 2000 iterations, and make a note of the cost at every 100 iterations, we should end up with 20 values in an array.

Any help will be much appreciated! Thanks!

Yes, but the point is that you shouldn’t always be running 2000 iterations: you should be running the number that the test case requested. So how did that happen? One of the most common mistakes on this function is to “hard-code” the parameter values that you are passing when you call optimize from model. There should be no assignments in the parameter list there: you should simply pass through the values that are being requested at the top level of the model function.

If you are new to the concept of “named parameters” in python, it would be worth reading up on that. There are two kinds of parameters in python: “positional parameters”, which are required on every call and “named parameters” which are optional. The named parameters (also sometimes called “keyword parameters”) are declared with a default value in the definition of the function, which is what will be used if they are not passed on a given invocation of the function. But if a value is passed for the parameter, then you should be using the value that was passed, not the default.

Here’s the definiton of the model function that we are given in the template code:

def model(X_train, Y_train, X_test, Y_test, num_iterations=2000, learning_rate=0.5, print_cost=False):

That does NOT mean that the value of the number of iterations is always going to be 2000. That means that it will be 2000 only in cases in which no value is provided on the function call for the num_iterations parameter.

@paulinpaloalto Thank you very much for this detailed and timely answer! It helped me get the function to work properly.