Home Expected dense_3_input to have shape (None, 40) but got array with shape (40, 1)
Reply: 1

Expected dense_3_input to have shape (None, 40) but got array with shape (40, 1)

user3754646 Published in 2018-01-13 02:47:50Z

I am a beginner at Deep Learning and am attempting to practice the implementation of Neural Networks in Python by performing audio analysis on a dataset. I have been following the Urban Sound Challenge tutorial and have completed the code for training the model, but I keep running into errors when trying to run the model on the test set.

Here is my code for creation of the model and training:

import numpy as np
from sklearn.preprocessing import LabelEncoder
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten

num_labels = y.shape[1]
filter_size = 2

model = Sequential()

model.add(Dense(256, input_shape = (40,)))
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
model.fit(X, y, batch_size=32, epochs=40, validation_data=(val_X, val_Y))

Running model.summary() before fitting the model gives me:

Layer (type)                 Output Shape              Param #   
dense_3 (Dense)              (None, 256)               10496     
activation_3 (Activation)    (None, 256)               0         
dropout_2 (Dropout)          (None, 256)               0         
dense_4 (Dense)              (None, 10)                2570      
activation_4 (Activation)    (None, 10)                0         
Total params: 13,066
Trainable params: 13,066
Non-trainable params: 0

After fitting the model, I attempt to run it on one file so that it can classify the sound.

file_name = ".../UrbanSoundClassifier/test/Test/5.wav"
test_X, sample_rate = librosa.load(file_name,res_type='kaiser_fast')
mfccs = np.mean(librosa.feature.mfcc(y=test_X, sr=sample_rate, n_mfcc=40).T,axis=0)
test_X = np.array(mfccs)

However, I get

ValueError: Error when checking : expected dense_3_input to have shape  

(None, 40) but got array with shape (40, 1)

Would someone kindly like to point me in the right direction as to how I should be testing the model? I do not know what the input for model.predict() should be.

Full code can be found here.

Marcin Możejko
Marcin Możejko Reply to 2018-01-13 11:01:32Z


  1. The easiest fix to that is simply reshaping test_x:

    test_x = test_x.reshape((1, 40))
  2. More sophisticated is to reuse the pipeline you have for the creation of train and valid set also for a test set. Please, notice that the process you applied to data files is totally different in case of test. I'd create a test dataframe:

    test_dataframe = pd.DataFrame({'filename': ["here path to test file"]}

    and then reused existing pipeline for creation of validation set.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.308737 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO