Skip to content

Instantly share code, notes, and snippets.

@phoebewong
Last active December 12, 2019 06:59
Show Gist options
  • Select an option

  • Save phoebewong/fd33e5bda8fb06ed69202eb8b8f570aa to your computer and use it in GitHub Desktop.

Select an option

Save phoebewong/fd33e5bda8fb06ed69202eb8b8f570aa to your computer and use it in GitHub Desktop.
Calculate cosine distance of embeddings
# 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