dict reference:

simple assign

a = {“num”: 1, "l" = [1, 2, 3]}
b = a

“b” is a reference to the dict just like “a”. any change to either a or b will be reflected on both.

Shallow copy

b = a.copy()

a new dict is created “b”, but the values in dict “b” is a reference to value in “a”. But since int is always passed by value change of num in b will not affect a. However changes to ‘l’ in either dict will be reflected in the other.

Deep copy

is deep copy.



Install fastttext

pip install fasttext

Download pretrained bin and extract embeddings.

Bin contains ngram info so you can extract OOV words.

Vec does not contain ngram.

import fasttext
import fasttext.util
fasttext.util.download_model('en', if_exists='ignore')
ft = fasttext.load_model('cc.en.300.bin')

# Sentence embedding

is the average of normalized words in the sentence including a EOS token.\

def l2_norm(x):
return np.sqrt(np.sum(x**2))
def div_norm(x):
norm_value = l2_norm(x)
if norm_value > 0:
return x * ( 1.0 / norm_value)
return x
one = ft.get_word_vector('bob')
two = ft.get_word_vector('is')
three = ft.get_word_vector('fun')
eos = ft.get_word_vector('\n')
sentEnc = ft.get_sentence_vector("bob is fun")
avg = (div_norm(one) + div_norm(two) + div_norm(three) + div_norm(eos)) / 3