什么会导致 TensorFlow.js 模型以不可读的格式生成输出?

What could cause a TensorFlow.js model to produce output in an unreadable format?

我的 tf.sequential() 模型已创建并且训练过程有效(即我可以在可视化选项卡中看到损失减少和准确性提高)。

但是,当我尝试通过提供输入并要求模型使用 model.predict(input) 预测输出来 运行 单个测试用例时,我收到的输出格式为我无法获得价值。

t {isDisposedInternal: false, shape: Array(2), dtype: "float32", size: 1, strides: Array(1), …}

如果对象被打开,这是里面的东西:

dataId: {}
dtype: "float32"
id: 20129
isDisposed: (...)
isDisposedInternal: false
rank: (...)
rankType: "2"
shape: (2) [1, 1]
size: 1
strides: [1]
__proto__: Object

是什么导致了这个问题?有没有办法从这个对象中提取预测值?

结果是张量。张量的数据存储在后端(例如在计算机的显卡上)。要下载并查看实际数据,您需要 运行 tensor.array(或 tensor.data)函数。

代码示例

const ar1 = await tensor.array(); // returns the data as array
const ar2 = tensor.arraySync(); // same, but blocks the runtime during execution

// Alternative, just the data (just as a single array):
const data1 = await tensor.data();
const data2 = tensor.dataSync();

建议使用异步函数,因为它们不会阻止执行,但请注意,您必须 运行 像这样在 async function 中使用它们:

(async () => {
  const ar = await tensor.array();
})();