Skip to content

Instantly share code, notes, and snippets.

View eustin's full-sized avatar
🦏

Justin Evans eustin

🦏
View GitHub Profile
@eustin
eustin / import_numpy.py
Created March 31, 2020 08:12
import numpy
import numpy as np
np.random.seed(123)
def draw_pairs(num_trials):
trial_results = {}
for trial_num in range(num_trials):
if trial_num % 10000 == 0:
print(f"running trial number: {trial_num}")
trial_results[trial_num+1] = np.random.choice(all_socks, 2, replace=False).tolist()
return trial_results
def calc_prop_matching_pairs(trial_results):
num_matching_pairs = sum([1 for x in results.values() if x[0] == x[1]])
num_total_pairs = len(results.values())
# of all the pairs we have drawn, what proportion were matching pairs of socks?
prop_matching_pairs = num_matching_pairs / num_total_pairs
print(f"\nprop valid pairs over {NUM_TRIALS:,} trials: {prop_matching_pairs:.2f}")
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
sentence = "Snoopy is a beagle"
tokens = sentence.split(" ")
print(tokens)
index_word = {i: x for i, x in enumerate(tokens)}
print(index_word)
num_classes = len(index_word)
index_one_hot = {i: tf.one_hot(x, depth=num_classes) \
for i, x in enumerate(index_word.keys())}
for k, v in index_one_hot.items():
word = index_word[k]
one_hot_vector = v.numpy()
print(f"{word:<6}: {one_hot_vector}")
embeddings = tf.random.uniform((4, 2), minval=-0.05, maxval=0.05).numpy()
print(embeddings)
snoopy_vec = index_one_hot[0]
beagle_vec = index_one_hot[3]
snoopy_vs_beagle = tf.sqrt(tf.reduce_sum(tf.square(snoopy_vec - beagle_vec)))
print(snoopy_vs_beagle.numpy())
is_vec = index_one_hot[1]
snoopy_vs_is = tf.sqrt(tf.reduce_sum(tf.square(snoopy_vec - is_vec)))
print(snoopy_vs_is.numpy())
snoopy_vs_beagle = tf.sqrt(tf.reduce_sum(tf.square(embeddings[0] - embeddings[3])))
snoopy_vs_is = tf.sqrt(tf.reduce_sum(tf.square(embeddings[0] - embeddings[1])))
print(snoopy_vs_beagle.numpy())
print(snoopy_vs_is.numpy())