Skip to content

Instantly share code, notes, and snippets.

@RichardJohnn
Last active April 10, 2020 09:50
Show Gist options
  • Save RichardJohnn/c30a855019c6f94289e4396a9655f820 to your computer and use it in GitHub Desktop.
Save RichardJohnn/c30a855019c6f94289e4396a9655f820 to your computer and use it in GitHub Desktop.
universal sentence encoder - percent similarity
tf = require('@tensorflow/tfjs-node')
use = require('@tensorflow-models/universal-sentence-encoder')
use.load().then (model) ->
sentences = [
'Hello.'
'Hi.'
'Greetings.'
'Sup?'
"It's nice to meet you."
'How are you?'
'Hell'
]
model.embed(sentences).then (embeddings) ->
similarityMeasure =
tf.matMul(embeddings, embeddings, false, true)
.flatten()
.slice(1, sentences.length - 1)
console.log 'Percent similar to "Hello":'
(await similarityMeasure.data()).map (percentage, index) ->
console.log "#{(percentage * 100).toFixed(1)}% - #{sentences[index+1]}"
###
Percent similar to "Hello":
92.8% - Hi.
73.6% - Greetings.
72.0% - Sup?
55.2% - It's nice to meet you.
46.5% - How are you?
34.0% - Hell
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment