I am currently studying from Hands-On Machine Learning with Scikit-Learn and TensorFlow. At some point in Chapter 10, the author demonstrates an MLP architecture to tackle the FashionMNIST problem:

model = keras.models.Sequential()
model.add(keras.layers.Flatten(input_shape=[28, 28]))
model.add(keras.layers.Dense(300, activation="relu"))
model.add(keras.layers.Dense(100, activation="relu"))
model.add(keras.layers.Dense(10, activation="softmax"))

He then gives the following descripition for the flatten layer:

Next, we build the first layer and add it to the model. It is a Flatten layer whose role is simply to convert each input image into a 1D array: if it receives input data X, it computes X.reshape(-1, 1). This layer does not have any parameters, it is just there to do some simple preprocessing. Since it is the first layer in the model, you should specify the input_shape: this does not include the batch size, only the shape of the instances. Alternatively, you could add a keras.layers.InputLayer as the first layer, setting shape=[28,28].

At this point, I can't understand why X.reshape(-1,1) is the chosen reshape action. Assuming that Flatten is performed separately for each batch (is that true?), it should transform (28,28) to (784,), yet the above reshape yields (784,1), which is not even a 1-D array. If that is correct, what is the purpose of this extra dimension with length 1?

EDIT: Considering also batch_size, it should transform (batch_size,28,28) to (batch_size, 784). However, the reshape results to (batch_size*784,1), which does not seem correct either.


1 Answer 1


I actually found this in the Errata of the book (https://www.oreilly.com/catalog/errata.csp?isbn=0636920142874). The correct is X.reshape(-1,28*28). I assume that the purpose of -1 is to keep the batch_size.

Note from the Author or Editor:

Thanks a lot for your feedback. Indeed, this is an error. I should have written: "receives input data X, it computes X.reshape(-1, 28*28)". Fixed, thanks again!


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.