C5 W1 Assignment 2 exercise 4: the model()

Hi all,

I am setting my idx this way: idx = (len(examples)+j)%len(examples); but getting an IndexError.
I believe the other part of the exercise is well coded.

I would appreciate if anyone could help me solve this.
Many thanks

Hi @Bertrand_T_Tameza,

Here is how to do the recycling:
idx = j % len(examples)

2 Likes

Thank you, @Kic. I followed the hint you suggested, but still getting the same error message. I think the bug lies somewhere else. I’m confused.

Hi @Bertrand_T_Tameza ,

Indeed, there is problem somewhere else. The IndexError is raised when running rnn_forward(). This rnn_forward() is an utility function, the code is provided for you. It could be somewhere a wrong value is picked up. Try refresh the kernel and clear all output, rerun the code from start.

I have changed the title of the thread, because the assert log shows you’re working on Assignment 2, not Assignment 1.

Hi @TMosh, Yes I am currently working on assignment 2. But the thread is about assignment 1. It is simply because I got stuck on Assign 1 that I wanted to move forward and start Assignment 2. I have both Assign 1 and 2 currently opened. I tried the tips provided by @Kic and unfortunately it didn’t work, that is why I decided to not stay at the same place.

When you have a new question, you really should start a new thread for it. Nothing in this thread refers to C5 W1 A1 at all. The image in the OP refers to W1A2. The “model()” function is in A2.

The ‘t’ value in utils:rnn_forward() comes from range(len(X)).

So there is most likely a problem with your code for setting the “X” variable in the model() function.

model() calls optimize(), which calls rnn_forward().

Thank you @TMosh for the hint. Actually, the thread is related to the notebook “Dinosaurus_Island_Character_level_language_model” ( Exercise 4 - model), which I referred to as c5w1 A1.

This is how I coded X and Y respectively:
[None] + single_example_chars and
[X[i+1] for i in range(len(X)-1)] + [ix_newline]

Will there be an improvement please?

1 Like

The dino island assignment is C5 W1 A2.

[None] + single_example_chars
That is not correct.

[X[i+1] for i in range(len(X)-1)] + [ix_newline]
That is not correct.

Hi @TMosh,

Apologies for the confusion and many thanks for correcting the title of the thread.

For X, I wanted to prepend the list [None] to the input characters as per the instruction.
Thank you also for labeling my coding as incorrect, I really appreciate the hint.

And for Y, using list comprehension, I set Y to be one-step ahead of X and ending the list by adding the index of the newline character.

Am I misunderstanding the instruction, or don’t using the right inputs?
I basically used the + operator instead of appendleft() and append() (respectively) following the advice provided in the instructions.

For X, you use the character index numbers, not the characters.

For Y, you start from X[1:] - that’s how you skip the [None] character - and add ix_newline at the end.

Using the + operator to concatenate the lists is fine.

Many thanks!

I was confused by this sentence in the instruction for setting X ‘’ Prepend the list [ None ] in front of the list of input characters.’’, reason why I used the list of characters instead of their corresponding index numbers in the dictionary.

I retrieved the indices from the dictionary like this:
[None] + [single_example_ix[ch] for ch in single_example_ix.keys()], prepending [None] to form the list X. I did so because I could not concatenate the list [None] with the dictionary single_example_ix

For Y, I did this: X[1:] + [ix_newline]

With the above the algorithm runs, but not outputting the expected result.

Could anyone, please, provide more hints, I am basically stuck.

You’re making “X” a bit too complicated.

First you need to get the correct values for single_example_ix. There is a print output in the notebook for that.

Then you just need
X = [None] + single_example_ix

My single_example_ix is a dictionary, which I constructed from the single_example_chars list, following something like { ch:i for i,ch in enumerate(chars) }

So when I set X = [None] + single_example_ix, I get an error that I can’t concatenate a list and a dictionary.

It should not be a dictionary. it should be a list of word indices.

Still confused, really want to find a way out.

I fist set it like this (triying to construct the list of character index numbers using the single_example_chars list):

{mentor edit: code removed}
and it didn’t work

Secondly, like this (using char_to_ix):

{mentor edit: code removed}
and it did not work too.

Actually, the instruction says we should use char_to_ix. But char_to_ix is a global variable already set. So when using it to set single_example_ix, we do not use single_example_chars.

Or maybe I should combine both char_to_ix and single_example_chars to set single_example_ix? how?

Thanks a lot @TMosh for assisting.

I wanted to make another comment if I really understand what is going on, and maybe it can also help someone struggling at the same stage.

char_to_ix is a global variable (a dictionary containing all the English characters along with their corresponding index numbers) and the single_example_ix list should be constructed directly from indices in that dictionary, but using characters from single_example_chars.

I was actually trying to construct new indices from single_example_chars using the enumerate() function; which was my mistake.

Thanks, that was a key helper comment!