Week 1 Dinosaur Assignment Ex.1

Hi,
It seems like the default code has some issues with one of the for loops. Before, I was getting this error

TypeError: builtin_function_or_method is not iterable Solution

for this for loop:

for grad in gradients2.keys:
valuei = gradients[grad]
valuef = gradients2[grad]
mink = np.min(valuef)
maxk = np.max(valuef)
assert mink >= -abs(mValue), f"Problem with {grad}. Set a_min to -mValue in the np.clip call"
assert maxk <= abs(mValue), f"Problem with {grad}.Set a_max to mValue in the np.clip call"
index_not_clipped = np.logical_and(valuei <= mValue, valuei >= -mValue)
assert np.all(valuei[index_not_clipped] == valuef[index_not_clipped]), f" Problem with {grad}. Some values that should not have changed, changed during the clipping process."

I had to change it to keys() to make it work.

If this fix is not supposed to work - then perhaps I wonder why .keys should be correct? Thanks

Do not modify the code in the test functions. Doing that just masks errors in your function code.

Or, you need to restart the kernel and run the notebook cells again.

Hi, maybe you can enlighten me as to why i was getting an error. I tried refreshing the kernel but it didn’t work.

with .keys, the error reads the following:

Gradients for mValue=10
gradients["dWaa"][1][2] = 10.0
gradients["dWax"][3][1] = -10.0
gradients["dWya"][1][2] = 0.2971381536101662
gradients["db"][4] = [10.]
gradients["dby"][1] = [8.45833407]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-73d1a4f3ccd5> in <module>
     29     print("\033[92mAll tests passed!\x1b[0m")
     30 
---> 31 clip_test(clip, 10)
     32 clip_test(clip, 5)
     33 

<ipython-input-6-73d1a4f3ccd5> in clip_test(target, mValue)
     17     print("gradients[\"dby\"][1] =", gradients2["dby"][1])
     18 
---> 19     for grad in gradients2.keys:
     20         valuei = gradients[grad]
     21         valuef = gradients2[grad]

TypeError: 'builtin_function_or_method' object is not iterable

My clip function works fine in the later sections of the assignment. I know that I can’t post the exact code here, but basically what I did is to iterate through a list of the exploding gradients as suggested, and then used numpy.clip. I used the out= parameter.

As far as I understand, the function generates a dictionary object. The .keys() method returns the keys in a dictionary object. I dont think .keys works for dictionaries.

What exactly do you mean by “with .keys”?

You can see that in the error message. The original test function has the following for loop:

for grad in gradients2.keys:

“.keys” refers to this call of an attribute called “keys” - but it does not seem like that is how you would call the keys in a dictionary…

This is how it looks in my copy of the notebook:

I think maybe your notebook was accidentally edited to remove the ().

OK Thanks. Glad to know that is how it’s supposed to be.