Vandhana Vishnuraj
Vandhana Vishnuraj

Reputation: 1

My Jupyter Kernel keeps dying is I run the following piece of code

My Jupyter notebook kernel keeps dying if I run this piece of code:

tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertModel.from_pretrained('bert-base-uncased')

def get_bert_embedding(text):

    if pd.isna(text):
        return None
    elif not isinstance(text, str):
        text = str(text)

    inputs = tokenizer(text, return_tensors='pt')
    with torch.no_grad():
        outputs = model(**inputs)
    embeddings = outputs.last_hidden_state.mean(dim=1).squeeze()
    return embeddings.numpy()

def compute_cosine_similarity_bert(text1, text2):

    if text1 is None or text2 is None:
        return None
    vec1 = get_bert_embedding(text1)
    vec2 = get_bert_embedding(text2)
    return cosine_similarity([vec1], [vec2])[0][0]

Can anyone please provide a solution for this?

I tried connecting and reconnecting my kernel but I'm unable to get the desired output. Should I check my port connections as well?

Upvotes: 0

Views: 30

Answers (0)

Related Questions