Created
November 10, 2017 20:12
-
-
Save manashmandal/aefabdb339baacdb48d5eb3a019e09f5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# LSTM for sequence classification in the IMDB dataset | |
import numpy | |
from keras.datasets import imdb | |
from keras.models import Sequential | |
from keras.layers import Dense | |
from keras.layers import LSTM | |
from keras.layers.embeddings import Embedding | |
from keras.preprocessing import sequence | |
# fix random seed for reproducibility | |
numpy.random.seed(7) | |
# load the dataset but only keep the top n words, zero the rest | |
top_words = 5000 | |
(X_train, y_train), (X_test, y_test) = imdb.load_data(nb_words=top_words) | |
# truncate and pad input sequences | |
max_review_length = 500 | |
X_train = sequence.pad_sequences(X_train, maxlen=max_review_length) | |
X_test = sequence.pad_sequences(X_test, maxlen=max_review_length) | |
# create the model | |
embedding_vecor_length = 32 | |
model = Sequential() | |
model.add(Embedding(top_words, embedding_vecor_length, input_length=max_review_length)) | |
model.add(LSTM(100)) | |
model.add(Dense(1, activation='sigmoid')) | |
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) | |
print(model.summary()) | |
model.fit(X_train, y_train, nb_epoch=3, batch_size=64) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment