我在节点中使用tensorflow js并尝试对我的输入进行编码。
const tf = require('@tensorflow/tfjs-node');
const argparse = require('argparse');
const use = require('@tensorflow-models/universal-sentence-encoder');
这些是导入,我的节点环境中不允许使用建议的导入语句(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
返回解析为模型的承诺
use.load().then(model => {
// use the model here
let embeddings = model.embed(sentences);
console.log(embeddings.shape);
})
如果您更愿意使用
await
,则load
方法需要包含在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.
};