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

Viewed 775

I'm currently trying out linear regression in tensorflow js. I'm inputting x-values into a predict function and getting y-values using dataSync(). Is there a way I can do this asynchronously so it's quicker? The x-values are inputted by plotting points on a canvas.

Here's a part of the code:

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();
1 Answers

Asynchronous code does not execute faster, synchronous code stops processing while waiting for I/O and the opposite is true for asynchronous code.

tf.dataSync() downloads the values from the tensor synchronously, while tf.data() downloads the values from the tf.tensor() asynchronously and returns a promise, so that is what you're looking for.

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

Related