Skip to content

Instantly share code, notes, and snippets.

@davidmezzetti
Last active January 24, 2020 00:52
Show Gist options
  • Save davidmezzetti/37695fa2ecc8e1be008f79765dfcfe4e to your computer and use it in GitHub Desktop.
Save davidmezzetti/37695fa2ecc8e1be008f79765dfcfe4e to your computer and use it in GitHub Desktop.
import time
# Generate random data
x = np.random.rand(1000, 25)
y = np.random.rand(50, 25)
# Time NumPy
start = time.time()
for _ in range(100):
np_cosine_similarity(x, y)
print("np time =", time.time() - start)
# Time TensorFlow
start = time.time()
for _ in range(100):
tf_cosine_similarity(x, y).numpy()
print("tf time =", time.time() - start)
print("similarity output equal:", np.allclose(np_cosine_similarity(x, y), tf_cosine_similarity(x, y).numpy()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment