# Sample function not correct \[DLS 5, Week 1, Exercise 2\]

**URL:** <https://community.deeplearning.ai/t/sample-function-not-correct-dls-5-week-1-exercise-2/24105>\
**Category:** Sequence Models\
**Tags:** coursera-platform\
**Created:** [July 29, 2021, 3:44pm UTC](https://community.deeplearning.ai/t/sample-function-not-correct-dls-5-week-1-exercise-2/24105 "2021-07-29T15:44:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kjayathunge](https://avatars.discourse-cdn.com/v4/letter/k/59ef9b/32.png) [@kjayathunge](https://community.deeplearning.ai/u/kjayathunge)\
**Post date:** [July 29, 2021, 3:44pm UTC](https://community.deeplearning.ai/t/sample-function-not-correct-dls-5-week-1-exercise-2/24105/1 "2021-07-29T15:44:53Z")

</div>

Hello all,

I’m stuck on the 2nd programming exercise in DLS 5 ( Dinosaur Island-Character-Level Language Modeling). The problem occurs in `UNQ_C2 - sample()` There are no runtime errors, but the final assertion fails as shown below:

```auto
AssertionError Traceback (most recent call last)
<ipython-input-91-a13d660e654a> in <module>
     19 print("\033[92mAll tests passed!")
     20 
---> 21 sample_test(sample)

<ipython-input-91-a13d660e654a> in sample_test(target)
     15 assert indices[-1] == char_to_ix['\n'], "All samples must end with \\n"
     16 assert min(indices) >= 0 and max(indices) < len(char_to_ix), f"Sampled indexes must be between 0 and len(char_to_ix)={len(char_to_ix)}"
---> 17 assert np.allclose(indices[0:6], [23, 16, 26, 26, 24, 3]), "Wrong values"
     18 
     19 print("\033[92mAll tests passed!")

AssertionError: Wrong values

```

Here’s my code for the sample() function

```auto
{mentor edit: code removed

```

I’ve checked multiple times that the equations a, z and y match up with the instructions, and they do, so I don’t understand why I’m getting a different answer from the grader. Any help would be appreciated!

Thanks!

---

<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:** [July 29, 2021, 4:13pm UTC](https://community.deeplearning.ai/t/sample-function-not-correct-dls-5-week-1-exercise-2/24105/2 "2021-07-29T16:13:57Z")

</div>

Do not use tanh() when you compute z.  
That’s not shown in the equations.

---

<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:** [July 29, 2021, 4:15pm UTC](https://community.deeplearning.ai/t/sample-function-not-correct-dls-5-week-1-exercise-2/24105/3 "2021-07-29T16:15:04Z")

</div>

Also you do not need len(y.ravel(). Just “len(y)” is sufficient.

---

<div class="post-metadata">

**Author:** ![kjayathunge](https://avatars.discourse-cdn.com/v4/letter/k/59ef9b/32.png) [@kjayathunge](https://community.deeplearning.ai/u/kjayathunge)\
**Post date:** [July 29, 2021, 4:21pm UTC](https://community.deeplearning.ai/t/sample-function-not-correct-dls-5-week-1-exercise-2/24105/4 "2021-07-29T16:21:30Z")

</div>

Wow, completely missed that somehow. Thanks!
