我用什么代替 tensor.dataSync() 来获得 tensorflow.js 中的预测值?

What do I use instead of tensor.dataSync() to get the predicted value in tensorflow.js?

我目前正在尝试在 tensorflow js 中进行线性回归。我将 x 值输入预测函数并使用 dataSync() 获取 y 值。有没有办法可以异步执行此操作以便更快?通过在 canvas.

上绘制点来输入 x 值

这是代码的一部分:

function setup() {
    createCanvas(1000, 600);
    canvas.style.border = '1px solid blue';

    m = tf.variable(tf.scalar(random(0)));
    b = tf.variable(tf.scalar(random(0)));

}

 let ys = tf.tidy(() => predict(curveX));
        let curveY = ys.dataSync();
        ys.dispose();

异步代码不会执行得更快,同步代码在等待时停止处理I/O,异步代码则相反。

tf.dataSync() 从张量同步下载值,而 tf.data() 从张量下载值来自 tf.tensor() 的异步值和 returns 一个承诺,所以这就是你要找的东西。

const getPrediction = async (curveX) => {
    const prediction = predict(curveX)
    return await prediction.data()
}