C2_W1_Assignment - error exercice 4

In the last exercice 4, from week 1 of Optimizing Functions of One Variable: Cost Minimization, I got an error message when i am running the dldOmega_of_omega_array function, about TypeError: len() of unsized object

Some of you do you know why ? Apparently the len and grad functions are not compatible…

Here the function:

This is organised as a function only for grading purposes.

def dLdOmega_of_omega_array(omega_array, pA, pB):
N = len(omega_array)
###N = omega_array.shape[0]
print(omega_array.shape)
dLdOmega_array = np.zeros(N)

for i in range(N):
    ### START CODE HERE ### (~ 2 lines of code)
    dLdOmega = grad(L_of_omega_array)(omega_array[i], pA, pB)
    print (dLdOmega)
    dLdOmega_array = dLdOmega_array.at[i].set(dLdOmega)
    ### END CODE HERE ###
    
return dLdOmega_array

dLdOmega_array = dLdOmega_of_omega_array(omega_array, prices_A, prices_B)

Here the error:

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 [33], line 17
13 ### END CODE HERE ###
15 return dLdOmega_array
—> 17 dLdOmega_array = dLdOmega_of_omega_array(omega_array, prices_A, prices_B)

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

[... skipping hidden 10 frame]

Cell In [29], 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

You are not allowed to post code solutions publicly.

About your error did you check is omega_array a python list? And also more specifically maybe it is a numpy array or someother kind of array! Im not familiar with this course though.

(Solution code removed, as posting it publicly is against the honour code of this community, regardless if it is correct or not. You can share the errors you get)

here the error

(Solution code removed, as posting it publicly is against the honour code of this community, regardless if it is correct or not. You can share the errors you get)

Hi @Sebastien_MANDON,

As @gent.spah mentioned earlier, please don’t post your solution code publicly as it is against the honour code of this Forum.

Thanks,
Mubsi

hello sorry about that, I found my mistake thank you for the support