kemakino
kemakino

Reputation: 1122

How do I implement a model that finds a correlation (not similarity) between query and target sentence?

When building an NLP model (I'm going to use an attention-based one), how can we implement one for finding the correlation, not similarity, between the query and target sentences? For instance, the two sentences "I am an environmentalist." and "We should raise the gas price, ban combustion-engine vehicles, and promote better public transit." are somehow similar and positively correlated. However, if the first sentence becomes "I am not an environmentalist.", the two sentences are still similar but now negatively correlated.

import json
import azure.functions as func
from sentence_transformers import SentenceTransformer, util

query = ["I am an environmentalist.",
         "I am not an environmentalist.",
         "I am a tech-savvy person."]
target = ["We should raise the gas price, ban combustion-engine vehicles, and promote better public transit."]

embedder = SentenceTransformer('distilbert-base-nli-stsb-mean-tokens')
query_embedding = embedder.encode(query, convert_to_tensor=True)
target_embedding = embedder.encode(target, convert_to_tensor=True)
searched = util.semantic_search(query_embedding, target_embedding)

print(searched)

# [
#     [{'corpus_id': 0, 'score': 0.30188844}],
#     [{'corpus_id': 0, 'score': 0.22667089}],
#     [{'corpus_id': 0, 'score': 0.05061193}]
# ]

Are there any useful resources or information about this difference and/or finding the correlation by a model? I'm still new to the field of NLP (I have used the sentence transformer for some of my projects) so maybe I simply didn't do a good search on the web.

Upvotes: 0

Views: 84

Answers (0)

Related Questions