javascript - 如何使用 keras.js 进行预测
问题描述
我是 keras.js 的新手,并且使用 Keras.JS 对输入进行预测我正在使用以下代码来加载和训练模型
要加载 JSON 文件:
async load(event)
{
const response = await fetch('./model.json');
const json = await response;
this.setState({ value: json });
this.predictedValue = this.predictValue([this.state.value]);
}
预测:
predictValue(inputs)
{
const res = new Float32Array(inputs);
const prediction = KerasJS.model.predict(res);
return prediction.get(0, 0);
}
但输出是[object Response]
. 我怎样才能得到预测值?
任何帮助,将不胜感激。
解决方案
由于predict
返回一个承诺(根据docs),您应该使用
const prediction = await KerasJS.model.predict(inputData);
或then
和catch
方法
KerasJS.model.predict(res)
.then(prediction => {
// prediction is an object keyed by names of the output layers
// or `output` for Sequential models
// e.g.,
// prediction ['fc1000']
})
.catch(err => {
// handle error
})
希望这可以帮助!
推荐阅读
- c++ - 破坏 unique_ptr 的不同方法
- azure - Azure 监视器警报定价
- javascript - 创建一个去抖函数,在搜索时延迟调用 func
- kubernetes - Kubernetes 自动扩展的可能指标在哪里定义
- hibernate - 即使使用 jandex 插件和空的 META-INF/beans.xml,Quarkus 也无法找到 Jandex 索引的第三方类
- ruby-on-rails - 在 TS 模块中使用全局 Rails-UJS(带有 webpacker 的 Rails)
- ios - 如何使用 Swift 4/5 扩展多个类
- c# - C# 控制台硒 | 从文本文件中读取行
- javascript - TypeError:无法在“Node”上执行“insertBefore”:参数 2 不是“Node”类型。角材料
- python - 如何比较二维分布?