Course 4, week 1, Convolution_model_Step_by_Step_v1, Exercise 3 - unable to overcome shapes error

Oh yeah,

thank you, now all my tests pass

1 Like

Thanks a lot your explanation helped me find a bug that three hours of going over my code couldnā€™t

1 Like

Hi, Iā€™m getting this error and Iā€™m struggling to figure out the issue. My a_slice_prev seems to be correct, my weights and biases also seem to be correct. My vert_start and horiz_start multiply by stride, while the ends add f to it. The for h loop loops over n_H, the for w loop loops over n_W, and the for c loop loops over n_C. a_prev_pad slices A_prev_pad[i:,:,:] like so. What am I doing wrong? Any clue? Iā€™ve been stumped on this for quite a while, any help would be appreciated. Thanks.


ValueError Traceback (most recent call last)
in
6 ā€œstrideā€: 2}
7
----> 8 Z, cache_conv = conv_forward(A_prev, W, b, hparameters)
9 z_mean = np.mean(Z)
10 z_0_2_1 = Z[0, 2, 1]

in conv_forward(A_prev, W, b, hparameters)
88 weights = W[:,:,:,c]
89 biases = b[0,0,0,c]
ā€”> 90 Z[i, h, w, c] = conv_single_step(a_slice_prev, weights, biases)
91 print(np.shape(Z))
92

in conv_single_step(a_slice_prev, W, b)
23 # Z = None
24 # YOUR CODE STARTS HERE
ā€”> 25 s = a_slice_prev*W
26 Z = np.sum(s)
27 Z = Z + float(b)

ValueError: operands could not be broadcast together with shapes (2,3,9,4) (3,3,4)

Hello @Shaka_Flowers!

Check your biases. Hint:

b -- Biases, numpy array of shape (1, 1, 1, n_C)

Best,
Saif.

1 Like

I appreciate the quick response. Unfortunately when I adjust the biases to account for that I just get this error. Not sure why.


IndexError Traceback (most recent call last)
in
6 ā€œstrideā€: 2}
7
----> 8 Z, cache_conv = conv_forward(A_prev, W, b, hparameters)
9 z_mean = np.mean(Z)
10 z_0_2_1 = Z[0, 2, 1]

in conv_forward(A_prev, W, b, hparameters)
87
88 weights = W[:,:,:,c]
ā€”> 89 biases = b[1,1,1,c]
90 print(biases)
91 Z[i, h, w, c] = conv_single_step(a_slice_prev, weights, biases)

IndexError: index 1 is out of bounds for axis 0 with size 1

1 Like

Remember that indexing is 0-based in python. That means that if the shape is (1,1,1,c), then the indexes are [0,0,0,c], right? Or alternatively you can just say [:,:,:,c] because using ā€œ:ā€ means ā€œuse all of that indexā€, but thereā€™s only one value.

1 Like

I actually had [0,0,0,c] initially, then upon reading Saifhanengrā€™s post I changed it to [1,1,1,c]. By using [0,0,0,c] (or [:,:,:,c]) Iā€™m just getting my original error ā€œValueError: operands could not be broadcast together with shapes (2,3,9,4) (3,3,4)ā€

So Iā€™m kinda just going in a circle here. Is it okay if I message you my code? There must be something weird going on in my code that Iā€™m not catching.

EDIT: And I refreshed the workspace with a new copy of the assignment, just to make sure it wasnā€™t a weird workspace quirk, but the issue remains.

1 Like

Hello @Shaka_Flowers! Thank you for sending me your code.

So, you hard coded the Z = np.zeros(). You have given that Z shape = (m, n_H, n_W, n_C). So, you just need this to initialize the Z with zeros.

Furthermore, you are missing a comma in a_prev_pad = A_prev_pad[i:,:,:]. Can you find it where?

Moreover, you are not looping the a_slice_prev over i. You need to do this.

If you face any other error, feel free to share with us.

Best,
Saif.

2 Likes

Iā€™ve been stuck on this problem for longer than Iā€™d like to admit. Iā€™ve fixed the issues you stated (to the best of my knowledge), but yet I continue to get this error (see below). Itā€™s driving me a bit nuts. Iā€™ve checked the public_tests.py file, but it didnā€™t really give me much of a clue beyond what the error already states (it seems to be a Z[0,2,1] issue). Iā€™ve experimented a good bit in the code but the problem remains, so Iā€™m at a loss at what to do next. Iā€™ve direct messaged you my code to give you further insight into my issue. I do appreciate the help youā€™ve provided so far.

Zā€™s mean =
0.5511276474566765
Z[0,2,1] =
[10.76716029 6.05837302 2.14966486 10.34035645 9.51530963 -7.63337335
9.38081853 10.43136103]
cache_conv[0][1][2][3] =
[-1.1191154 1.9560789 -0.3264995 -1.34267579]
First Test: Z[0,2,1] is incorrect. Expected: [-2.17796037, 8.07171329, -0.5772704, 3.36286738, 4.48113645, -2.89198428, 10.99288867, 3.03171932]
Your output: [10.76716029 6.05837302 2.14966486 10.34035645 9.51530963 -7.63337335
9.38081853 10.43136103]

Second Test: All tests passed!

1 Like

Notice that you pass the second test, but not the first test. Thatā€™s an important clue, because the second test has stride = 1 and the first test has stride = 2. So the place to look is how you handle the stride value. Did you read the earlier post on this thread from mrgransky? Pay particular attention to the description of how the stride is handled.

1 Like

Hello @Shaka_Flowers! Itā€™s worth looking at the thread that Paul mentioned. As you sent me your code, one more thing I would like to add:

In Python, the i: syntax is used to slice a list or an array from the i-th index to the end of the list or array. So A[i:] will return all the elements of the list or array A starting from the i-th element and continuing to the end.
On the other hand, using only i without the colon : means to access a single element at the index i in the list or array. So A[i] will return the i-th element of the list or array A.

So, my question is: Do we need to loop all the elements of i or a single element?

Best,
Saif.

1 Like

Thanks to the both of you for the help. I just began tackling the issue again and resolved it.

1 Like

Thanks @mrgransky!
My code was close, but your instructions helped me find my mistakes :slight_smile:

1 Like

tnx a lot you helped me a lot

1 Like

why should we miltiply stride to vert_start and horiz_start? And not plus?

1 Like

Updated:
If we want to choose a point of a tensor where h and w are one. And assume stride is also 1. So, how we will do that? Choosing a particular point? If we add, stride + h = 1 + 1 = 2. But we want to choose the point 1. So, how?

1 Like

Just do stride 0? I compelely do not understand why is multiply here, honestly.

And add better explanation, please, how to extract a_prev_pad. It is really unclear to use first index vert or horiz.

1 Like

and by the way, you didnā€™t say in this thread that we should start from 0 always. I did something like vert_start = h if h == 0 else h *stride. But most probably there is better way

1 Like

Stride canā€™t be zero. It start from 1. If we set stride = 0, it means sliding window is not moving.

1 Like

are cases when we need to use stride 0?

1 Like