IMDB Classification on Keras

IMDB Classification on Keras

In the book of Deep Learning with Python, there is an example of IMDB move reviews sentiment classification.

# encoding:utf8

from keras.datasets import imdb
from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers import Embedding, Dense, LSTM
from sklearn.metrics import classification_report


vocab_size = 1000
maxlen = 100
batch_size = 32
embedding_dim = 16
epochs = 1


if __name__ == '__main__':
    (x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=vocab_size)
    # The shape of x_train and x_test are (25000,)
    # The shape of y_train and y_test are (25000,)
    
    x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
    x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
    # The shape of x_train and x_test are (25000, 100)

    model = Sequential()
    model.add(Embedding(vocab_size, embedding_dim))
    model.add(LSTM(embedding_dim))
    model.add(Dense(1, activation='sigmoid'))

    model.compile(
        optimizer='rmsprop',
        loss='binary_crossentropy',
        metrics=['acc']
    )
    model.fit(
        x_train,
        y_train,
        epochs=epochs,
        batch_size=batch_size,
        validation_split=0.2
    )

    y_pred = model.predict_classes(x_test)
    print(classification_report(y_test, y_pred, target_names=['positive', 'negative']))

posted @ 2019-08-02 08:26  健康平安快乐  阅读(209)  评论(0编辑  收藏  举报