Skip to content

Instantly share code, notes, and snippets.

View bluedistro's full-sized avatar
🏠
...

romann_empyre bluedistro

🏠
...
  • United Kingdom
View GitHub Profile
@bluedistro
bluedistro / bankers_algorithm.py
Created November 6, 2018 18:23
Python Code to simulate Bankers algorithm as used in Operating Systems
# 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"
@bluedistro
bluedistro / code_0.py
Created December 31, 2018 14:18
data preprocessing
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':
@bluedistro
bluedistro / code_1.py
Last active December 31, 2018 15:09
tokenization
# 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
@bluedistro
bluedistro / decode_vector.py
Created December 31, 2018 14:25
Decode Vectorized data
# 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]])
@bluedistro
bluedistro / model.py
Last active December 31, 2018 15:10
model building
# 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)
@bluedistro
bluedistro / fit.py
Created December 31, 2018 15:11
fitting model
history = model.fit(x_train, y_train, epochs=50, batch_size=128, callbacks=callback_list,
validation_data=(x_val, y_val))
@bluedistro
bluedistro / callback.py
Created December 31, 2018 15:15
callbacks
callback_list = [
keras.callbacks.EarlyStopping(
patience=1,
monitor='acc',
),
keras.callbacks.TensorBoard(
log_dir='log_dir_m1',
histogram_freq=1,
embeddings_freq=1,
@bluedistro
bluedistro / save_tokenizer.py
Created January 1, 2019 19:23
save tokenizer
# save the tokenizer
with open(os.path.join(tokenizer_path, 'tokenizer_m1.pickle'), 'wb') as handle:
pk.dump(tokenizer, handle, protocol=pk.HIGHEST_PROTOCOL)
@bluedistro
bluedistro / load_model_tokenizer.py
Created January 1, 2019 19:24
load model and tokenizer
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)
@bluedistro
bluedistro / polarity_scale.py
Created January 1, 2019 19:27
Scale polarity
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')