W2 Exercise 2 random_mini_batches

first_mini_batch_X = shuffled_X[:, 0 : mini_batch_size]
I suppose I’ am saying that every column of X will be renamed as mini_batch_size

second_mini_batch_X = shuffled_X[:, mini_batch_size : 2 * mini_batch_size]
what am I saying here?

Many thks

Not any column is gonna renamed.

first_mini_batch_X = shuffled_X[:, 0:mini_batch_size] means to select all rows of the matrix shuffled_X and specific columns from 0 to (but not including) mini_batch_size.

Similarly, second_mini_batch_X = shuffled_X[:, mini_batch_size : 2 * mini_batch_size] means to select all rows of the matrix shuffled_X and specific columns from mini_batch_size to (but not including) twice of mini_batch_size.

For example, if mini_batch_size is 64, then first_mini_batch_X has all the rows of shuffled_X and columns from 0 to 63. And, second_mini_batch_X has all the rows but columns from 64 to 127 (as 2 * 64 = 128).

Best,
Saif.

Saif, in the introduction of the exercise 2 is stated:

If the total number of examples is not a multiple of mini_batch_size=64 then there will be βŒŠπ‘š /π‘šπ‘–π‘›π‘–_π‘π‘Žπ‘‘π‘β„Ž_𝑠𝑖𝑧𝑒 mini-batches with a full 64 examples, and the number of examples in the final mini-batch will be (π‘šβˆ’π‘šπ‘–π‘›π‘–_π‘π‘Žπ‘‘π‘β„Ž_π‘ π‘–π‘§π‘’Γ—βŒŠπ‘šπ‘šπ‘–π‘›π‘–_π‘π‘Žπ‘‘π‘β„Ž_π‘ π‘–π‘§π‘’βŒ‹)
If I write in the code
mini_batch_X= shuffled_X[:, int(m -mini_batch_size)* mini_batch_size :] it won’t work
I have to do
mini_batch_X= shuffled_X[:, int(m /mini_batch_size)* mini_batch_size :]
why?

Thks

Why not do the math? Try it out. Put the values of m and mini_batch_size in both equations and compare the output.

I know that the math is correct
the definition conversely was stating m - min-batch_size end not m/min_batch-_size
am I wrong?

The below text is from the assignment:

"If the total number of examples is not a multiple of mini_batch_size=64 then there will be \left\lfloor \frac{m}{mini\_batch\_size}\right\rfloor mini-batches with a full 64 examples, and the number of examples in the final mini-batch will be \left(m-mini_\_batch_\_size \times \left\lfloor \frac{m}{mini\_batch\_size}\right\rfloor\right). "

[quote=β€œabdelkoudos, post:7, topic:348586”]
I need help and clarification concerning this part of the assignment please

first_mini_batch_X = shuffled_X[:, 0 : mini_batch_size]

Here by the indexing rules of numpy:

   X[rows, cols] -> X[data_pts, features]

so that’s mean that this code divide a mini batches from the features not from the data pts.

thanks :slight_smile:

Sorry, but I think you are misinterpreting the way the data is arranged here. Prof Ng uses the columns as the β€œsamples” dimension, so the dimensions are X are features x samples.

You can do it either way, so you just have to pay attention to how it is being done in each particular case.

1 Like