最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - tensorflow.js model.predict() Prints Tensor [[NaN],] - Stack Overflow

matteradmin7PV0评论

I am pletely new to Machine learning and also to tensorflow.js, I am trying to predict the values of the next set but it is giving me "NaN" in result. What am I doing wrong ?

Following this Github example

 async function myFirstTfjs(arr) {
    // Create a simple model.
    const model = tf.sequential();
    model.add(tf.layers.dense({units: 1, inputShape: [2]}));

    // Prepare the model for training: Specify the loss and the optimizer.
    modelpile({
      loss: 'meanSquaredError',
      optimizer: 'sgd'
    });
    const xs = tf.tensor([[1,6],
        [2,0],
        [3,1],
        [4,2],
        [5,3],
        [6,4],
        [7,5],
        [8,6],
        [9,0],
        [10,1],
        [11,2],
        [12,3],
        [13,4],
        [14,5],
        [15,6],
        [16,0],
        [17,1],
        [18,2],
        [19,3],
        [20,4],
        [21,5],
        [22,6],
        [23,0],
        [24,1],
        [25,2],
        [26,3]]);
    const ys = tf.tensor([104780,30280,21605,42415,32710,30385,35230,97795,31985,34570,35180,30095,36175,57300,104140,30735,28715,36035,34515,42355,38355,110080,26745,35315,40365,30655], [26, 1]);
    // Train the model using the data.
    await model.fit(xs, ys, {epochs: 500});
    // Use the model to do inference on a data point the model hasn't seen.
  model.predict(tf.tensor(arr, [1, 2])).print();
  }
  myFirstTfjs([28,5]);

I am pletely new to Machine learning and also to tensorflow.js, I am trying to predict the values of the next set but it is giving me "NaN" in result. What am I doing wrong ?

Following this Github example

 async function myFirstTfjs(arr) {
    // Create a simple model.
    const model = tf.sequential();
    model.add(tf.layers.dense({units: 1, inputShape: [2]}));

    // Prepare the model for training: Specify the loss and the optimizer.
    model.pile({
      loss: 'meanSquaredError',
      optimizer: 'sgd'
    });
    const xs = tf.tensor([[1,6],
        [2,0],
        [3,1],
        [4,2],
        [5,3],
        [6,4],
        [7,5],
        [8,6],
        [9,0],
        [10,1],
        [11,2],
        [12,3],
        [13,4],
        [14,5],
        [15,6],
        [16,0],
        [17,1],
        [18,2],
        [19,3],
        [20,4],
        [21,5],
        [22,6],
        [23,0],
        [24,1],
        [25,2],
        [26,3]]);
    const ys = tf.tensor([104780,30280,21605,42415,32710,30385,35230,97795,31985,34570,35180,30095,36175,57300,104140,30735,28715,36035,34515,42355,38355,110080,26745,35315,40365,30655], [26, 1]);
    // Train the model using the data.
    await model.fit(xs, ys, {epochs: 500});
    // Use the model to do inference on a data point the model hasn't seen.
  model.predict(tf.tensor(arr, [1, 2])).print();
  }
  myFirstTfjs([28,5]);
Share Improve this question edited May 2, 2018 at 5:29 Pratik Khadtale asked May 1, 2018 at 20:04 Pratik KhadtalePratik Khadtale 3055 silver badges11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

What's happening is that the large values in ys are leading to a very large error. That large error, in bination with the (default) learning rate, are causing the model to overcorrect and be unstable. The model will converge if you lower the learning rate.

const learningRate = 0.0001;
const optimizer = tf.train.sgd(learningRate);

model.pile({
  loss: 'meanSquaredError',
  optimizer: optimizer,      
});

Try convert your output to more readable and change your optimizer

var pred = model.predict(tf.tensor(arr, [1, 2]));
    var readable_output = pred.dataSync();
    console.log(readable_output);
Post a comment

comment list (0)

  1. No comments so far