javascript - Tensorflow 给出回归问题的随机答案

标签 javascript node.js tensorflow artificial-intelligence tensorflow.js

我正在尝试使用 Tensorflow.js 使用 Node.js 重现 Python 练习。

目标是使用机器学习将摄氏度简单地转换为华氏度。

但是,我是 Tensorflow.js 的菜鸟,它一直给我随机答案。

我尝试过很多东西,比如很多不同的形状。 我检查过 Python 和 Node.js 都具有相同的模型。他们都有以下模型:

Layer (type)                 Output shape              Param #   
=================================================================
dense_Dense1 (Dense)         [null,1]                  2         
=================================================================
Total params: 2
Trainable params: 2
Non-trainable params: 0
const tf = require("@tensorflow/tfjs-node")

function convert(c){
    return (c*1.8)+32 // Convert celsius to fahrenheit
} 

var celsius = []
var fahrenheit = []

for (let i = 0; i < 20; i++) {
    var r = 100; // Keeping this only value to ensure that Tf knows the answer I also have tried with 20 different values but doesn't work
    celsius.push([r]) // Shape [20,1]
    fahrenheit.push([convert(r)]) // Push the answer (212) to the fahrenheit array
}

var model = tf.sequential();
model.add(tf.layers.dense({inputShape:[1], units: 1}))

async function trainModel(model, inputs, labels) {
    // Prepare the model for training.  
    model.compile({
      optimizer: tf.train.adam(),
      loss: tf.losses.meanSquaredError,
      metrics: ['accuracy'], // Accuracy = 0
    });

    model.summary(); 

    const epochs = 500;

    return await model.fit(inputs, labels, {
      epochs,
      batchSize: 20, 
      verbose: false // Nothing interesting with verbose
    });
  }

c = tf.tensor(celsius)
f = tf.tensor(fahrenheit)

var training = trainModel(model, c, f)

training.then(function(args){
    var prediction = model.predict(tf.tensor([[100]]));
    prediction.print(); // Prints a random number
    console.log("Real answer = "+convert(100)) 
})

输出张量值每次都是随机变化的。 这是一个例子:

Tensor
     [[65.9411697],]
Real answer = 212

最佳答案

看来主要问题是优化器。 - 如果使用 SGD 优化器进行训练。预测工作正常。

const tf = require("@tensorflow/tfjs-node")
const nr_epochs=500; 

function convert(c){
  return (c*1.8)+32 // Convert celsius to fahrenheit
} 


let celsius = []
let fahrenheit = []

for (let i = 0; i < 100; i++) {
  var r = 100; // Keeping this only value to ensure that Tf knows the answer
  celsius.push(i) // Shape [20,1]
  fahrenheit.push(convert(i)) // Push the answer (212) to the fahrenheit array
}

const train = async (xy, ys) => {
  const model = tf.sequential();

  model.add(tf.layers.dense({units: 1, inputShape: [1]}));

  model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
  await model.fit(xs,ys,{epochs: nr_epochs})
  return model;
}

const predict =  (model, n) => {
  const predicted =  model.predict(tf.tensor2d([n],[1,1])); 
  return predicted;
}

const xs = tf.tensor2d(celsius.slice (0,15), [15,1]);
const ys = tf.tensor2d(fahrenheit.slice (0,15), [15,1]);
(async () => {
  let trained = await train (xs,ys);
  for (let n of [4,6,12]) {
    let predicted = predict (trained, n).dataSync ();
    console.log (`Value: ${n} Predicted: ${predicted [0]}`)
  }
})()

日志:

Value: 4 Predicted: 38.01055908203125
Value: 6 Predicted: 42.033267974853516
Value: 12 Predicted: 54.101402282714844

关于javascript - Tensorflow 给出回归问题的随机答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56366435/

相关文章:

tensorflow - 了解更高维度的密集层的输出

javascript - 从脚本与命令行在 Node 中分配全局变量

javascript - 如何通过 Dropzone.js 使用按钮上传 2 个以上文件

node.js - NodeJS 嵌套 Redis 命令

javascript - Node 调度运行任务太多次

node.js - 在 Node 8.5 中使用 async/await

csv - TensorFlow 对象检测 API CSV 文件格式

javascript - 如何在 AngularJS 中追加 Json 字符串

javascript - 上传页面的一部分或页面中的一张图片 - 第一个(尽快)

python - 导入错误 : cannot import name 'resnet'