Platform
Resources
Pricing
Sign in
Get started
Daniel Russ
Workspace
Fork
Public
By
Daniel Russ
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.
Try it for free
Learn more
Fork
View
Export
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
transformers
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
tf
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
use
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
use_model
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
extractor
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
extractor2
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
jobs
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
embeddings
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
ss
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
embed_tensor
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
similarity
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
x
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
plotly
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
tt
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML