title: Convolutional Neural Network Input Shape with Keras
body: <p>I have 32760 audio spectrums calculated of dimensions = 72 (# frames) x 40 (# frequency bands) that I am trying to feed into a "wide" convolutional neural network (first layer is an ensemble of 4 different conv layers). These spectrums have no depth, therefore they can be represented as a 72 x 40 2D numpy array of floats, and thus the X input to the classifier is an array 32760 elements long, each element being one of these 72 x 40 x 1 spectrums. The Y input is an array of labels, one-hot encoded, with 32760 elements.</p>
  
  <p>When trying to fit the CNN using </p>
  
  <pre><code>model.fit(mono_X, mono_Y, epochs=10, batch_size=None, verbose=2)
  </code></pre>
  
  <p>I get the following error:</p>
  
  <pre><code>ValueError when checking input: expected input_47 to have 4 dimensions, but got array with shape (32760, 1)
  </code></pre>
  
  <p>Below is the architecture of my CNN:</p>
  
  <pre><code>spectra = Input(shape=(72, 40, 1)) 
  
  # conv1a
  c1a = Conv2D(48, (3,5), activation='relu', padding = 'same')(spectra)
  c1a = BatchNormalization()(c1a)
  c1a = MaxPooling2D(pool_size=(5, 5), strides = 1)(c1a)
  # conv1b
  c1b = Conv2D(32, (3,9), activation='relu', padding = 'same')(spectra)
  c1b = BatchNormalization()(c1b)
  c1b = MaxPooling2D(pool_size=(5, 5), strides = 1)(c1b)
  # conv1c
  c1c = Conv2D(16, (3,15), activation='relu', padding = 'same')(spectra)
  c1c = BatchNormalization()(c1c)
  c1c = MaxPooling2D(pool_size=(5, 5), strides = 1)(c1c)
  # conv1d
  c1d = Conv2D(16, (3,21), activation='relu', padding = 'same')(spectra)
  c1d = BatchNormalization()(c1d)
  c1d = MaxPooling2D(pool_size=(5, 5), strides = 1)(c1d)
  
  # stack the layers
  merged = keras.layers.concatenate([c1a, c1b, c1c, c1d], axis=3)
  
  # conv2
  c2 = Conv2D(224, (5,5), activation='relu')(merged)
  c2 = BatchNormalization()(c2)
  c2 = MaxPooling2D(pool_size=(5, 5), strides = 1)(c2)
  
  # output softmax
  out = Dense(15, activation='softmax')(c2)
  
  # create Model
  model = Model(spectra, out)
  
  # apply optimization and loss function
  adam = Adam(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
  model.compile(optimizer=adam,
              loss='categorical_crossentropy',
              metrics=['accuracy'])
  </code></pre>
  
  <p>However, if I try to change the input shape to 32760x1 I receive the following error:</p>
  
  <pre><code>ValueError: Input 0 is incompatible with layer conv2d_203: expected ndim=4, found ndim=3
  </code></pre>
  
  <p>What am I doing wrong here? Is there a better way to represent my input data? I have already tried using a pandas DataFrame where each row represents one of the spectra and a myriad of other combinations.
  Using Python 3.6.5 with Keras 2.1.3 with TensorFlow 1.1.0 on the backend.</p>
  
  <p>This is my first CNN, I have only implemented ANNs previously using Keras so there may be a very obvious mistake I'm making. Any help appreciated!</p>
  
  <hr>
  
  <p>Update! Taking the advice from @enumaris, using <code>data_format=channels_last</code> as param on the input layer and adding a <code>Flatten()</code> layer between the last <code>Conv2D</code> and the softmax output layer fixed the latter value error. Now I've come to realize that my training data <code>mono_X</code> is of the wrong shape. The expected input shape <em>if I'm not mistaken</em> should be (#samples, H, W, #channels). <code>mono_X</code> is of shape (32760,) while <code>mono_X[0]</code> is of shape (72, 40). Using numpy's reshape doesn't seem to be able to unpack these nested arrays. How can I properly prepare the input tensor?</p>