C5W1 assignment 2- exercise 4 model() l ist index out of range

looking for help, as i cannot understand this english:

My code has…

…so i think this is why it is failing with:

I dont know what the equivalent of n_e is, if it is not the list [examples]. I dont know what they are looking for here, re the modulus IF ([examples] +1) [examples] = 1 # then there is a remainder of 1

Any help appreciated. :pray:

i have fluked the solution here i think, but dont understand what i did. Could someone please explain the following code?

{moderator edit - solution code removed}

Many thanks

Please read this point again:

For example, if there are n_e examples, and the for-loop increments the index to n_e onwards, think of how you would make the index cycle back to 0, so that you can continue feeding the examples into the model when j is n_e, n_e + 1, etc.

In this assignment, training batch size is 1. Since we have to perform num_iterations batches of training, cycle through the examples once you’re done exhausting the entire training data.

Do you know what the % operator does in python? It is “modulo” operation from arithmetic: it gives you the remainder if you divide j by the number of examples. So, as Balaji says, you repetitively cycle through the set of examples if the number of iterations exceeds the number of examples.

1 Like

Also note that the way I wrote this code idx is an index into examples, not the actual element of examples. But I suppose you could make it work your way depending on how the subsequent lines look.

2 Likes

ok thanks. I know what % is , but clearly not well enough as Ive never used it as an iterator before. i think i could have achieved the same thing with a second (nested) for loop with a 2nd iterator . But its nice clean code.

Sure, there are other ways to implement this, but I would bet you a pint of the finest California IPA that the code would be more complicated than what we can achieve by using %. E.g. you could use an if statement to detect the fact that you’d hit the end of the examples array.

One other general note is that the grader doesn’t look at your source code: it just calls your functions. There are almost always multiple ways to write a given chunk of code, so you can follow your own preferences as long as the returned values are correct. But in ML, the performance of algorithms does still matter, since we are frequently dealing with large datasets and many iterations of training. Even the performance and size of your code can matter in “inference” mode, if you want to be able to run your trained model on an embedded device.