2

I came across this line of code that uses numpy:

img = data.reshape(data.shape[0], 3, 32, 32)

I understood this line of code, except for data.shape[0]. What I know is that this portion will return the number of rows. But, what I didn't get is how the data (i.e. rows) will be reshaped to a 32x32 matrix with 3-channels. Why didn't data alone be used?

Maybe I'm confusing things here?

Thanks.

2
  • Reading the docs again might help. Commented Mar 2, 2017 at 16:55
  • What's the shape of data? It has to be at least 2d, and that data.shape[1:] values have the same product as 3*32*32. Commented Mar 2, 2017 at 17:20

4 Answers 4

3

When reshaping a matrix, the new shape should be compatible with the original shape.

In order to ensure this dynamically, this code uses data.shape[0] to get the number of rows of the original matrix (i.e. the first dimension of data). Knowing this, it reshapes the matrix into a 4-d matrix defined as: rows|3|32|32.

As you pointed out the array itself could have been used, so this code isn't checking the validity of the number of rows, rather it is using the row count to ensure that row elements are not shuffled into the new columns during the reshape.

Sign up to request clarification or add additional context in comments.

Comments

2

Reshape cannot change the size of the data but it can change the number of dimensions. If the original shape was e.g. (10x3072) or (10x3x1024) you can reshape the complete array to 10x3x32x32 but not to 9x3x32x32.

Apparently, this code is written to leave the first dimension unchanged and reshape the rest of the array. No matter what the actual shape of data is, img will always be Nx3x32x32 if the size of data is N*3072. Otherwise this throws an error.

Probably whoever wrote the code did not know that you can pass -1 to reshape in order to automatically set the size of one dimension:

img = data.reshape(-1, 3, 32, 32)

This is possible because the total size of the data must remain unchanged.

Why didn't data alone be used?

Difficult to say without knowing the previous shape of data, but from the name img I would deduct that the reshaped array is likely supposed to hold a collection of n 32x32 RGB images.

Comments

1

One way this could make sense would be if you know exactly how the final however many dimensions are organized, but that you do not know exactly how many rows there are; for instance,

In [9]: data.shape
Out[9]: (5, 3072)

In [7]: img = data.reshape(data.shape[0], 3, 32, 32)  # 3072 = 3*32*32

In [10]: img.shape
Out[10]: (5, 3, 32, 32)

Comments

1

Lets say you have an array with N lines, each line being a 3*32*32=3072 long array. If you don't know for sure how many lines there are, but know you want the rest reshaped to (3, 32, 32), you can use the indicated line.

Otherwise you would get an error:

>>> import numpy as np
>>> n_lines = 10
>>> data = np.arange(n_lines*3*32*32)
>>> data.reshape(n_lines + 1 , 3*32*32) # notice the + 1 !!
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: total size of new array must be unchanged

But if you use the correct number:

>>> data.reshape(n_lines, 3*32*32)
array([[    0,     1,     2, ...,  3069,  3070,  3071],
       [ 3072,  3073,  3074, ...,  6141,  6142,  6143],
       [ 6144,  6145,  6146, ...,  9213,  9214,  9215],
       ..., 
       [21504, 21505, 21506, ..., 24573, 24574, 24575],
       [24576, 24577, 24578, ..., 27645, 27646, 27647],
       [27648, 27649, 27650, ..., 30717, 30718, 30719]])
>>> data.reshape(n_lines, 3, 32, 32)
array([[[[    0,     1,     2, ...,    29,    30,    31],
         [   32,    33,    34, ...,    61,    62,    63],
         [   64,    65,    66, ...,    93,    94,    95],
         ..., 
         [  928,   929,   930, ...,   957,   958,   959],
         [  960,   961,   962, ...,   989,   990,   991],
         [  992,   993,   994, ...,  1021,  1022,  1023]],

        [[ 1024,  1025,  1026, ...,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.