如何提取tensorflow.js中张量的值?

How to extract the value of a tensor in tensorflow.js?

我的模型输出十个预测,我从中使用 argmax 取最大的一个。不幸的是,这也是一个 returns 张量(具有像这样的单个值:张量:[8]),我无法弄清楚如何将这个张量变成整数,或者提取唯一的值。

到目前为止,我已经尝试过 gather、asType、dataSync 和 get。

dataSync returns 一个数组。要获取单个元素,需要使用如下数组索引

const t = model.predict(tensor)
// Use of argmax and get the value
const v = t.argMax().dataSync()[0]