Week 1 Convolutional Model, Step by Step: axis confusion

Hi,
In the conv_forward functions’s most inner loop, we need to calculate a_slice_prev variable. However, in normal python slicing we use x axis first then the y axis. But here it seems the correct way is a_slice_prev = a_prev_pad[vert_start:vert_end, horiz_start:horiz_end,:]. But should not be this a_slice_prev = a_prev_pad[ horiz_start:horiz_end, vert_start:vert_end,:] as horiz corresponds to the x axis? What am I missing here?

Thanks

Hi @Irfan_Alahi,

Let’s do an exercise, below is a 2D array representing a photo:

array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29, 30, 31],
       [32, 33, 34, 35, 36, 37, 38, 39],
       [40, 41, 42, 43, 44, 45, 46, 47]])
  1. What is the height of the photo?

  2. When you count its height, are you counting the number of rows or the number of columns?

  3. If I understand you correctly, height should be a “y-axis” thing, are you referring such “y-axis” thing to counting the number of rows or the number of columns?

  4. If you cut horizontally so that the photo is divided in to 3 equal, smaller photos of the same width as the original one but one-third the height, and now we only want the middle “sub-” photo, are we slicing some rows out, or some columns out? Note that if you are slicing some rows out, you do a_prev_pad[row_idx_start:row_idx_end, :]

These are guidance questions for you to clear your question, and if you think your questions have been cleared, there is no need to answer them here, otherwise, I would ask you to provide your answers to the above questions so that I know how you approach them which is very relevant to your question.

Cheers,
Raymond

I think I am still confused. Here are my responses to the questions:

  1. What is the height of the photo?

6 as it has 6 rows.

  1. When you count its height, are you counting the number of rows or the number of columns?

Number of rows

  1. If I understand you correctly, height should be a “y-axis” thing, are you referring such “y-axis” thing to counting the number of rows or the number of columns?

Yes, height should be a “y-axis” thing and I am referring such “y-axis” thing to counting the number of rows.

  1. If you cut horizontally so that the photo is divided in to 3 equal, smaller photos of the same width as the original one but one-third the height, and now we only want the middle “sub-” photo, are we slicing some rows out, or some columns out? Note that if you are slicing some rows out, you do a_prev_pad[row_idx_start:row_idx_end, :]

I am confused here. I am slicing some rows out but again should not it be a_prev_pad[ : , row_idx_start:row_idx_end] because row idx is y axis? In my mind, when I think about the python traditional list, I think we first use the x axis, then the y axis to slice.

@Irfan_Alahi,

Try it.

import numpy as np
x = np.array(
      [[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29, 30, 31],
       [32, 33, 34, 35, 36, 37, 38, 39],
       [40, 41, 42, 43, 44, 45, 46, 47]])

Open a notebook, run the above code to define the x, and try to slice the 3rd and the 4th rows out. We develope that muscle memory by getting hands dirty. It is our job to learn how numpy works.

Raymond