首页 > 解决方案 > 如何使用 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]. 我怎样才能得到预测值?

任何帮助,将不胜感激。

标签: javascriptreactjskeras

解决方案


由于predict返回一个承诺(根据docs),您应该使用

const prediction = await KerasJS.model.predict(inputData);

thencatch方法

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
})

希望这可以帮助!


推荐阅读