Skip to content

Instantly share code, notes, and snippets.

View DerekChia's full-sized avatar
🎯
Focusing

Derek Chia DerekChia

🎯
Focusing
View GitHub Profile
aaa
aarp
abb
abbott
abogado
ac
academy
accenture
accountant
accountants
@DerekChia
DerekChia / gist:46a92aaf5119a1f73190454e440753e0
Created July 31, 2017 18:25 — forked from casschin/gist:1990245
Python webdriver api quick sheet
### Locating UI elements ###
# By ID
<div id="coolestWidgetEvah">...</div>
element = driver.find_element_by_id("coolestWidgetEvah")
or
from selenium.webdriver.common.by import By
element = driver.find_element(by=By.ID, value="coolestWidgetEvah")
# By class name:
@DerekChia
DerekChia / w2v_generate_training_data.py
Last active December 1, 2018 05:17
w2v_generate_training_data
text = "natural language processing and machine learning is fun and exciting"
# Note the .lower() as upper and lowercase does not matter in our implementation
# [['natural', 'language', 'processing', 'and', 'machine', 'learning', 'is', 'fun', 'and', 'exciting']]
corpus = [[word.lower() for word in text.split()]]
@DerekChia
DerekChia / w2v_generate_training_data_2.py
Last active December 1, 2018 11:11
w2v_generate_training_data_2
# Initialise object
w2v = word2vec()
# Numpy ndarray with one-hot representation for [target_word, context_words]
training_data = w2v.generate_training_data(settings, corpus)
@DerekChia
DerekChia / w2v_generate_training_data_func.py
Last active December 1, 2018 11:35
w2v_generate_training_data_func
class word2vec():
def __init__(self):
self.n = settings['n']
self.lr = settings['learning_rate']
self.epochs = settings['epochs']
self.window = settings['window_size']
def generate_training_data(self, settings, corpus):
# Find unique word counts using dictonary
word_counts = defaultdict(int)
@DerekChia
DerekChia / w2v_training_1.py
Created December 1, 2018 11:43
w2v_training_1
# Training
w2v.train(training_data)
class word2vec():
def train(self, training_data):
# Initialising weight matrices
# Both s1 and s2 should be randomly initialised but for this demo, we pre-determine the arrays (getW1 and getW2)
# getW1 - shape (9x10) and getW2 - shape (10x9)
self.w1 = np.array(getW1)
self.w2 = np.array(getW2)
@DerekChia
DerekChia / w2v_training.py
Last active December 1, 2018 12:25
w2v_training
# Training
w2v.train(training_data)
class word2vec():
def train(self, training_data):
# Initialising weight matrices
# Both s1 and s2 should be randomly initialised but for this demo, we pre-determine the arrays (getW1 and getW2)
# getW1 - shape (9x10) and getW2 - shape (10x9)
self.w1 = np.array(getW1)
self.w2 = np.array(getW2)
@DerekChia
DerekChia / w2v_training_error_backpropagation.py
Last active December 1, 2018 15:26
w2v_training_error_backpropagation
class word2vec():
##Removed##
for i in range(self.epochs):
self.loss = 0
for w_t, w_c in training_data:
##Removed##
# Calculate error
# 1. For a target word, calculate difference between y_pred and each of the context words
@DerekChia
DerekChia / w2v_get_vector.py
Last active December 3, 2018 10:32
w2v_get_vector
# Get vector for word
vec = w2v.word_vec("machine")
class word2vec():
## Removed ##
# Get vector from word
def word_vec(self, word):
w_index = self.word_index[word]
v_w = self.w1[w_index]
@DerekChia
DerekChia / w2v_find_similar_words.py
Created December 3, 2018 10:33
w2v_find_similar_words
# Find similar words
w2v.vec_sim("machine", 3)
class word2vec():
## Removed##
# Input vector, returns nearest word(s)
def vec_sim(self, word, top_n):
v_w1 = self.word_vec(word)
word_sim = {}