Random_mini_batches

W2 E1
in random_mini_batches function i’m geting an error
AssertionError: Wrong shape in the last minibatch. (12288, 127) != (12288, 20)


please assistance
All the best

It should be clear where to look for the error, right? It is your indexing or “slicing” operation on the second dimension in the “partial last batch” case.

The second index in that range is incorrect, because indexing in python is zero based: using m - 1 will cause you to omit the last element. So that’s why it turns out to be 127 instead of 128. But maybe the more critical point is that the starting point of the range is not correct. Think about what that will give you:

m - mini_batch_size*num_complete_minibatches

So that is the total number of samples (148 in this case), minus the number included in the full batches (2 * 64 = 128). So it gives you 20 in this case. That is not the correct starting point for the last partial batch, right?

Awesome! Woke up in the morning and read your feedback and solved it on the first try.
Thanks a lot :slight_smile:

I’m getting similar errors but still couldn’t figure out how my code is wrong after reading all threads on this exercise multiple times… help!


AssertionError Traceback (most recent call last)
in
18
19 assert np.allclose(mini_batches[0][0][0][0:3], [294912, 86016, 454656]), “Wrong values. Check the indexes used to form the mini batches”
—> 20 assert np.allclose(mini_batches[-1][0][-1][0:3], [1425407, 1769471, 897023]), “Wrong values. Check the indexes used to form the mini batches”
21
22 print(“\033[92mAll test passed!”)

AssertionError: Wrong values. Check the indexes used to form the mini batches

shape of the 1st mini_batch_X: (12288, 64)
shape of the 2nd mini_batch_X: (12288, 64)
shape of the 3rd mini_batch_X: (12288, 20)
shape of the 1st mini_batch_Y: (1, 64)
shape of the 2nd mini_batch_Y: (1, 64)
shape of the 3rd mini_batch_Y: (1, 20)
mini batch sanity check: [ 0.90085595 -0.7612069 0.2344157 ]
2 Tests passed
1 Tests failed

AssertionError Traceback (most recent call last)
in
10 print ("mini batch sanity check: " + str(mini_batches[0][0][0][0:3]))
11
—> 12 random_mini_batches_test(random_mini_batches)

~/work/release/W2A1/public_tests.py in random_mini_batches_test(target)
72 ]
73
—> 74 multiple_test(test_cases, target)
75
76 def initialize_velocity_test(target):

/opt/conda/lib/python3.7/site-packages/dlai_tools/testing_utils.py in multiple_test(test_cases, target)
162 print(‘\033[91m’, len(test_cases) - success, " Tests failed")
163 raise AssertionError(
→ 164 “Not all tests were passed for {}. Check your equations and avoid using global variables inside the function.”.format(target.name))

AssertionError: Not all tests were passed for random_mini_batches. Check your equations and avoid using global variables inside the function.

Hi, Kimberly.

It looks like all three batches are the correct shape with your code. If you look at the assertions, you can see that your first minibatch has the correct elements (that assertion came first and didn’t “throw”). The failure is on the last minibatch. So the place to look as at how you compute the start index value for the last minibatch. We have 148 samples with a batch size of 64. So the first batch starts at index 0, the second at 64 and the last one at 128, right? Try printing your “starting index” values and see if they match what we expect.

1 Like

thank you for the pointer - I fixed the equation for the starting index and passed all tests!

That’s great to hear! Nice work!

Hello,
my code also didn’t pass the second test, already checked the “starting index” which is fine.

shape of the 1st mini_batch_X: (12288, 64)
shape of the 2nd mini_batch_X: (12288, 64)
shape of the 3rd mini_batch_X: (12288, 20)
shape of the 1st mini_batch_Y: (1, 64)
shape of the 2nd mini_batch_Y: (1, 64)
shape of the 3rd mini_batch_Y: (1, 20)
mini batch sanity check: [ 0.90085595 -0.7612069 0.2344157 ]
1 Tests passed
2 Tests failed

AssertionError Traceback (most recent call last)
in
10 print ("mini batch sanity check: " + str(mini_batches[0][0][0][0:3]))
11
—> 12 random_mini_batches_test(random_mini_batches)

~/work/release/W2A1/public_tests.py in random_mini_batches_test(target)
72 ]
73
—> 74 multiple_test(test_cases, target)
75
76 def initialize_velocity_test(target):

/opt/conda/lib/python3.7/site-packages/dlai_tools/testing_utils.py in multiple_test(test_cases, target)
162 print(‘\033[91m’, len(test_cases) - success, " Tests failed")
163 raise AssertionError(
→ 164 “Not all tests were passed for {}. Check your equations and avoid using global variables inside the function.”.format(target.name))

AssertionError: Not all tests were passed for random_mini_batches. Check your equations and avoid using global variables inside the function.

Thank you!

Welcome to the community!

There are observations from your outputs.

  1. If a batch size is 64, it seems to work from a list of print lines. The shape is correct, and the value (from a sanity check) is correct.
  2. But, you failed a hidden test which has different parameters like batch size. In here, three types of test is conducted. Those are ‘data type check’, ‘output shape check’ and ‘output value check’. Most likely, you passed ‘data type check’, but did not for ‘output shape and value’.

In the hidden test, even your output shape is not correct. This means, the most possible case is you may use a hard coded value, like 'mini_batch_size = 64" or something similar. A hidden test uses a different value.

Please ensure that you do not use any hard coded variables in your function.

Hello Nobu Asai,
I didn’t use hard coded variables, but there was an error in the definition of “number of examples in the final mini-batch”.

Thank you very much for your advice!

Great to hear that you fixed your code. Thank you for letting us know.