# Week 2 exercise 6: Optimization

Need some tips identifying the problem. I get this error when running the tests:

## w = [[0.80956046] [2.0508202 ]] b = 1.5948713189708588 dw = [[ 0.17860505] [-0.04840656]] db = -0.08888460336847771 Costs = [array(0.15900538)]

AssertionError Traceback (most recent call last)
in
7 print("Costs = " + str(costs))
8
----> 9 optimize_test(optimize)

~/work/release/W2A2/public_tests.py in optimize_test(target)
63 assert type(costs) == list, “Wrong type for costs. It must be a list”
64 assert len(costs) == 2, f"Wrong length for costs. {len(costs)} != 2"
—> 65 assert np.allclose(costs, expected_cost), f"Wrong values for costs. {costs} != {expected_cost}"
66

AssertionError: Wrong values for costs. [array(5.80154532), array(1.05593344)] != [5.80154532, 0.31057104]

What am I doing wrong here?

I solved the problem. It seems that instead of passing the learning_rate as variable name, I was providing a value. Even though the values were same apparently, the variable name works while rigid float values don’t.

I’m glad to hear that you found the solution under your own power. The point is it is always a bad idea to hard-code the values: it doesn’t work if the other test cases passes a different value.

1 Like

Hi dear Paul, I just solved the same problem, perhaps it’s silly, but I still want to ask, why are the results using learning rate and using 0.009 are different? the w and b also iterate?
and how do we set the number of iterations and learning rate in other cases?
what does “print_cost=False” mean?

1 Like

The learning rate controls how big a step you take in the opposite direction of the gradient for each iteration. Look at the logic in optimize and what it does with the learning rate. Prof Ng talked about this in the lectures also. It is this lecture starting at about 4:00. You did watch it, right? A large learning rate may converge faster, but it may also “overshoot” and cause oscillation of divergence. There is no fixed recipe for what the right combination is of learning rate and number of iterations. These are what Prof Ng calls “hyperparameters”, meaning values that you have to choose through experimentation. How to approach such choices in a systematic way will be a major topic in Week 1 of Course 2 in this series, so stay tuned for that. He can’t cover everything in one course.

As to what the print_cost flag does, it’s pretty much what you would think from the name, right? Take a look at the code: it’s not complicated and it’s all there for you to see. “Use the Force: read the Source!”

1 Like

I just started week 1 of course 2 and can relate to what you are trying to say regarding the hyper-parameters. I still don’t understand why the AssertionError exists when I hard code the value. I mean I would have understood if the response was something like:

Wrong values for costs. [5.80154532, 1.05593344] != [5.80154532, 0.31057104]
but, it looks significantly different.

Sorry, I don’t understand your point. The assertion you show is exactly what you got, right? You show it in the original post on this thread.

Or if you are saying that you are talking about a different exercise, then the question is incomplete because you haven’t shown what the other assertion failure looks like (the one you expected to be like the one here). But the larger point is that all the tests in each assignment are written specifically for that assignment and maybe they wrote that one differently. You can look at the tests. Just click “File → Open” and then open the file public_tests.py. With computer programs, things don’t just happen for mysterious reasons. If you don’t understand why it gives a certain result, that simply means you haven’t studied the source code carefully enough yet.

Hi, this is interesting because I am also assigning the learning rate to a variable but still get the ‘Wrong values for cost. [array(A),array(B)] != [A,C]’ error.

It all depends on what you mean by that. When you call `optimize` from `model`, there should be no equal signs in the parameter list. If there are, that means you are over-riding the requested values. Just pass `learning_rate` as is.