DSL Course 5, Week 1, assignment 2, exercise 1

I am having trouble with the “out” part of the clipping function. I tried setting it at “gradient” or “gradients[gradient]” and I keep getting error messages. What am I doing wrong?

Specifically, here is my code:
for gradient in gradients:
np.clip(gradient, -abs(maxValue), abs(maxValue), out = gradient)

and the error: UFuncTypeError: ufunc ‘clip’ did not contain a loop with signature matching types (dtype(’<U21’), dtype(’<U21’), dtype(’<U21’)) → dtype(’<U21’)

Thank you!

1 Like

I ended up finding the mistake, thank you.
np.clip(gradients[gradient], …, …, out=gradients[gradient])


np.clip(gradient, -maxValue, maxValue, out=gradient)

That’s what I first tried, as shown in the code above. Do you know why it didn’t work? Thanks!

Perhaps the kernel got confused, and did not import your updated cell.

That’s why we recommend restarting the kernel and re-running all of the cells before you post on the Forums.


exactly I wrote what you mentioned( for gradient in gradients:
np.clip(gradient, -maxValue, maxValue, out = gradient)), but this error appeared.
what’s wrong?
Gradients for mValue=10

TypeError Traceback (most recent call last)
29 print("\033[92mAll tests passed!\x1b[0m")
—> 31 clip_test(clip, 10)
32 clip_test(clip, 5)

in clip_test(target, mValue)
10 gradients = {“dWax”: dWax, “dWaa”: dWaa, “dWya”: dWya, “db”: db, “dby”: dby}
—> 12 gradients2 = target(gradients, mValue)
13 print(“gradients[“dWaa”][1][2] =”, gradients2[“dWaa”][1][2])
14 print(“gradients[“dWax”][3][1] =”, gradients2[“dWax”][3][1])

in clip(gradients, maxValue)
20 # Clip to mitigate exploding gradients, loop over [dWax, dWaa, dWya, db, dby]. (≈2 lines)
21 for gradient in gradients:
—> 22 np.clip(gradient, -maxValue, maxValue, out = gradient)
23 ### END CODE HERE ###

<array_function internals> in clip(*args, **kwargs)

/opt/conda/lib/python3.7/site-packages/numpy/core/fromnumeric.py in clip(a, a_min, a_max, out, **kwargs)
2083 “”"
→ 2084 return _wrapfunc(a, ‘clip’, a_min, a_max, out=out, **kwargs)

/opt/conda/lib/python3.7/site-packages/numpy/core/fromnumeric.py in _wrapfunc(obj, method, *args, **kwds)
56 bound = getattr(obj, method, None)
57 if bound is None:
—> 58 return _wrapit(obj, method, *args, **kwds)
60 try:

/opt/conda/lib/python3.7/site-packages/numpy/core/fromnumeric.py in _wrapit(obj, method, *args, **kwds)
45 except AttributeError:
46 wrap = None
—> 47 result = getattr(asarray(obj), method)(*args, **kwds)
48 if wrap:
49 if not isinstance(result, mu.ndarray):

/opt/conda/lib/python3.7/site-packages/numpy/core/_methods.py in _clip(a, min, max, out, casting, **kwargs)
130 else:
131 return _clip_dep_invoke_with_casting(
→ 132 um.clip, a, min, max, out=out, casting=casting, **kwargs)
134 def _mean(a, axis=None, dtype=None, out=None, keepdims=False):

/opt/conda/lib/python3.7/site-packages/numpy/core/_methods.py in _clip_dep_invoke_with_casting(ufunc, out, casting, *args, **kwargs)
83 # try to deal with broken casting rules
84 try:
—> 85 return ufunc(*args, out=out, **kwargs)
86 except _exceptions._UFuncOutputCastingError as e:
87 # Numpy 1.17.0, 2019-02-24

TypeError: return arrays must be of ArrayType

1 Like

exactly I wrote what you mentioned( for gradient in gradients:

Why are you using ‘gradients’?
The instructions say this:
...loop over [dWax, dWaa, dWya, db, dby]

1 Like