C1 W2 Exercise 2

I really don’t like talking to people but I can’t figure this out.

On the exercise it says to setup the mini-batches as so:

first_mini_batch_X = shuffled_X[:, 0 : mini_batch_size]
second_mini_batch_X = shuffled_X[:, mini_batch_size : 2 * mini_batch_size]

Which I did in the for loop form and then below with the less than full mini-batch size. The X and Y are setup exactly the same because in my mind, the first “:” auto selects the single row in Y and follows the same mini_batch_size as above.

It gives me the assertion error:
“Wrong values. Check the indexes used to form the mini batches”

But I don’t understand why when it’s setup, to my understanding, as it was stated.

Hello @Juillian_Sawyer,

While looking at the above code it seems you are trying to make the batches using all the rows and specific columns/features.

You can try specifying the size of mini batches in rows/records

something like the below syntax.

first_mini_batch_X = shuffled_X[0 : mini_batch_size, :]
second_mini_batch_X = shuffled_X[mini_batch_size : 2 * mini_batch_size, :]

Swapping sides, it gives a shape error instead

Assuming because it’s putting the features as the batches instead of the inputs in that format

The point is that you want all the rows here and that you are selecting by columns to form the minibatches. As Juillian showed in the first post here, that is what is happening. It is fine to use the same syntax with the Y values, even though they have only one row. The “:” syntax says “use all possible values in this index position”. That works perfectly well if there is only one row.

But back to the high level question here: my interpretation of that message is that it’s not complaining about the shapes of the results, but the values. Meaning that your minibatches are probably the correct sizes, but are being selected from the wrong positions. At least that’s my guess. Are you sure that for the last partial minibatch, that you start the column index at the correct point? The index will equal the sum total of the sizes of all the full minibatches, right? Check your loop index values. Run this code and watch what happens:

for ii in range(5):
    print(f"ii = {ii}")
print(f"After loop ii = {ii}")

One way to debug this would be to print the shapes and starting index for each minibatch for both the X and Y values through your loops and including the last partial minibatch. Do you see the pattern that you would expect? In other words, you believe your code is correct, but it apparently isn’t. So there must be some way in which what you wrote does not comport with your intentions. So now you need to figure out where the error is and starting from concrete evidence is one good way to begin the process of figuring this out.


I appreciate your response, the error lied in the second remainder section where the batch was starting. I was too focused on the first part being the error

It’s great to hear that you were able to find the solution. Onward! :nerd_face:

1 Like