Last active
April 2, 2018 22:00
-
-
Save orsonadams/5faeaf239fa1093f1560e87fd49f4cbe to your computer and use it in GitHub Desktop.
A quick demo of lstms used for classification with dummy data all in keras
This file contains 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
import numpy as np | |
from keras.preprocessing.sequence import pad_sequences | |
from keras.utils import to_categorical | |
from keras.models import Sequential | |
from keras.layers import Embedding, Dense, LSTM | |
def main(): | |
num_classes = 5 | |
max_words = 20 | |
sentences = ["The cat is in the house", | |
"The green boy", | |
"computer programs are not alive while the children are"] | |
labels = np.random.randint(0, num_classes, len(sentences)) | |
y = to_categorical(labels, num_classes=num_classes) | |
print(y, "Y") | |
words = set(w for sent in sentences for w in sent.split()) | |
word_map = {w : i+1 for (i, w) in enumerate(words)} | |
sent_ints = [[word_map[w] for w in sent.split()] for sent in sentences] | |
vocab_size = len(words) | |
# pad to max_words length and encode with len(words) + 1 | |
# + 1 because we'll reserve 0 as the padding sentinel. | |
X = np.array([to_categorical(pad_sequences((sent,), max_words), | |
vocab_size + 1) for sent in sent_ints]) | |
print(X.shape) # (3, 20, 16) | |
model = Sequential() | |
model.add(Dense(512, input_shape=(max_words, vocab_size + 1))) | |
model.add(LSTM(128)) | |
model.add(Dense(num_classes, activation='softmax')) | |
model.compile(loss='categorical_crossentropy', | |
optimizer='adam', | |
metrics=['accuracy']) | |
model.fit(X, y) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment