Course 2, week 2, optimization, minibatches, test error: "wrong values"

For function random_mini_batches(), I failed the test for partitioning for ‘wrong values’, see below test outputs. I used the same random seed (it is 0). I then tried my algorithm on a smaller problem I created (nx=3, m=7) and got the expected results for the “mini_batches”. Suggestion for why the values are incorrect is welcome.

That assertion is failing not because of the shapes of your minibatches, but because of the contents. Did you modify the code that generates the input values to the original X in this test case? All the values should be positive integers, but you can see that your 3 “sanity check” values are positive and negative real numbers. You should not be performing any computations on the contents of X and Y, just splitting them into mini-batches. If you carefully examine the assertion line that “throws”, you can see what those values should be: 294912, 86016 and 454656. So how did you end up with 1.62434536 and so forth?

Paul -

Thanks again. I did realize that it’s the content that is wrong. Per your question:

  • Did you modify the code that generates the input values to the original X in this test case?

No, I did not. There is a small chance I may have typed in this box by mistake, though unlikely. In case this is what happened, I do not have a way to know what the correct values should be. Below are the current screenshots. You can see that the values of the X are integers before and after the first test:

In the second test, a NEW set of X and Y values were generated, X being random Gaussian rather than integers:
image

Finally, my code is correct in splitting the data because (1) I completed the rest of this assignment which calls this function and got all expected results; (2) when tested on a small example of my own, the result is correct:

Following your suggestion, I’m ‘relearning’ Python but it does not appear to be a python problem.

Thanks again!

I found my mistake! In the splitting code, I used ‘X’ and ‘Y’ rather than ‘shuffled_X’ and ‘shuffled_Y’. This explains everything. I’m really just getting old. Thanks again.

3 Likes

That’s great news that you found the issue! You had me worried there for a while. :scream_cat: Onward! :nerd_face:

Paul -

I greatly appreciate your prompt feedback. They are very helpful. I used to be a better programmer but after years of being a science manager (with students doing the hands-on work), I have lost the touch. Sigh… But, move on I will.

Ye

Hi, I have a same error, but I use shuffled_X and shuffled_Y, but appears that error, I Don’t know if I am puting wrong the formulas, because I don’t use global variables, some of you can help me please.

[Removed solution code]


I think the problem is in your handling of the last partial “minibatch”. Notice that your second index starts with “:”. That means you are starting from 0. I don’t think that’s what you intended. Your last batch is the right shape, but it contains the wrong values.

1 Like

Thank you so very much by help me, I don’t had see that error :smiley:

1 Like

My test for random_mini_batches is passing all assertions when run through the Coursera Jupyter environment, but when I downloaded my completed notebook and ran it locally the tests failed for random_mini_batches. Any idea why this would happen?

Note: everything else worked fine locally.


1 Like

You saved me too man! I’ve spent more than an hour searching for my error. Thanks a lot!

1 Like

Exactly the same thing is happening to me right now.

I get an AssertionError if I run it in VSCode, but it works fine in Coursera’s online environment… that’s weird!

1 Like

That may be caused by using different versions of the various packages. The courses use a configuration from April of 2021, but things change pretty quickly in this space.

For those of us that are married to VSCode, it would be really helpful to have an virtual enviroment file so that issues like this dont stump us

It’s a worthwhile suggestion, but the point to keep in mind is how many external tools like that are there? Do the course staff have to accommodate all such third party tools that aren’t actually part of the course? How about PyCharm? How about Spyder? How about Google Colab? They have given you a working environment. If you want to use any of the many different potential environments out there, then you need to have the skills to figure out how to make that work. It’s not reasonable to expect the course staff or the mentors to be your personal IT support staff for things that are beyond the scope of the course.

But maybe the better hope is that there are other VSCode users listening here and maybe some of them have figured this out and can share the type of environment file you need. Don’t get me wrong: it’s a completely fine question to ask, but the right way to consider it would be as a community support issue, not something the course staff or the mentors are required to act on …

I personally am not familiar with VSCode, so I don’t know the format of the information that you would need. But there have been various threads discussing how to extract the version information for the packages in the assignments here. Here’s a good one to start with and here’s another one with good links.

1 Like

Thats very fair. tbh it looks like this only affected 3 of us and it only took me around 30 mins to figure this out.

Come to think of it most people doing this course are probably older STEM / business people so including technical details about editors could be damaging.

Thank you for the links anyway. Really appreciate you going the extra mile

If you have figured out how to do this for VSCode users, you could post the info in a separate thread. E.g. under DLS Resources or the like. Give it a title like “How to run the assignment code in VSCode” or whatever you think would be most meaningful to a VSCode user like yourself. It could very well help other folks in the future, in the same spirit as those other threads I linked, which were created by fellow students as well.

If you do create something like that, please “DM” me a link to it and I’ll “bookmark” it in case I see questions about it in the future.

Cheers,
Paul

1 Like