Last active
September 5, 2018 12:39
-
-
Save csetzkorn/7b134cd25ccf08c508aeb002ddf699a9 to your computer and use it in GitHub Desktop.
simple text classification example using keras and word embedding
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
from numpy import array | |
from keras.preprocessing.text import one_hot | |
from keras.preprocessing.sequence import pad_sequences | |
from keras.models import Sequential | |
from keras.layers import Dense | |
from keras.layers import Flatten | |
from keras.layers.embeddings import Embedding | |
# see also: https://machinelearningmastery.com/use-word-embedding-layers-deep-learning-keras/ | |
# define documents | |
docs = ['Well done!', | |
'Good work', | |
'Great effort', | |
'nice work', | |
'Excellent!', | |
'Weak', | |
'Poor effort!', | |
'not good', | |
'poor work', | |
'Could have done better.'] | |
# define class labels | |
labels = array([1,1,1,1,1,0,0,0,0,0]) | |
# integer encode the documents | |
vocab_size = 50 | |
encoded_docs = [one_hot(d, vocab_size) for d in docs] | |
print(encoded_docs) | |
# pad documents to a max length of 4 words | |
max_length = 4 | |
padded_docs = pad_sequences(encoded_docs, maxlen=max_length, padding='post') | |
print(padded_docs) | |
# define the model | |
model = Sequential() | |
model.add(Embedding(vocab_size, 8, input_length=max_length)) | |
model.add(Flatten()) | |
model.add(Dense(1, activation='sigmoid')) | |
# compile the model | |
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc']) | |
# summarize the model | |
print(model.summary()) | |
# fit the model | |
model.fit(padded_docs, labels, epochs=50, verbose=0) | |
# evaluate the model | |
loss, accuracy = model.evaluate(padded_docs, labels, verbose=0) | |
print('Accuracy: %f' % (accuracy*100)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment