Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot reshape array of size into shape

I am following a machine learning video on youtube at https://www.youtube.com/watch?v=lbFEZAXzk0g. The tutorial is in python2 so I need to convert it into python3. Here is the section of the code I am having an error with:

def load_mnist_images(filename):
    if not os.path.exists(filename):
        download(filename)
    with gzip.open(filename,'rb') as file:
        data = numpy.frombuffer(file.read(),numpy.uint8, offset=16)
        data = data.reshape(-1,1,28,28)
        return data/numpy.float32(256)

I am getting this error: ValueError: cannot reshape array of size 9992 into shape (1,28,28). How do I fix this? In the tutorial it was working. Also, if I have any other errors please tell me.

like image 537
Varun Rajkumar Avatar asked Feb 02 '20 17:02

Varun Rajkumar


3 Answers

Your input does not have the same number of elements as your output array. Your input is size 9992. Your output is size [? x 1 x 28 x 28] since the -1 indicates that the reshape command should determine how many indices along this dimension are necessary to fit your array. 28x28x1 is 784, so any input you want to reshape to this size must be neatly divisible by 784 so it fits in the output shape. 9992 is not divisible by 784, so it is throwing a ValueError. Here is a minimal example to illustrate:

import numpy as np

data = np.zeros(2352) # 2352 is 784 x 3
out = data.reshape((-1,1,28,28)) # executes correctly -  out is size [3,1,28,28]

data = np.zeros(9992) # 9992 is 784 x 12.745 ... not integer divisible
out = data.reshape((-1,1,28,28)) # throws ValueError: cannot reshape array of size 9992 into shape (1,28,28)

So, if you don't want a ValueError, you need to reshape the input into a differently sized array where it fits correctly.

like image 128
DerekG Avatar answered Oct 07 '22 10:10

DerekG


the reshape has the following syntax

data.reshape(shape)

shapes are passed in the form of tuples (a, b). so try,

data.reshape((-1, 1, 28, 28))
like image 44
psnbaba Avatar answered Oct 07 '22 09:10

psnbaba


Try like this

import numpy as np

x_train_reshaped=np.reshape(x_train,(60000, 28, 28))
x_test_reshaped=np.reshape(x_test,(10000, 28, 28))
like image 1
Shilpa Badge Avatar answered Oct 07 '22 11:10

Shilpa Badge