【问题标题】:tensorflow.js model.predict() Prints Tensor [[NaN],]tensorflow.js model.predict() 打印张量 [[NaN],]
【发布时间】:2018-05-01 20:04:25
【问题描述】:

我对机器学习和 tensorflow.js 完全陌生,我正在尝试预测下一组的值,但结果是“NaN”。我究竟做错了什么 ?

关注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.compile({
      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]);

【问题讨论】:

    标签: javascript tensorflow machine-learning deep-learning tensorflow.js


    【解决方案1】:

    发生的情况是ys 中的大值导致了非常大的错误。这个大错误与(默认)学习率相结合,导致模型过度校正并且不稳定。如果您降低学习率,模型将收敛。

    const learningRate = 0.0001;
    const optimizer = tf.train.sgd(learningRate);
    
    model.compile({
      loss: 'meanSquaredError',
      optimizer: optimizer,      
    });
    

    【讨论】:

    • 非常感谢它的工作,至少得到了一些输出。但它并没有给出非常正确的值,请参阅周日销售额图表非常高docs.google.com/spreadsheets/d/…
    • 你搞清楚了吗?
    • 还没有,我正在寻找如何更好地训练模型
    【解决方案2】:

    尝试将您的输出转换为更具可读性并更改您的优化器

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-30
      • 2019-04-13
      • 2019-04-21
      • 1970-01-01
      • 1970-01-01
      • 2017-01-14
      相关资源
      最近更新 更多