Week 1 Assignment 2 Exercise 4 - Vanishing loss and Wrong expected output


All functions prior have passed tests but I keep getting the same error of a vanishing loss and wrong output. I think I might be calculating idx ( (j+1) % len(examples) ) incorrectly but i’m not sure.

1 Like

First, your title says this is assignment 1 but you shared the content from assignment 2. Please correct that.

So, I am not sure which caused you this error but to set the idx correctly, let me give you an example:

Suppose we have 10 examples (dinosaur names) and want to iterate through them one by one. For iteration 0, we want index 0; for iteration 1, index 1; and so on. Let’s say we have 100 iterations and 10 examples, in the loop for j in range(num_iterations):

If we do like idx = j, for loop 0, j is 0 and idx is also 0, for loop 1, j is 1 and idx is also 1, and so on till loop 10 where j becomes 10 but our number of examples is 10 so there is no such idx which is 10. So, we want to make the idx 0 at this point. How?

If I divide 1 by 10, the remainder will be 1, if I divide 2 by 10, the remainder will be 2 and If I divide 10 by 10, the remainder will be 0 and hence the loop begins from 0 again.
Yep, we want this. We want the idx should be the remainder of the current iteration divided by the length of examples.

1 Like

I am setting my idx variable to j % length of examples. Is that not correct? I also keep getting the same error output

This is the expected code for that line in the model() function:
idx = j % len(examples)

The tricky bit in this function is how you calculate these values:

  • single_example
  • single_example_chars
  • single_example_ix
  • X
  • ix_newline
  • Y

So you may have been focusing on the wrong part of the code.

So then the error lies somewhere else in my code…all tests have passed in the previous exercises and I’ve restarted the kernel and cleared output several times. I’ll look at my code and review but if I cant find the error can I send my code to you privately for review?

Update for those who find this thread later.
The code for X = ... and Y = ... needed some adjustments.