# Week 2 Exercise 2 - random_mini_batches

## can anybody pls tell why this error show? my minibatch_X and Y are only print twice and didn’t reach to final condition one. does there are any problem with mini_batch_X = m - mini_batch_size * num_complete_minibatches ?

AttributeError Traceback (most recent call last)
in
15 assert np.sum(np.sum(mini_batches[k][0] - mini_batches[k][0][0], axis=0)) == ((nx * (nx - 1) / 2 ) * mini_batch_size), “Wrong values. It happens if the order of X rows(features) changes”
16 if ( m mini_batch_size > 0): ---> 17 assert mini_batches[n_batches - 1][0].shape == (nx, m mini_batch_size), f"Wrong shape in the last minibatch. {mini_batches[n_batches - 1][0].shape} != {(nx, m % mini_batch_size)}"
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”

AttributeError: ‘int’ object has no attribute ‘shape’

This is to extract “part of” data by Python “slice” function which is specified by “:”.
In this exercise, the shape of shuffled data is [12288,148]. We want to get mini-batch data with slicing the 2nd dimension, which is “number of examples”. We do not touch any for the first dimension. So, basically the slice is;
shuffled_X[:, (start position of a slice) : (end position of a slice)]
And the size of mini-batch is “mini_batch_size”. We only touch the start and end position. As you see, you have one extra dimension in a slice. Please double check.

hi Nobu,
so third_minibatch_X= shuffled_X[:, 2mini_batch_size : 3mini_batch_size ] am i understand you correct?

You are close. ”2mini_batch_size” is not a right variable for Python. You may want to insert a proper operator, like “*”.

By the way, you can use a numeric character for a Python variable, but can not use at the first position like “2min…”.

yes i understand that, i just miss a *
but when i use shuffled_X[:,(k*mini_batch_size):((k+1)*mini_batch_size)] stand for mini_batch_X it still shows error : ‘int’ object has no attribute ‘shape’

if you can tell am i use this quotation correctly?

I suppose that line is fine. Looks like an error comes from a different place. Have you added other instructions like print(xxx.shape) around that ?

i print minibatch_X and minibatch_Y shape are (12288, 64)
(1, 64) . only 2 loop for minibatch X and Y so this is correct ?

Correct. But, as guided, you are better to remove those before submitting it.

1. You have not added any extra `print` statement(s) in the assignment.

i have, but show error ‘int’ object has no attribute ‘shape’

i have no idea where the error is as i mentioned at begining

if the code is correct, why my loop only did twice ? and didn’t pass to final condition : if m % mini_batch_size != 0

For the previous question, “shape” works for"numpy.ndarray", not “int”, “list”, and so on. So, if you add “.shape” to “int” type variable, you will get your error. You can ensure the variable type with “type(xxx)”.
For the second question, you can trace the logic by yourself. First, you get 148 samples. Then, fall into the first loop. As your mini-batch size is 64, this loop gets 2 mini-batches (yes, did twice.) Then, fall into the process to get remaining. So, your mini_batches is the list of 64, 64, (148-64*2).

i didn’t add any shape… it is the error show what i didn’t get the meaning,

and i am ask how should i do if it didn’t get to the final one??? where is the problem from my code mini_batch_X = m - mini_batch_size * num_complete_minibatches?

i tried mini_batch_X =shuffled_X[:,((m-1)(m - mini_batch_size * num_complete_minibatches)):((m)(m - mini_batch_size * num_complete_minibatches))] but still failed with error
AssertionError: Wrong shape in the last minibatch. (12288, 0) != (12288, 20)

I would recommend you to study Python. Of course, you can learn it with solving these exercises, but it is the base for Tensorflow which is one of well-known frameworks for deep learning. That helps you to go further quickly. Here is the link to the thread for that.
https://community.deeplearning.ai/t/best-way-to-learn-basic-python/714

This instruction includes several errors. For example, “(m-1)(m-mini_batch…” does not make sense for Python.
This means, you set (m-1) at first. “m” is, “int”, I think. Then, you added () after that, which mean, you tried to call “int” value with a parameter of (m-mini_batch…). So, my first thought was, we will get an error message of ‘int’ object is not callable’ at first.

What we want to is quite simple. We want create the last slice which does not have the given batch size. (less than 64). So, what is the starting point and the last point ? The starting point for the last slice is “mini_batch_size*num_complete_minibatches”, of course. And, the last point is, “end of the shuffled buffer”. For that, if you lean Python, you can find the easiest way, i.e, do not specify any. For example [:,start_point: ] means that a slice is from the “start_point” to the end of buffer.
Hope this helps some.

sorry , i don’t think i paste correctly , * always have in my code!
mini_batch_X =shuffled_X[:,((m-1)(m - mini_batch_size * num_complete_minibatches)):((m)(m - mini_batch_size * num_complete_minibatches))]
mini_batch_Y =shuffled_Y[:,((m-1)(m - mini_batch_size * num_complete_minibatches)):((m)(m - mini_batch_size * num_complete_minibatches))]

it is just can’t show. so this is not the problem i am asking

Please double check the start point and end point. As I wrote in my previous note, it is quite simple.
In you case, both start position and end position seem to be out of boundary. We have only 148 samples. The starting position from your equation “((m-1) *(m - mini_batch_size * num_complete_minibatches))” is 2940. And, the end position is 2960. Both are beyond the buffer size. With this, you can not get any data, which results in the batch size of (12288, 0).

yes this is why i am asking, i have no idea how to set start position. and end position…

mini_batch_X =shuffled_X[:,((num_complete_minibatches-1)(m - mini_batch_size * num_complete_minibatches)):((num_complete_minibatches)(m - mini_batch_size * num_complete_minibatches))] still shows wrong,

That is already in my previous response. Please re-look again. Very simple, actually.

If you lost the way, I just paste my previous note below…

What we want to is quite simple. We want create the last slice which does not have the given batch size. (less than 64). So, what is the starting point and the last point ? The starting point for the last slice is “mini_batch_size*num_complete_minibatches”, of course. And, the last point is, “end of the shuffled buffer”. For that, if you lean Python, you can find the easiest way, i.e, do not specify any. For example [:,start_point: ] means that a slice is from the “start_point” to the end of buffer.
Hope this helps some.

1 Like