gpt4 book ai didi

javascript - 在 Node.js 中使用 Tensorflows 通用句子编码器?

转载 作者:行者123 更新时间:2023-11-30 19:01:28 25 4
gpt4 key购买 nike

我在 Node 中使用 tensorflow js 并尝试对我的输入进行编码。

const tf = require('@tensorflow/tfjs-node');
const argparse = require('argparse');
const use = require('@tensorflow-models/universal-sentence-encoder');

这些是导入,在我的 Node 环境中不允许我使用建议的导入语句 (ES6)?尽管它们在这里似乎工作正常。

const encodeData = (tasks) => {
const sentences = tasks.map(t => t.input);
let model = use.load();
let embeddings = model.embed(sentences);
console.log(embeddings.shape);
return embeddings; // `embeddings` is a 2D tensor consisting of the 512-dimensional embeddings for each sentence.
};

此代码产生一个错误,指出 model.embed 不是一个函数。为什么?如何在 node.js 中正确实现编码器?

最佳答案

load 返回解析为模型的 promise

use.load().then(model => {
// use the model here
let embeddings = model.embed(sentences);
console.log(embeddings.shape);
})

如果您更愿意使用awaitload 方法需要在封闭的async 函数中

const encodeData = async (tasks) => {
const sentences = tasks.map(t => t.input);
let model = await use.load();
let embeddings = model.embed(sentences);
console.log(embeddings.shape);
return embeddings; // `embeddings` is a 2D tensor consisting of the 512-dimensional embeddings for each sentence.
};

关于javascript - 在 Node.js 中使用 Tensorflows 通用句子编码器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59473894/

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