<jupyter_start><jupyter_text><jupyter_code>import numpy as np
import torch
t = np.transpose
import matplotlib.pyplot as plt
%matplotlib inline
def plot_sent_embeddings(sent_vec, sentence, 
                         image_width=23, image_height=5):
    sent_len = len(sentence)
        
    plt.figure(figsize=(image_width,image_height))
    plt.imshow(sent_vec[0:sent_len],
            cmap="hot", interpolation='nearest', aspect='auto')
    plt.colorbar()
!pip install -q flair
from flair.embeddings import BertEmbeddings
from flair.data import Sentence


# init embedding
embedding = BertEmbeddings('bert-large-uncased')

# create a sentence
sentence = Sentence('The grass is green .')

# embed words in sentence
x = embedding.embed(sentence)
se = torch.stack([token.embedding for token in sentence])
sent_emb = se.numpy()
plot_sent_embeddings(sent_emb,sentence)
from sklearn.metrics.pairwise import cosine_similarity

cosine_similarity(sent_emb)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09723643807574207, 'fraction_numerical': 0.0040941658137154556, 'mean_word_length': 4.8562874251497, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '344817', 'license': 'Apache-2.0', 'n_tokens_mistral': 328, 'n_tokens_neox': 311, 'n_words': 71, 'path': 'utils/flair_bert.ipynb', 'repo_name': 'jsedoc/ConceptorDebia', 'size': '977'}