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