'Calculating the cosineDistance between two arrays of sentences with tensorFlow.js
I am trying to get a rating of similarity for 2 lists of sentences. I have the algorithm with tensorFlow.js that gives a rating of 2 sentences using universal-sentence-encoder, however, I have trouble finding a way to get such a rating for two lists of sentences.
Two sentences similarity:
const tf = require("@tensorflow/tfjs");
const use = require("@tensorflow-models/universal-sentence-encoder");
(async () => {
const model = await use.load();
const embeddings = (
await model.embed(["How old are you?", "What is your age?"])
).unstack();
const cosine = await tf.losses
.cosineDistance(embeddings[0], embeddings[1], 0)
.data();
console.log(1 - cosine[0]); // 0.8464165329933167
})();
So instead of embeddings[0] being a sentence, it should be an array of sentences, and embeddings[1] as well should be an array of sentences. I thought maybe to compare the sentences within those arrays by going 2 sentences at a time, like in the example above, but I highly doubt that would be a good solution.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
