нан при прогнозировании значения тензорного потока - PullRequest
0 голосов
/ 05 июня 2018
const nr_epochs = 500; // higher=better but slower
var tfinterface;
const model = tf.sequential();

function initTF() {

  // learn 10x table
  //const xs = tf.tensor2d([1,2,3,4,5,6], [6,1]);
  //const ys = tf.tensor2d([10,20,30,40,50,60], [6,1]);
  const xs = tf.tensor2d([8, 12, 15, 24, 30, 35], [6, 1]);
  const ys = tf.tensor2d([10, 20, 30, 60, 115, 140], [6, 1]);


  // linear regression model
  model.add(tf.layers.dense({
    units: 1,
    inputShape: [1]
  }));
  // Prep for training
  model.compile({
    loss: 'meanSquaredError',
    optimizer: 'sgd'
  });

  // train -- the higher the number the more accurate you'll get (but longer run time)
  tfinterface = model.fit(xs, ys, {
    epochs: nr_epochs
  });
  //tfinterface=model.fit(xs,ys);
  console.log('Training completed');
}

// wrapper around model predict
function predict(n) {

  return tfinterface.then(() => {
    var t = model.predict(tf.tensor2d([n], [1, 1]));
    //console.log(t);
    t.print();
    return t;
  });

}

// Helper for form
function formpredict(v, r) {
  predict(v).then(function(res) {
    //alert(res.get([0]));
    r.innerHTML = res.get([0]);
  });

}

Я приложил ссылку полного HTML ссылка

Если я использую следующие данные, которые работают нормально -

const xs = tf.tensor2d([1,2,3,4,5,6], [6,1]);
const ys = tf.tensor2d([10,20,30,40,50,60], [6,1]);

Однако, когда я изменяючто следование начинает предсказывать NaN-

const xs = tf.tensor2d([8, 12, 15, 24, 30, 35], [6,1]);
const ys = tf.tensor2d([10,20, 30, 60, 115, 140], [6,1]);
...