如何解决此问题?
ValueError:输入0与层lstm_10不兼容:应为ndim=3,发现ndim=2
https://github.com/zahiruddinnorzain/keras_lstm_mfcc
数据集链接:数据集
我在运行此代码时出现了上述错误。该代码将训练数字的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]
第三个错误是我们在输入层中定义输入形状的方式。休息,如果有任何困惑,你可以阅读并告诉我。