This file contains hidden or 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
| import numpy as np | |
| np.random.seed(123) |
This file contains hidden or 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 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 |
This file contains hidden or 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 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}") |
This file contains hidden or 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
| import matplotlib.pyplot as plt | |
| import numpy as np | |
| import tensorflow as tf | |
| sentence = "Snoopy is a beagle" | |
| tokens = sentence.split(" ") | |
| print(tokens) |
This file contains hidden or 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
| index_word = {i: x for i, x in enumerate(tokens)} | |
| print(index_word) |
This file contains hidden or 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
| 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}") |
This file contains hidden or 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
| embeddings = tf.random.uniform((4, 2), minval=-0.05, maxval=0.05).numpy() | |
| print(embeddings) |
This file contains hidden or 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
| 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()) |
This file contains hidden or 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
| 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()) |
This file contains hidden or 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
| 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()) |
OlderNewer