Week 2 Exercise 8 cost with wrong value

Hi,

My exercise 8 kept on throwing the below error: to my understanding, it was verifying the initial cost value before iteration. I am wondering how an experienced coder would troubleshoot this error without having to manually compute the cost out of the training set?

AssertionError Traceback (most recent call last)
in
1 from public_tests import *
2
----> 3 model_test(model)

~/work/release/W2A2/public_tests.py in model_test(target)
127 assert type(d[‘costs’]) == list, f"Wrong type for d[‘costs’]. {type(d[‘costs’])} != list"
128 assert len(d[‘costs’]) == 1, f"Wrong length for d[‘costs’]. {len(d[‘costs’])} != 1"
→ 129 assert np.allclose(d[‘costs’], expected_output[‘costs’]), f"Wrong values for d[‘costs’]. {d[‘costs’]} != {expected_output[‘costs’]}"
130
131 assert type(d[‘w’]) == np.ndarray, f"Wrong type for d[‘w’]. {type(d[‘w’])} != np.ndarray"

AssertionError: Wrong values for d[‘costs’]. [array(1.20873842)] != [array(0.69314718)]

1 Like

You may initialize parameters incorrectly or hard-code the learning_rate value. For the former, you have to call initialize_with_zeros function. For the latter, you should not use specific values, so, your code can be tested for any value the grader wants. Same for the number of iterations.

1 Like

Thanks. I did call initialize_with_zeros and passed on first dimension of X_train.

For gradient descent, I passed on X_train and Y_train through optimize.

I toggled different learning_rates but it doesn’t affect the cost value computed (1.20873842), which makes sense to me since iteration 0 %100 == 0, so to my understanding the validation is on the initial cost computed before iteration. Is this the correct understanding?

1 Like

Yes, you’re right that in this case you only see the cost at 0 iterations, so it must be the case that it is just your cost logic that is incorrect. Are you sure that you included two main terms in your cost? You need to handle both the Y = 1 and the Y = 0 case, right? Please compare your cost logic to the math formula they show. Also note that there are two ways to do it: you can just use elementwise multiply followed by np.sum for each of the main terms or you can use dot product, but if you go the dot product route you need a transpose. And there are right and wrong ways to use the transpose. See this thread for examples.

1 Like

Thank you Paul. I tried what I understood from the 2021 post but I am still getting the same cost result for this exercise.

  • My propagate function exercise had passed.
  • I transposed the sigmoid activation result before taking its np.log and the np.log of one minus the activation result.
  • I then used np.dot to multiply the above results with Y and (1-Y) respectively, before summing them using np.sum
  • then * -1/m
1 Like

Sorry, my previous post wasn’t really that useful. I was addressing a different kind of problem. If you are saying that your propagate function passes the tests, then that most likely means that the bug here is in model, not propagate. E.g. somehow you are passing the wrong arguments to optimize or not handling the return values correctly. That’s where you need to look for the bug. A perfectly correct function can give the wrong answer or even “throw” an error if you pass it incorrect parameter values.

Note that there are 3 layers of functions here: model calls optimize and optimize calls propagate. Please look first at your model code and make sure you understand how it calls optimize and handles the return values. My guess is that you are referencing global variables or something like that in your model logic.

1 Like

Thank you Paul. I found the oversight and now I’m able to obtain the correct initial cost value.

I am now encountering the below error for model:
~/work/release/W2A2/public_tests.py in model_test(target)
126
127 assert type(d[‘costs’]) == list, f"Wrong type for d[‘costs’]. {type(d[‘costs’])} != list"
→ 128 assert len(d[‘costs’]) == 1, f"Wrong length for d[‘costs’]. {len(d[‘costs’])} != 1"
129 assert np.allclose(d[‘costs’], expected_output[‘costs’]), f"Wrong values for d[‘costs’]. {d[‘costs’]} != {expected_output[‘costs’]}"
130

AssertionError: Wrong length for d[‘costs’]. 20 != 1

Since model takes in num_iterations=2000, and costs.append every 100th step. it seems that a length of 20 for costs makes sense. Is this a valid check, or am I misunderstanding something?

1 Like

You’ve made one of the most common mistakes on this code: you are invoking optimize incorrectly. Why are you specifying num_iterations = 2000? That means you are hard-coding that value and literally ignoring the actual value passed by the test case into model at the top level. That is the problem here: the test case requested 100 iterations but you ignored that and did 2000. That is why those lengths mismatch.

My guess is that you are probably new to python and don’t understand what the “keyword” arguments mean. I suggest you google “python keyword arguments” and spent a few minutes reading up on how they work.

1 Like

Thank you Paul. My apologies as I had gotten confused there.

1 Like