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

Hello,

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

Hello!
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.

Experiencing a similar problem.

The values of β€œsingle_example” etc seem to be fine, but the output is incorrect:

j = 0 idx = 0
single_example = turiasaurus
single_example_chars [β€˜t’, β€˜u’, β€˜r’, β€˜i’, β€˜a’, β€˜s’, β€˜a’, β€˜u’, β€˜r’, β€˜u’, β€˜s’]
single_example_ix [20, 21, 18, 9, 1, 19, 1, 21, 18, 21, 19]
X = [20, 21, 18, 9, 1, 19, 1, 21, 18, 21, 19]
Y = [20, 21, 18, 9, 1, 19, 1, 21, 18, 21, 19, 0]

Iteration: 0, Loss: 23.084047

single_example = turiasaurus
single_example_chars = [β€˜t’, β€˜u’, β€˜r’, β€˜i’, β€˜a’, β€˜s’, β€˜a’, β€˜u’, β€˜r’, β€˜u’, β€˜s’]
single_example_ix = [20, 21, 18, 9, 1, 19, 1, 21, 18, 21, 19]
Nkzxwtdmfqoeyhsqwasjkjvu
Kneb
Kzxwtdmfqoeyhsqwasjkjvu
Neb
Zxwtdmfqoeyhsqwasjkjvu
Eb
Xwtdmfqoeyhsqwasjkjvu

j = 1535 idx = 1535
j = 1536 idx = 0
Iteration: 2000, Loss: 8.299936

single_example = kentrurosaurus
single_example_chars = [β€˜k’, β€˜e’, β€˜n’, β€˜t’, β€˜r’, β€˜u’, β€˜r’, β€˜o’, β€˜s’, β€˜a’, β€˜u’, β€˜r’, β€˜u’, β€˜s’]
single_example_ix = [11, 5, 14, 20, 18, 21, 18, 15, 19, 1, 21, 18, 21, 19]
Mmyyyyggggggyuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
Jmllggkyoooooooooooooooooooooooooooooooooooooooooo
Kyyyyggggggyuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
Mmdbdkymeeeeeeeeeeeeeppppppppppppppppppppppppppppp
Yyyyggggggyuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
Ddaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Xyyggggggyuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

Iteration: 4000, Loss: 2.055703

single_example = barosaurus
single_example_chars = [β€˜b’, β€˜a’, β€˜r’, β€˜o’, β€˜s’, β€˜a’, β€˜u’, β€˜r’, β€˜u’, β€˜s’]
single_example_ix = [2, 1, 18, 15, 19, 1, 21, 18, 21, 19]
Mmmmmmmmmmmmmmmmmmmmmmmmjkwkzkkbbbbxbbbbbbbbbbwvhh
Kkkbaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Kzyyyyyyyyyyyyyyfwkkkwwaaaaaaaaaaaaaaaaaaaaaaaaaaa
Mmmbblllllllllllllllllllllllllllllllllllllllllllll
Zyyyyyyyyyyyyyyfwkkkwwaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
Xxxbbbbbbyyyyyfwkkkwwaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

…

Iteration: 20000, Loss: 0.045345

single_example = nothronychus
single_example_chars = [β€˜n’, β€˜o’, β€˜t’, β€˜h’, β€˜r’, β€˜o’, β€˜n’, β€˜y’, β€˜c’, β€˜h’, β€˜u’, β€˜s’]
single_example_ix = [14, 15, 20, 8, 18, 15, 14, 25, 3, 8, 21, 19]
Qqzzzzzzzzzzzzzzzwwwwwwwffjjwwwffffuuuuuuuuuuuuuuu
Kkkkkkkkkkkkkkkkkkkkkkwwwwwqfuuuuuuuuuuuuuuuuuuuuu
Llllllllllllllllllllllllllllllllllllllllllllllllll
Qiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
Zzzzzzzzzzzzzzzwwwwwwwffjjwwwffffuuuuuuuuuuuuuuuuu
Fffffqjjjjjjjjjjoooooooooooooooooooooooooooooooooo
Yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

Iteration: 22000, Loss: 0.034120

single_example = nedoceratops
single_example_chars = [β€˜n’, β€˜e’, β€˜d’, β€˜o’, β€˜c’, β€˜e’, β€˜r’, β€˜a’, β€˜t’, β€˜o’, β€˜p’, β€˜s’]
single_example_ix = [14, 5, 4, 15, 3, 5, 18, 1, 20, 15, 16, 19]
Qqzzzzzzzzzzzzzzzwwwwwwwffffwwwffffqffffffffffqqjj
Llllllllllllllllllllllllllllllllllllllllllllllllll
Llllllllllllllllllllllllllllllllllllllllllllllllll
Qjjffffffffjjjjjjjjjjwwwwwqfssssssssssssssssssssss
Zzzzzzzzzzzzzzzwwwwwwwffffwwwffffqffffffffffqqjjjj
Ffffffffffjjjjjjjjjjwwwwwqfsssssssssssssssssssssss
Xxxxxxxxxxxxxxwwwwwwwffffwwwffffqffffffffffqqjjjjj


AssertionError Traceback (most recent call last)
in
1 parameters, last_name = model(data.split(β€œ\n”), ix_to_char, char_to_ix, 22001, verbose = True)
2
----> 3 assert last_name == β€˜Trodonosaurus\n’, β€œWrong expected output”
4 print(β€œ\033[92mAll tests passed!”)

AssertionError: Wrong expected output

Hints are appreciated!

Sorry, I’m unable to reply on this thread in depth at this time. Hopefully another mentor will reply, or you might consider starting a new thread.