Last active
December 12, 2019 06:59
-
-
Save phoebewong/fd33e5bda8fb06ed69202eb8b8f570aa to your computer and use it in GitHub Desktop.
Calculate cosine distance of 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
| # import dependencies | |
| import numpy as np | |
| from sklearn.preprocessing import Normalizer | |
| # normalize the vector | |
| # flatten for matrix multiplication | |
| normalized_headline = Normalizer().fit_transform(article_headline).flatten() | |
| # compute cosine distances between input article headline and all image captions | |
| img_scores = np.asarray(np.dot(normalized_img_embedding_matrix, normalized_headline.T)).flatten() | |
| # get indices of sorted cosine distances in descending order | |
| output_img_idx = (-img_scores).argsort() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment