Python 基于keras的LSTM用于未知特征向量的多类分类

Python 基于keras的LSTM用于未知特征向量的多类分类,python,keras,lstm,Python,Keras,Lstm,我是keras的LSTM新手,在这里尝试了以下使用LSTM进行多类分类的解决方案。我在10个类中有768个维度特征向量,我想使用LSTM对它们进行分类。这是我试过的 def do_experiment(train_file, validation_file, test_file, experiment_number, optimizer_name): def scheduler(epoch): if epoch % 4 == 0 and epoch:

我是keras的LSTM新手,在这里尝试了以下使用LSTM进行多类分类的解决方案。我在10个类中有768个维度特征向量,我想使用LSTM对它们进行分类。这是我试过的

def do_experiment(train_file, validation_file, test_file, experiment_number, optimizer_name):

    def scheduler(epoch):
        if epoch % 4 == 0 and epoch:
            K.set_value(model.optimizer.lr, K.get_value(model.optimizer.lr)*0.9)
            print(K.get_value(model.optimizer.lr))
        return K.get_value(model.optimizer.lr)

    change_lr = LearningRateScheduler(scheduler)
    early_stopper = EarlyStopping(min_delta=0.001, patience=15)
    csv_logger = CSVLogger('lstm.csv')
    weights_file="trained_model/" + str(experiment_number) + "-weights.h5"

    model_checkpoint= ModelCheckpoint(weights_file, monitor="val_loss", save_best_only=True, save_weights_only=True, mode='auto')

    x_train, y_train, groundtruth_train= du.loaddata(train_file, experiment_number)
    x_validation, y_validation, groundtruth_validation= du.loaddata(validation_file, experiment_number)

    batch_size = 32
    nb_classes = 10
    nb_epoch = 100

    model = Sequential()
    model.add(Embedding(5000, 32, input_length=768))
    model.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))
    model.add(Dense(10, activation='softmax'))
    model.compile(loss='sparse_categorical_crossentropy', optimizer=optimizer_name, metrics=['accuracy'])
    model.fit(x_train, y_train, batch_size=batch_size, epochs=nb_epoch, validation_data=(x_validation, y_validation), shuffle=True, callbacks=[change_lr, early_stopper, csv_logger,model_checkpoint])
但每当我运行此代码时,都会出现以下错误:

 File "/usr/lib64/python2.7/site-packages/keras/models.py", line 960, in fit
validation_steps=validation_steps)
 File "/usr/lib64/python2.7/site-packages/keras/engine/training.py", line 1581, in fit
batch_size=batch_size)
 File "/usr/lib64/python2.7/site-packages/keras/engine/training.py", line 1418, in _standardize_user_data
exception_prefix='target')
 File "/usr/lib64/python2.7/site-packages/keras/engine/training.py", line 153, in _standardize_input_data
 str(array.shape))
 ValueError: Error when checking target: expected dense_1 to have shape (None, 1) but got array with shape (61171, 10)

我相信我在这里做了一些非常愚蠢的事情,但我无法识别。我应该如何更改此代码以对768维向量进行分类

y\u-train
y\u-validation
看起来像什么?我最好的猜测是,这些是类标签的1xN向量,但是模型的输出层正在寻找类似于一个热编码标签的10xN矩阵的东西。是的,它们正是您所认为的。我如何调整代码使其工作?你能用建议回答我的问题吗?我的问题是一个包含10个类的多类问题。谢谢