Predict returning NaN

1,478 views
Skip to first unread message

John Mann

unread,
Apr 23, 2018, 8:45:01 AM4/23/18
to TensorFlow.js Discussion
I've been trying to put together a simple example of Tensorflow.js and appear to have the code below all working apart from when I do the final predict, when I always get NaN returned.

What I was looking for was some sort of example where data when into a model and some predictions came out, and I couldn't really find anything suitable from the Tensorflow.js examples, however the code in this video seemed just what I wanted:


This is training the model where simpleInput and simpleOutput are just one dimensional arrays.

    function ML_RunTensor2(simpleInput, simpleOutput) {

        try {
            // define a model for linear regression
            linearModel = tf.sequential();
            linearModel.add(tf.layers.dense({ units: 1, inputShape: [1] }));

            // prepare the model for training: specify the loss and the optimizer
            linearModel.compile({ loss: 'meanSquaredError', optimizer: 'sgd' });

            // training data
            const xs = tf.tensor1d(simpleInput);
            const ys = tf.tensor1d(simpleOutput);

            // train
            linearModel.fit(xs, ys);
        } catch (e) {
            debugger;
            console.log(e.Message);
        }
    }

Then ones the model is trained I call this for a prediction:

    function ML_PredictClicked() {
        var inputPrediction = jQuery('#' + predictionInput).val();
        var inputPredictionNumber = parseFloat(inputPrediction);
        const output = linearModel.predict(tf.tensor2d([inputPredictionNumber], [1, 1]));
        var outputPrediction = Array.from(output.dataSync())[0];      
        jQuery('#' + predictionOutput).val(outputPrediction);
    }

Here is the code in the debugger - I tried just output.dataSync() as well.





Nikhil Thorat

unread,
Apr 23, 2018, 9:34:13 AM4/23/18
to John Mann, TensorFlow.js Discussion
Hi John,

If you call "xs.dataSync()" and "ys.dataSync()" are you getting any NaN values?

--
You received this message because you are subscribed to the Google Groups "TensorFlow.js Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tfjs+unsubscribe@tensorflow.org.
Visit this group at https://groups.google.com/a/tensorflow.org/group/tfjs/.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/tfjs/7b78feea-da45-4f33-9c55-d4bb2738a229%40tensorflow.org.

John Mann

unread,
Apr 23, 2018, 11:52:05 AM4/23/18
to TensorFlow.js Discussion, john...@googlemail.com
Hi Nikhil thanks for replying. Yes I am - I can see now what the problem is - the simpleInputs array turned out to be objects not numbers. The simplyOutputs was fine. THANK YOU.
To unsubscribe from this group and stop receiving emails from it, send an email to tfjs+uns...@tensorflow.org.
Reply all
Reply to author
Forward
0 new messages