# C5W1 A2-Dinosaurus\_Island EX-4 | list index out of range

**URL:** <https://community.deeplearning.ai/t/c5w1-a2-dinosaurus-island-ex-4-list-index-out-of-range/235552>\
**Category:** Sequence Models\
**Tags:** coursera-platform\
**Created:** [November 27, 2022, 5:18pm UTC](https://community.deeplearning.ai/t/c5w1-a2-dinosaurus-island-ex-4-list-index-out-of-range/235552 "2022-11-27T17:18:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vsalas](https://avatars.discourse-cdn.com/v4/letter/v/41988e/32.png) [@vsalas](https://community.deeplearning.ai/u/vsalas)\
**Post date:** [November 27, 2022, 5:18pm UTC](https://community.deeplearning.ai/t/c5w1-a2-dinosaurus-island-ex-4-list-index-out-of-range/235552/1 "2022-11-27T17:18:41Z")

</div>

When coding the model() function, I have defined the labels Y to be the same as X but shifted one index to the left and have added a newline character. But I am stucked with the error " list index out of range" in the rnn\_forward function called by the optimize function.

Please, could you advise on what to modify in my code.

* * *

IndexError Traceback (most recent call last)  
Input In [114], in \<cell line: 1\>()  
----\> 1 parameters, last\_name = model(data.split(“\n”), ix\_to\_char, char\_to\_ix, 22001, verbose = True)  
3 assert last\_name == ‘Trodonosaurus\n’, “Wrong expected output”  
4 print(“\033[92mAll tests passed!”)

Input In [113], in model(data\_x, ix\_to\_char, char\_to\_ix, num\_iterations, n\_a, dino\_names, vocab\_size, verbose)  
53 single\_example\_chars = [c for c in single\_example]  
54 single\_example\_ix = [char\_to\_ix[c] for c in single\_example\_chars]  
—\> 55 X = rnn\_forward([None] + single\_example\_ix, single\_example\_ix, a\_prev, parameters, vocab\_size = 27)  
57 # Set the labels Y (see instructions above)  
58 ix\_newline = char\_to\_ix[‘\n’]

File ~\OneDrive\Documents\data\_scientist\FORMATION\DS\Coursera\deeplearning specialization\C5\_Sequence Models\S1\_Recurrent Neural Networks\TP02\utils.py:102, in rnn\_forward(X, Y, a0, parameters, vocab\_size)  
99 a[t], y\_hat[t] = rnn\_step\_forward(parameters, a[t-1], x[t])  
101 # Update the loss by substracting the cross-entropy term of this time-step from it.  
 → 102 loss -= np.log(y\_hat[t][Y[t],0])  
104 cache = (y\_hat, a, x)  
106 return loss, cache

IndexError: list index out of range

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [November 27, 2022, 10:37pm UTC](https://community.deeplearning.ai/t/c5w1-a2-dinosaurus-island-ex-4-list-index-out-of-range/235552/2 "2022-11-27T22:37:10Z")

</div>

> [@vsalas](#):
>
> X = rnn\_forward([None] + single\_example\_ix, single\_example\_ix, a\_prev, parameters, vocab\_size = 27)

The instructions do not tell you to use rnn\_forward() here. The instructions just point out why [None] has a special value in this model.

The key instruction here is is the second bullet: to prepend [None] to the list of example indices.

 ![image](https://global.discourse-cdn.com/dlai/original/3X/d/8/d896af6d2aa8ae6a3070fe99fc8b34f65a22c74b.png)

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [November 27, 2022, 10:40pm UTC](https://community.deeplearning.ai/t/c5w1-a2-dinosaurus-island-ex-4-list-index-out-of-range/235552/3 "2022-11-27T22:40:46Z")

</div>

rnn\_forward() is used indirectly later in this function, when it calls optimize().

---

<div class="post-metadata">

**Author:** ![vsalas](https://avatars.discourse-cdn.com/v4/letter/v/41988e/32.png) [@vsalas](https://community.deeplearning.ai/u/vsalas)\
**Post date:** [November 28, 2022, 10:08am UTC](https://community.deeplearning.ai/t/c5w1-a2-dinosaurus-island-ex-4-list-index-out-of-range/235552/4 "2022-11-28T10:08:03Z")

</div>

Thanks for your comment.  
I have removed the rnn\_forward) function here and have defined only X, adding [None] as a flag.

My code is now running and I get “AssertionError: Wrong expected output”
