mfcc in keras lstm error: Expected ndim=3, found ndim=2

mfcc in keras lstm error: Expected ndim=3, found ndim=2

我该如何解决这个问题?

ValueError: Input 0 is incompatible with layer lstm_10: expected ndim=3, found ndim=2

https://github.com/zahiruddinnorzain/keras_lstm_mfcc

数据集link:dataset

我在 运行 此代码时遇到上述错误。此代码将训练数字的 mfcc 数据,从 0 到 9,系数为 13。

from __future__ import print_function
import numpy as np

from keras.optimizers import SGD
np.random.seed(1337)
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.layers.recurrent import LSTM
#from SpeechResearch import loadData

from sklearn.preprocessing import LabelEncoder
import pandas

'exception_verbosity = high'
batch_size = 5
hidden_units = 13
nb_classes = 10
print('Loading data...')


# load train dataset
dataframe = pandas.read_csv("train.csv", header=None)
dataset = dataframe.values
X_train = dataset[:,0:13] #.astype(float)
Y = dataset[:,13]
# encode class values as integers
encoder = LabelEncoder()
encoder.fit(Y)
encoded_Y = encoder.transform(Y)
# convert integers to dummy variables (i.e. one hot encoded)
y_train = np_utils.to_categorical(encoded_Y)


# load test dataset
dataframe = pandas.read_csv("test.csv", header=None)
dataset = dataframe.values
X_test = dataset[:,0:13] #.astype(float)
y_test = dataset[:,13]
# encode class values as integers
encoder2 = LabelEncoder()
encoder2.fit(y_test)
encoded_Y2 = encoder.transform(y_test)
# convert integers to dummy variables (i.e. one hot encoded)
Y_test = np_utils.to_categorical(encoded_Y2)



#(X_train, y_train), (X_test, y_test) = loadData.load_mfcc(10, 2)

print(len(X_train), 'train sequences')
print(len(X_test), 'test sequences')
print('X_train shape:', X_train.shape)
print('X_test shape:', X_test.shape)
print('y_train shape:', y_train.shape)
print('y_test shape:', y_test.shape)
print(y_test)
print('Build model...')

Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)

model = Sequential()
model.add(LSTM(output_dim=hidden_units, init='uniform', inner_init='uniform',
               forget_bias_init='one', activation='tanh', inner_activation='sigmoid', input_shape=X_train.shape[1:]))


model.add(Dense(nb_classes))
model.add(Activation('softmax'))

sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd)

print("Train...")
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=3, validation_data=(X_test, Y_test), show_accuracy=True)
score, acc = model.evaluate(X_test, Y_test,
                            batch_size=batch_size,
                            show_accuracy=True)
print('Test score:', score)
print('Test accuracy:', acc)

我解决了你的代码,这里是工作代码。我不得不面对很多错误,并且警告也在那里,因为您似乎使用的是旧版本的 keras。 下面的代码是根据 keras 的更新版本:

from __future__ import print_function
import numpy as np

from keras.optimizers import SGD
np.random.seed(1337)
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.layers.recurrent import LSTM
#from SpeechResearch import loadData

from sklearn.preprocessing import LabelEncoder
import pandas

'exception_verbosity = high'
batch_size = 5
hidden_units = 13
nb_classes = 10
print('Loading data...')


# load train dataset
dataframe = pandas.read_csv("train.csv", header=None)
dataset = dataframe.values
X_train = dataset[:,0:13] #.astype(float)
Y = dataset[:,13]
# encode class values as integers
encoder = LabelEncoder()
encoder.fit(Y)
encoded_Y = encoder.transform(Y)
# convert integers to dummy variables (i.e. one hot encoded)
# y_train = np_utils.to_categorical(encoded_Y)


# load test dataset
dataframe = pandas.read_csv("test.csv", header=None)
dataset = dataframe.values
X_test = dataset[:,0:13] #.astype(float)
y_test = dataset[:,13]
# encode class values as integers
encoder2 = LabelEncoder()
encoder2.fit(y_test)
encoded_Y2 = encoder.transform(y_test)
# convert integers to dummy variables (i.e. one hot encoded)
# y_test = np_utils.to_categorical(encoded_Y2)



#(X_train, y_train), (X_test, y_test) = loadData.load_mfcc(10, 2)

print(len(X_train), 'train sequences')
print(len(X_test), 'test sequences')
print('X_train shape:', X_train.shape)
print('X_test shape:', X_test.shape)
# print('y_train shape:', y_train.shape)
# print('y_test shape:', y_test.shape)
# print(y_test)
print('Build model...')

X_train = X_train.reshape(1, X_train.shape[0], X_train.shape[1])
X_test = X_test.reshape(1, X_test.shape[0], X_test.shape[1])
print('X_train shape:', X_train.shape)
print('X_test shape:', X_test.shape)

Y_train = np_utils.to_categorical(encoded_Y, nb_classes)
Y_test = np_utils.to_categorical(encoded_Y2, nb_classes)
print(Y_train.shape)
print(Y_test.shape)
Y_train = Y_train.reshape(1, Y_train.shape[0], Y_train.shape[1])
Y_test = Y_test.reshape(1, Y_test.shape[0], Y_test.shape[1])

model = Sequential()
model.add(LSTM(units=hidden_units, kernel_initializer='uniform',
           unit_forget_bias='one', activation='tanh', recurrent_activation='sigmoid', input_shape=(None,X_train.shape[2]),     return_sequences=True))


model.add(Dense(nb_classes))
model.add(Activation('softmax'))

sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
print(model.summary())

print("Train...")
model.fit(X_train, Y_train, batch_size=batch_size, epochs=3)
score, acc = model.evaluate(X_test, Y_test,
                        batch_size=batch_size)
print('Test score:', score)
print('Test accuracy:', acc)

您遇到的错误是因为您在输入 LSTM 之前没有重塑数据 遇到第二个错误是因为在 keras 的更新版本中,没有像 show_accuracy 这样的术语。我们只需要在 model.compile 中编译模型时定义 metrics = ['accuracy] 第三个错误是我们在输入层定义输入形状的方式。 休息,你可以阅读,如果有任何困惑,请告诉我。