4D array slicing

Hi Team,

I am stuck on assignment1 of course 4 (convolutional neural networks)
It refers to a 4d array , which should be structured as
(number of training examples, rows, columns, channels). The array is randomly generated, but when i look at it i am struggling to make which are the rows, columns and channels.

?? Can someone provide me with an example of a 4d array and how i can determine rows columns and channels.

many thanks
Sanjay

Training Example 1 of my 4d array is as follows,

[[[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]]

[[ 0. 0. 0. 0. ]
[ 1.62434536 -0.61175641 -0.52817175 -1.07296862]
[ 0.86540763 -2.3015387 1.74481176 -0.7612069 ]
[ 0.3190391 -0.24937038 1.46210794 -2.06014071]
[-0.3224172 -0.38405435 1.13376944 -1.09989127]
[-0.17242821 -0.87785842 0.04221375 0.58281521]
[-1.10061918 1.14472371 0.90159072 0.50249434]
[ 0.90085595 -0.68372786 -0.12289023 -0.93576943]
[ 0. 0. 0. 0. ]]

[[ 0. 0. 0. 0. ]
[-0.26788808 0.53035547 -0.69166075 -0.39675353]
[-0.6871727 -0.84520564 -0.67124613 -0.0126646 ]
[-1.11731035 0.2344157 1.65980218 0.74204416]
[-0.19183555 -0.88762896 -0.74715829 1.6924546 ]
[ 0.05080775 -0.63699565 0.19091548 2.10025514]
[ 0.12015895 0.61720311 0.30017032 -0.35224985]
[-1.1425182 -0.34934272 -0.20889423 0.58662319]
[ 0. 0. 0. 0. ]]

[[ 0. 0. 0. 0. ]
[ 0.83898341 0.93110208 0.28558733 0.88514116]
[-0.75439794 1.25286816 0.51292982 -0.29809284]
[ 0.48851815 -0.07557171 1.13162939 1.51981682]
[ 2.18557541 -1.39649634 -1.44411381 -0.50446586]
[ 0.16003707 0.87616892 0.31563495 -2.02220122]
[-0.30620401 0.82797464 0.23009474 0.76201118]
[-0.22232814 -0.20075807 0.18656139 0.41005165]
[ 0. 0. 0. 0. ]]

[[ 0. 0. 0. 0. ]
[ 0.19829972 0.11900865 -0.67066229 0.37756379]
[ 0.12182127 1.12948391 1.19891788 0.18515642]
[-0.37528495 -0.63873041 0.42349435 0.07734007]
[-0.34385368 0.04359686 -0.62000084 0.69803203]
[-0.44712856 1.2245077 0.40349164 0.59357852]
[-1.09491185 0.16938243 0.74055645 -0.9537006 ]
[-0.26621851 0.03261455 -1.37311732 0.31515939]
[ 0. 0. 0. 0. ]]

[[ 0. 0. 0. 0. ]
[ 0.84616065 -0.85951594 0.35054598 -1.31228341]
[-0.03869551 -1.61577235 1.12141771 0.40890054]
[-0.02461696 -0.77516162 1.27375593 1.96710175]
[-1.85798186 1.23616403 1.62765075 0.3380117 ]
[-1.19926803 0.86334532 -0.1809203 -0.60392063]
[-1.23005814 0.5505375 0.79280687 -0.62353073]
[ 0.52057634 -1.14434139 0.80186103 0.0465673 ]
[ 0. 0. 0. 0. ]]

[[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]]]

Hey @ss3399 , please be mindful about posting your queries in the right category. You are posting your query in the Deep Learning Resources category. This category is not monitored by our mentors. Your specific query belongs to DLS Course 4.

If you are unclear about how to use Discourse, we have made this guide for our learners. Hopefully reading it will help you out in posting in the appropriate categories next time.

For now, I have moved your post in the DLS Course 4 category. For future, in order to get swift responses, post in the appropriate categories.

Thank you and happy learning!
Mubsi

Visualizing a 4D matrix is impossible.

It might be worth having a look at this thread about how to flatten 4D image arrays into 2D matrices. It includes an example of a “telltale” 4D array.

Thanks Paul,

I guess where I am confused is that in the assignment the array as an example is (2,5,4,3) where 2 is the number of samples and 5 is the rows and 4 the columns and 3 the channels. But isn’t it the case that 2 is the samples and 5 is the channels and 4 is the rows and 3 is the columns. Thx

How you choose to define the dimensions is arbitrary, but there are two popular methods: “channel first” and “channel last”. But in every case I have seen so far Prof Ng always uses “channel last” mode. And in either of those cases, it’s really “samples first”, meaning that the difference is the position of the channel dimension relative to the height and width. So for our purposes in your example, 5 is the height dimension (number of rows) and 4 is the width dimension (number of columns) and there are 3 channels.

thanks Paul, appreciate you help.

Thanks Paul, appreciate your help.

Paul,

Just one more question,

Below I am showing sample of a (2, 5, 7, 4) array where the row and height have been zero padded to give an array of size (2, 7, 9, 4). i am only showing (1,7,9,4). now taking into consideration your previous comments, when I look at the array below (and this what is confusing me) → the 4 channels are going across as columns and the 9 columns are going down as the rows. Apologies for banging over the same thing.

[[[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]]

[[ 0. 0. 0. 0. ]
[ 1.62434536 -0.61175641 -0.52817175 -1.07296862]
[ 0.86540763 -2.3015387 1.74481176 -0.7612069 ]
[ 0.3190391 -0.24937038 1.46210794 -2.06014071]
[-0.3224172 -0.38405435 1.13376944 -1.09989127]
[-0.17242821 -0.87785842 0.04221375 0.58281521]
[-1.10061918 1.14472371 0.90159072 0.50249434]
[ 0.90085595 -0.68372786 -0.12289023 -0.93576943]
[ 0. 0. 0. 0. ]]

[[ 0. 0. 0. 0. ]
[-0.26788808 0.53035547 -0.69166075 -0.39675353]
[-0.6871727 -0.84520564 -0.67124613 -0.0126646 ]
[-1.11731035 0.2344157 1.65980218 0.74204416]
[-0.19183555 -0.88762896 -0.74715829 1.6924546 ]
[ 0.05080775 -0.63699565 0.19091548 2.10025514]
[ 0.12015895 0.61720311 0.30017032 -0.35224985]
[-1.1425182 -0.34934272 -0.20889423 0.58662319]
[ 0. 0. 0. 0. ]]

[[ 0. 0. 0. 0. ]
[ 0.83898341 0.93110208 0.28558733 0.88514116]
[-0.75439794 1.25286816 0.51292982 -0.29809284]
[ 0.48851815 -0.07557171 1.13162939 1.51981682]
[ 2.18557541 -1.39649634 -1.44411381 -0.50446586]
[ 0.16003707 0.87616892 0.31563495 -2.02220122]
[-0.30620401 0.82797464 0.23009474 0.76201118]
[-0.22232814 -0.20075807 0.18656139 0.41005165]
[ 0. 0. 0. 0. ]]

[[ 0. 0. 0. 0. ]
[ 0.19829972 0.11900865 -0.67066229 0.37756379]
[ 0.12182127 1.12948391 1.19891788 0.18515642]
[-0.37528495 -0.63873041 0.42349435 0.07734007]
[-0.34385368 0.04359686 -0.62000084 0.69803203]
[-0.44712856 1.2245077 0.40349164 0.59357852]
[-1.09491185 0.16938243 0.74055645 -0.9537006 ]
[-0.26621851 0.03261455 -1.37311732 0.31515939]
[ 0. 0. 0. 0. ]]

[[ 0. 0. 0. 0. ]
[ 0.84616065 -0.85951594 0.35054598 -1.31228341]
[-0.03869551 -1.61577235 1.12141771 0.40890054]
[-0.02461696 -0.77516162 1.27375593 1.96710175]
[-1.85798186 1.23616403 1.62765075 0.3380117 ]
[-1.19926803 0.86334532 -0.1809203 -0.60392063]
[-1.23005814 0.5505375 0.79280687 -0.62353073]
[ 0.52057634 -1.14434139 0.80186103 0.0465673 ]
[ 0. 0. 0. 0. ]]

[[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]]]

It takes some work to understand how they unroll the arrays when you print them. Notice that they show only 3 layers of brackets, so you must be printing just one sample. Either that or there is a bug in your padding logic. There are 7 matrices shown, each of which is 9 x 4.

Hi Paul,

yes you are correct i am only showing one sample out of 2. In this example the the 4 represent the channels? and the 9 the columns? since the 2 sample array is (2,7,9,4) where according the question from Andrew NG the 7 is the height, the 9 is the width and the 4 is the number of channels. ?So when I unravel for one sample i get the above. Is my interpretation correct thanks

Paul, sometimes it’s hard to get across what it is that one is struggling with using the medium. It would be nice if there could be some face to face contact since when speaking the area of confusion can be thrashed faster. juts an observation from me :slight_smile: Thx for you help.

Yes, that is the correct interpretation.

Sure, Zoom or some other video tech would be much better for clear communication, but that just doesn’t scale in the context of a MOOC. There are literally thousands of students active in these courses at any given time. Also please realize that the mentors are volunteers here, meaning that we don’t get paid to do this and don’t “work for you” here.

Absolutely Paul, fully understand the obstacles and fully appreciate all help that you provide. Thx. just thinking out loud.

1 Like