# How to properly use Flatten layer?

Context

I'm trying to create net that will be able to recognize printed-like digits. Something like MNIST, but only for standard printing font.

Images are of the size 40x40 and I'd like to put them into feedforward net since ConvNet seems too powerful for this task.

Question

How should I use Flatten layer in this task?

Code

My current net:

X, test_X, y, test_y = train_test_split(X, y, test_size=0.25, random_state=42)

self.model = Sequential()
self.model.add(Flatten())
self.model.add(Dense(64, activation='relu', input_shape=X.shape[1:]))
self.model.add(Dense(no_classes, activation='softmax'))
self.model.compile(loss="categorical_crossentropy",
optimizer="rmsprop",
metrics=['accuracy'])

self.history = self.model.fit(X, y, batch_size=256, epochs=20, validation_data=(test_X, test_y))
print(self.model.summary())

Example images

Current results

## 1 Answer

The Flatten layer is used for collapsing an ND tensor into a 1D tensor. In your case, the inputs appear to be $$28\times28$$ images, so Flatten will convert that into a tensor with shape $$1\times768$$. Note that no information is lost. Flatten layers are usually used where you have a convolutional layer with dimensions $$N\times M \times C$$ (where $$N$$,$$M$$ are the feature map sizes and $$C$$ is the number of channels) and want to fully connect with a Dense layer or another layer that only accepts 1D inputs. Flatten can also be used when the network is meant to output a feature vector from a final convolutional layer for image classification purposes using a different technique.

• Do You have an idea how I should choose net architecture to get decent results? I'm trying with 1 layer of size up to 1024 and two layers with different sizes, but not larger than 1024 and all the time I get results like on the plots. Pics are 40x40. May 4, 2021 at 8:41