在tensorflowjs中分类
Classify in tensorflowjs
下面this tutorial我想在tensorflowjs中加载并使用一个模型,然后使用classify方法对输入进行分类。
我这样加载并执行模型:
const model = await window.tf.loadGraphModel(MODEL_URL);
const threshold = 0.9;
const labelsToInclude = ["test1"];
model.load(threshold, labelsToInclude).then(model2 => {
model2.classify(["test sentence"])
.then(predictions => {
console.log('prediction: ' + predictions);
return true;
})
});
但我收到错误消息:
TypeError: model2.classify is not a function at App.js:23
如何正确使用tensorflowjs中的classify方法?
本教程使用特定模型 (toxicity)。它的 load
和 classify
功能不是 Tensorflow.js 模型本身的功能,而是由该特定模型实现的。
查看 API to see the supported functions for models in general. If you load a GraphModel, you want to use the model.predict
(or execute
) 函数来执行模型。
因此,您的代码应如下所示:
const model = await window.tf.loadGraphModel(MODEL_URL);
const input = tf.tensor(/* ... */); // whatever a valid tensor looks like for your model
const predictions = model.predict([input]);
console.log('prediction: ' + predictions);
下面this tutorial我想在tensorflowjs中加载并使用一个模型,然后使用classify方法对输入进行分类。
我这样加载并执行模型:
const model = await window.tf.loadGraphModel(MODEL_URL);
const threshold = 0.9;
const labelsToInclude = ["test1"];
model.load(threshold, labelsToInclude).then(model2 => {
model2.classify(["test sentence"])
.then(predictions => {
console.log('prediction: ' + predictions);
return true;
})
});
但我收到错误消息:
TypeError: model2.classify is not a function at App.js:23
如何正确使用tensorflowjs中的classify方法?
本教程使用特定模型 (toxicity)。它的 load
和 classify
功能不是 Tensorflow.js 模型本身的功能,而是由该特定模型实现的。
查看 API to see the supported functions for models in general. If you load a GraphModel, you want to use the model.predict
(or execute
) 函数来执行模型。
因此,您的代码应如下所示:
const model = await window.tf.loadGraphModel(MODEL_URL);
const input = tf.tensor(/* ... */); // whatever a valid tensor looks like for your model
const predictions = model.predict([input]);
console.log('prediction: ' + predictions);