C2_W1 Exercise 4 Error

HI there,

I think I got the code right but I keep getting the error as below. Please help me.


IndexError Traceback (most recent call last)
File /opt/conda/lib/python3.8/site-packages/jax/core.py:1427, in ShapedArray._len(self, ignored_tracer)
1426 try:
→ 1427 return self.shape[0]
1428 except IndexError as err:

IndexError: tuple index out of range

The above exception was the direct cause of the following exception:

TypeError Traceback (most recent call last)
Cell In [41], line 14
10 ### END CODE HERE ###
12 return dLdOmega_array
—> 14 dLdOmega_array = dLdOmega_of_omega_array(omega_array, prices_A, prices_B)

Cell In [41], line 8, in dLdOmega_of_omega_array(omega_array, pA, pB)
4 dLdOmega_array = np.zeros(N)
6 for i in range(N):
7 ### START CODE HERE ### (~ 2 lines of code)
----> 8 dLdOmega = grad(L_of_omega_array)(omega_array[i], pA, pB)
9 dLdOmega_array = dLdOmega_array.at[i].set(dLdOmega)
10 ### END CODE HERE ###

[... skipping hidden 10 frame]

Cell In [36], line 8, in L_of_omega_array(omega_array, pA, pB)
7 def L_of_omega_array(omega_array, pA, pB):
----> 8 N = len(omega_array)
9 L_array = np.zeros(N)
11 for i in range(N):
12 ### START CODE HERE ### (~ 2 lines of code)

[... skipping hidden 1 frame]

File /opt/conda/lib/python3.8/site-packages/jax/core.py:1429, in ShapedArray._len(self, ignored_tracer)
1427 return self.shape[0]
1428 except IndexError as err:
→ 1429 raise TypeError(“len() of unsized object”) from err

TypeError: len() of unsized object

Your error is stating
len(omega_array) is of unsized object.

So check if there is a any kind of type error while recalling the dLdOmega_array code

This can happen if you are using any incorrect function, like trying to recall an object as list where as the codes wants you to recall it as dictionary method with assigned arguments or function keys

But dLdOmega_array is the Exercise 3 and I passed it. Could you take a look at my code?

here!!!

ok it works now. I thought my job was to only replace the “none” and wasnt allowed to add any line of code.

I had to add one line of code above to use the grad function first to make it work

2 Likes

Great You debug your code yourself!!!

Keep learning!!!
DP

1 Like

Thank you for the quick response and your help!

Hi All, I am getting same error, could you please tell me how to resolve it?

I added this import statement

from jax import grad

still same error.

please create a separate topic for your issue, even your error seems similar.

Also while posting take a screenshot of the error rather than copy paste and make sure not to post any codes.

You are also not suppose to add any import statement by yourself as the assignment are already provided with the necessary libraries required you to use while writing codes.