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

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

user9432
1#
user9432 Published in September 21, 2018, 3:01 am

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.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_labels))
model.add(Activation('softmax'))
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)
print(model.predict(test_X))

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.

share|improve this question
  • Have you checked shape for both train and test audio files?, including sample_rate. They need to be same , else you will need to convert test to be same as train. – Anil_M Jan 13 at 5:50
  • Your test_X needs to be of the exact same type (including shape) with your X used for training – desertnaut Jan 13 at 10:32
  • In your X and val_X you have an additional "batch_size" column, which is missing in your test_X. Use the solution of Marcin stackoverflow.com/a/48239160/4132383 or np.expand_dims(test_X, axis=0) – sladomic Jan 13 at 13:49

1 Answer 1

active oldest votes
up vote 1 down vote accepted
You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO