Skip to content

Instantly share code, notes, and snippets.

Created January 2, 2019 04:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save lettergram/3cf518f52142b80362731241e9b49bc0 to your computer and use it in GitHub Desktop.
max_words, batch_size, maxlen, epochs = 10000, 125, 500, 5
# Determine the number of categories + default(i.e. sentence types)
num_classes = np.max(y_train) + 1
# Vectorize the output sentence type classifcations to Keras readable format
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
# Pad the input vectors to ensure a consistent length
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
model = Sequential()
# Create Embedding (Input) Layer (max_words) --> LSTM Layer (128)
model.add(Embedding(max_words, 128))
model.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2))
# LSTM Layer (128) --> Output Layer (num_classes)
model.add(Dense(num_classes, activation='softmax'))
# Add optimization method, loss function and optimization value
optimizer='adam', metrics=['accuracy'])
# "Fit the model" (train model), using training dataw (80% of dataset), y_train, batch_size=batch_size,
epochs=epochs, validation_data=(x_test, y_test))
# Evalute the trained model, using the test data (20% of the dataset)
score = model.evaluate(x_test, y_test, batch_size=batch_size)
# Final testing accuracy, using the resevered 20% testing data
print('Test accuracy:', score[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment