Public
Edited
Dec 14, 2023
1 star
Insert cell
Insert cell
//import { pipeline } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.6.2';
//import { pipeline } from 'xenova/transformers';
transformers = import('https://cdn.jsdelivr.net/npm/@xenova/transformers@2.6.2')
Insert cell
tf = import('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest/dist/+esm')
Insert cell
tf.setBackend('webgl')
Insert cell
use = import('https://cdn.jsdelivr.net/npm/@tensorflow-models/universal-sentence-encoder@1.3.3/+esm')
Insert cell
use_model = await use.load()
Insert cell
Insert cell
extractor = await transformers.pipeline(
"feature-extraction",
"Xenova/all-MiniLM-L6-v2",
{ quantized: false }
)
Insert cell
extractor2 = await transformers.pipeline(
"feature-extraction",
"Xenova/distiluse-base-multilingual-cased-v1",
{ quantized: false }
)
Insert cell
Insert cell
extractor("doctor", { pooling: "mean", normalize: true })
Insert cell
jobs = ["doctor","dentist","oral surgeon","endodontist","lawyer","attorney","chemistry professor","professor of analytical chemistry","chemist","plumber"]
Insert cell
Insert cell
embeddings = Promise.all(
jobs.map( (job) =>
extractor(job,{ pooling: "mean", normalize: true })
)
)
Insert cell
ss = embeddings.slice(0,2).
Insert cell
Insert cell
embed_tensor = tf.concat( embeddings.map( x => tf.tensor(x.tolist()) ),0)
Insert cell
similarity=tf.dot(embed_tensor,embed_tensor.transpose()).sqrt()
Insert cell
similarity.toString()
Insert cell
{
let myDiv = DOM.element('div')
let data = [{
x: jobs,
y: jobs,
z: similarity.arraySync(),
type: 'heatmap'
}]
plotly.newPlot(myDiv, data);
return myDiv
}
Insert cell
embed_tensor.euclideanNorm(1).toString()
Insert cell
x = {
let row_1 = embed_tensor.gather(2)
return tf.dot(row_1,row_1).sqrt().toString()
}
Insert cell
{
let t1=tf.tensor( [[0.240771706,0.963086825,0.120385853],[0.40824829,0.40824829,0.816496581],[0.371390676,0.557086015,0.742781353]] )
return tf.dot(t1,t1).toString()
}
Insert cell
{
let t1 = tf.tensor( [[0.240771706,0.963086825,0.120385853],[0.40824829,0.40824829,0.816496581],[0.371390676,0.557086015,0.742781353]] )
return tf.dot(t1,t1.transpose()).sqrt().toString()
}
Insert cell
plotly = require("https://cdn.plot.ly/plotly-latest.min.js")
Insert cell
tt = tf.tensor( embeddings.map( (s)=>s.data ) )
Insert cell
tt.toString()
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more