gpt4 book ai didi

javascript - WebWorker 中的 TensorflowJS 通用句子编码器

转载 作者:行者123 更新时间:2023-11-28 03:32:06 29 4
gpt4 key购买 nike

尝试在 webworker 中运行 USE 以避免性能问题,但它缺少 embed() 方法。

WebWorker

importScripts('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.2.9/dist/tf.min.js');
importScripts('https://cdn.jsdelivr.net/npm/@tensorflow-models/universal-sentence-encoder@1.1.1/dist/universal-sentence-encoder.min.js');
tf.setBackend('cpu');

onmessage = message => {
use.load().then(model => {
const sentences = [
'I like my phone.',
'Your cellphone looks great.'
];
/**
model = { model, tokenizer }
*/
model.embed(sentences).then(async embeddings => {
const vec = await embeddings.array();
const cosine = tf.losses.cosineDistance(vec[0], vec[1], 0);
const result = await cosine.data();
console.log(`${Math.round((1 - result) * 100)}%`);
});
});
}

模型内部没有 embed() ,这是主线程的情况
是否可以在 webworkers 中运行 USE?

最佳答案

通用句子编码器(USE)可以在网络 worker 中使用。请参阅下面的工作片段,其中包含从工作线程发送到主线程的结果

<head>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.14.2/dist/tf.min.js"></script>
<script>
const worker_function = () =>
{
importScripts('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.2.9/dist/tf.min.js');
importScripts('https://cdn.jsdelivr.net/npm/@tensorflow-models/universal-sentence-encoder@1.1.1/dist/universal-sentence-encoder.min.js');
tf.setBackend('cpu');
onmessage = message =>
{
console.log("from worker")
use.load().then(model =>
{
const sentences = [
'I like my phone.',
'Your cellphone looks great.'
];
model.embed(sentences).then(async embeddings =>
{
console.log(await embeddings)
const vec = await embeddings.array();
const cosine = tf.losses.cosineDistance(vec[0], vec[1], 0);
const result = await cosine.data();
console.log(`${Math.round((1 - result) * 100)}%`);

postMessage({res: cosine.dataSync(), shape: cosine.shape})
});

});
};
}
if (window != self)
worker_function();
</script>
<script>
const worker = new Worker(URL.createObjectURL(new Blob(["(" + worker_function.toString() + ")()"], { type: 'text/javascript' })));
worker.postMessage({});
worker.onmessage = (message) => {
console.log('from main thread')
const {data} = message
tf.tensor(message.data.res, message.data.shape).print()
}
</script>
</head>

关于javascript - WebWorker 中的 TensorflowJS 通用句子编码器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58090737/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com