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
# A python implementation of the Banker's Algorithm in Operating Systems using | |
# Processes and Resources | |
# { | |
# "Author: "Biney Kingsley (bluedistro@github.io)", | |
# "Date": 28-10-2018 | |
# } | |
''' | |
The Banker's algorithm is a resource allocation and deadlock avoidance algorithm | |
developed by Edsger Dijkstra that tests for safety by simulating the allocation of | |
predetermined maximum possible amounts of all resources, and then makes a "s-state" |
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
imdb_dir = '../datasets/aclImdb/aclImdb' | |
train_dir = os.path.join(imdb_dir, 'train') | |
labels = list() | |
texts = list() | |
# Processing the labels of the raw IMDB data | |
for label_type in ['neg', 'pos']: | |
dir_name = os.path.join(train_dir, label_type) | |
for fname in os.listdir(dir_name): | |
if fname[-4:] == '.txt': |
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
# cut off reviews after 500 words | |
max_len = 500 | |
# train on 10000 samples | |
training_samples = 10000 | |
# validate on 10000 samples | |
validation_samples = 10000 | |
# consider only the top 10000 words | |
max_words = 10000 | |
# import tokenizer with the consideration for only the top 500 words |
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
# decode the words | |
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()]) | |
decoded_review = ' '.join([reverse_word_index.get(i, '?') for i in sequences[0]]) |
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
# model developing | |
text_input_layer = Input(shape=(500,)) | |
embedding_layer = Embedding(max_words, 50)(text_input_layer) | |
text_layer = Conv1D(256, 3, activation='relu')(embedding_layer) | |
text_layer = MaxPooling1D(3)(text_layer) | |
text_layer = Conv1D(256, 3, activation='relu')(text_layer) | |
text_layer = MaxPooling1D(3)(text_layer) | |
text_layer = Conv1D(256, 3, activation='relu')(text_layer) | |
text_layer = MaxPooling1D(3)(text_layer) | |
text_layer = Conv1D(256, 3, activation='relu')(text_layer) |
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
history = model.fit(x_train, y_train, epochs=50, batch_size=128, callbacks=callback_list, | |
validation_data=(x_val, y_val)) |
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
callback_list = [ | |
keras.callbacks.EarlyStopping( | |
patience=1, | |
monitor='acc', | |
), | |
keras.callbacks.TensorBoard( | |
log_dir='log_dir_m1', | |
histogram_freq=1, | |
embeddings_freq=1, |
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
# save the tokenizer | |
with open(os.path.join(tokenizer_path, 'tokenizer_m1.pickle'), 'wb') as handle: | |
pk.dump(tokenizer, handle, protocol=pk.HIGHEST_PROTOCOL) |
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
tokenizer_path = 'tokenizer' | |
model_path = 'model' | |
model_file = os.path.join(model_path, 'movie_sentiment_m1.h5') | |
tokenizer_file = os.path.join(tokenizer_path, 'tokenizer_m1.pickle') | |
model = load_model(model_file) | |
# load tokenizer | |
with open(tokenizer_file, 'rb') as handle: | |
tokenizer = pickle.load(handle) |
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
def review_rating(score, decoded_review): | |
if float(score) >= 0.9: | |
print('Review: {}\nSentiment: Strongly Positive\nScore: {}'.format(decoded_review, score)) | |
elif float(score) >= 0.7 and float(score) < 0.9: | |
print('Review: {}\nSentiment: Positive\nScore: {}'.format(decoded_review, score)) | |
elif float(score) >= 0.5 and float(score) < 0.7: | |
print('Review: {}\nSentiment: Okay\nScore: {}'.format(decoded_review, score)) | |
else: | |
print('Review: {}\nSentiment: Negative\nScore: {}'.format(decoded_review, score)) | |
print('\n\n') |
OlderNewer