Skip to content

Instantly share code, notes, and snippets.

@phoebewong
Created December 12, 2019 21:05
Show Gist options
  • Select an option

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

Select an option

Save phoebewong/18cd04e62c272c32e4bd78f91cc0759a to your computer and use it in GitHub Desktop.
Create embedding matrix for all images using USE embedding
start_time = time.time()
# saving the use embeddings for all the image captions to a numpy array
use_img_embedding = np.zeros((len(image_df),512))
for i, text in enumerate(image_df.caption.values):
if i % 100000 == 0 and i > 0:
print(f'{i} out of {len(image_df.caption.values)} done in {time.time() - start_time:.2f}s')
emb = use_encoder([text])
use_img_embedding[i] = emb
print(f'{i} out of {len(image_df.caption.values)} done')
# normalize embeddings
use_img_embedding_normalized = use_img_embedding/np.linalg.norm(use_img_embedding,axis=1).reshape(-1,1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment