Answers.Body: <p>The Flatten layer is used for collapsing an ND tensor into a 1D tensor. In your case, the inputs appear to be <span class="math-container">$28\times28$</span> images, so Flatten will convert that into a tensor with shape <span class="math-container">$1\times768$</span>. Note that no information is lost. Flatten layers are usually used where you have a convolutional layer with dimensions <span class="math-container">$N\times M \times C$</span> (where <span class="math-container">$N$</span>,<span class="math-container">$M$</span> are the feature map sizes and <span class="math-container">$C$</span> 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.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-04T08:41:45.840Z
Answers.Text: 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.
Answers.UserDisplayName: MASTER OF CODE
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-03T20:44:25.857Z
Answers.LastActivityDate: 2021-05-03T20:54:13.367Z
Answers.LastEditDate: 2021-05-03T20:54:13.367Z
Answers.UserDisplayName: Mike NZ
Body: <p><strong>Context</strong></p>
  <p>I'm trying to create net that will be able to recognize printed-like digits. Something like MNIST, but only for standard printing font.</p>
  <p>Images are of the size 40x40 and I'd like to put them into feedforward net since ConvNet seems too powerful for this task.</p>
  <p><strong>Question</strong></p>
  <p>How should I use Flatten layer in this task?</p>
  <p><strong>Code</strong></p>
  <p>My current net:</p>
  <pre><code>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=&quot;categorical_crossentropy&quot;,
                     optimizer=&quot;rmsprop&quot;,
                     metrics=['accuracy'])
  
  self.history = self.model.fit(X, y, batch_size=256, epochs=20, validation_data=(test_X, test_y))
  print(self.model.summary())
  </code></pre>
  <p><strong>Example images</strong></p>
  <p><a href="https://i.sstatic.net/Gbq8v.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Gbq8v.png" alt="enter image description here" /></a> <a href="https://i.sstatic.net/za3rm.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/za3rm.png" alt="enter image description here" /></a> <a href="https://i.sstatic.net/SKwpJ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/SKwpJ.png" alt="enter image description here" /></a></p>
  <p><strong>Current results</strong></p>
  <p><a href="https://i.sstatic.net/Pnasd.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Pnasd.png" alt="enter image description here" /></a> <a href="https://i.sstatic.net/hshnT.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/hshnT.png" alt="enter image description here" /></a></p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-05-03T20:26:07.347Z
Comments.Text: Does this help? https://stackoverflow.com/q/44176982/9506872
Comments.UserDisplayName: Marcus
Tags: <machine-learning><training><image-recognition><architecture>
Title: How to properly use Flatten layer?
UserDisplayName: MASTER OF CODE
ThreadText: # How to properly use Flatten layer?
  
  (Asked by: username_0 on 2021-05-03)
  
  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
  
  **Tags:** machine-learning, training, image-recognition, architecture
  > **Comments:**
  > **username_1**: Does this help? https://stackoverflow.com/q/44176982/9506872  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2021-05-03. Score: 3 | Accepted 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.
  
  > **Comments:**
  > **username_0**: 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.  
  
  ---
  
  