0

I am very rookie on LSTM with keras and have tried the following solution here Keras LSTM multiclass classification for using LSTM for multiclass classification. I have 768-dimensional feature vectors within 10 classes and would like to use LSTM to classify them. Here is what I've tried

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])

But whenever I run this code I have the following error:

 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)

I believe I did something very dumb here but I cannot identify. What should I change this code to make the classification of 768-dimensional vectors?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
mad
  • 2,677
  • 8
  • 35
  • 78
  • 1
    What does `y_train` and `y_validation` look like? My best guess is that those are 1xN vectors of class labels, but the output layer of your model is looking for something like a 10xN matrix of one-hot encoded labels. – PaSTE May 22 '18 at 00:12
  • Yes, they are exactly what you think they are. How can I adapt the code to make it work? Can you answer my question with a suggestion? my problem is a multiclass problem with 10 classes. Thanks. – mad May 22 '18 at 07:35

0 Answers0