Inspired by: https://machinelearningmastery.com/use-word-embedding-layers-deep-learning-keras/
In [25]:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import tensorflow as tf
# for tf < version2, delete "tensorflow."
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Dense
from tensorflow.keras.layers import Flatten
from tensorflow.keras.utils import plot_model
from tensorflow.keras.layers import Embedding
from tensorflow.keras.preprocessing.text import one_hot
from tensorflow.keras.preprocessing.sequence import pad_sequences
print(tf.__version__)
print(tf.keras.__version__)
docs = ['Well done!',
'Good work',
'Great effort',
'Nice work',
'Excellent!',
'Wow!',
'Weak',
'Poor effort!',
'Not good',
'Poor work',
'Could have done better.',
'Very bad!']
# define class labels
labels = np.array([1,1,1,1,1,1,0,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)
In [26]:
# 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())
In [33]:
# fit the model
model.fit(padded_docs, labels, epochs=200, verbose=0)
# evaluate the model
loss, accuracy = model.evaluate(padded_docs, labels, verbose=0)
print('Accuracy: %f' % (accuracy*100))
In [39]:
test = ['Great job!']
encoded_test = [one_hot(d, vocab_size) for d in test]
print(encoded_test)
padded_test = pad_sequences(encoded_test, maxlen=max_length, padding='post')
In [40]:
model.predict(padded_test)
Out[40]:
In [41]:
test = ['This is bad!']
encoded_test = [one_hot(d, vocab_size) for d in test]
print(encoded_test)
padded_test = pad_sequences(encoded_test, maxlen=max_length, padding='post')
model.predict(padded_test)
Out[41]:
In [ ]: