Public
Edited
Oct 11, 2023
Insert cell
Insert cell
viewof file = Inputs.file({Label:"Tab-delimeted ONET file"})
Insert cell
file
Insert cell
data = file.tsv()
Insert cell
tf=require("@tensorflow/tfjs")
Insert cell
use = require("@tensorflow-models/universal-sentence-encoder")
Insert cell
use_model = use.load()
Insert cell
qna = require('@tensorflow-models/qna');
Insert cell
qna_model = qna.load()
Insert cell
use_qna = use.loadQnA()
Insert cell
use_qna.embed([1])
Insert cell
a=({
queries: ['How are you feeling today?', 'What is the captial of China?'],
responses: [
'I\'m not feeling very well.',
'Beijing is the capital of China.',
'You have five fingers on your hand.'
]
}
)
Insert cell
a2 = ({
queries:['farmer','doctor','plumber'],
responses:['11-1011','11-1021']
})
Insert cell
a_use = use_model.embed(a.queries)

Insert cell
a_use_qna = use_qna.embed(a)
Insert cell
a_qna = qna_model.findAnswers("what is the capital of China","I am feeling great and the capital of japan and china is tokyo and Beijing respectively")
Insert cell
b_use = use_model.embed(a2.queries)
Insert cell
b_use_qna = use_qna.embed(a2)
Insert cell
b_use.arraySync()
Insert cell
b_use_qna.queryEmbedding.arraySync()
Insert cell
Array.prototype.reshape1d = function(nrow,ncol){
if (nrow< -1 || ncol < -1 || (nrow == -1 && ncol ==-1) ) throw new Error("bad shape")
if (nrow == -1){
if ( this.length % ncol != 0) throw new Error("bad shape")
nrow = this.length/ncol
} else if (ncol==-1){
if ( this.length % nrow != 0) throw new Error("bad shape")
ncol = this.length/nrow
}
console.log(nrow,ncol)
if (nrow*ncol != this.length) throw new Error("bad shape")
for (let row = 0; row < nrow; ++row) {
this.push(this.splice(0,ncol))
}
console.log(this)
return this
}
Insert cell
x = [1,2,3,4,5,6,7,8,9].reshape1d(3,-1)
Insert cell
4 % 2
Insert cell
knownFunctions = import("http://localhost:5501/knownFunctions.js")
Insert cell
knownFunctions
Insert cell
y = (await use_qna.embed({queries:["doctor","lawyer","indian chief"],"responses":[]})).queryEmbedding
Insert cell
function reshape(typed_array,ncol){
if (typed_array.length%ncol) throw new Error(`${typed_array.length} is not a multiple of ${ncol}`)

let arr = []
for (let index = 0; index < typed_array.length; index+=ncol) {
arr.push( Array.from(typed_array.slice(index,index+ncol)) )
}
return arr
}
Insert cell
reshape(y.dataSync(),100)
Insert cell
class MobileBertEmbedLayer extends tf.layers.Layer {
constructor(qna_model) {
super({})
this.qna_model = qna_model
}

call(input) {
return tf.tidy( () => {
const embeddings = this.embed(input).queryEmbedding;
return embeddings.reshape([-1,100])
});
}
embed(input) {
return this.qna_model.embed({queries:input,responses:[]})
}
getConfig() {
const config = super.getConfig();
return config;
}

static get className() {
return 'MobileBertEmbedLayer';
}
}
Insert cell
y.reshape([-1,100]).toString()
Insert cell
layer = new MobileBertEmbedLayer(use_qna)
Insert cell
layer.apply(['doctor']).toString()
Insert cell
use_qna.embed({queries:["doctor"],responses:[]}).queryEmbedding.toString()

Insert cell
use_qna.embed({queries:tf.tensor(["doctor"]),responses:[]}).queryEmbedding.toString()

Insert cell
tf.tensor(["doctor"]) instanceof tf.Tensor
Insert cell
dl = use_model.embed(["doctor","lawyer"])
Insert cell
dl.arraySync()
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