Tensorflow在回归问题上给出随机答案

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

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

然而,我对Tensorflow.js还很生疏,它总是给我随机的答案。

我尝试了很多不同的方法,包括不同的形状。我已经确认Python和Node.js使用的是相同的模型。它们都有以下模型:

Layer (type)                 Output shape              Param #   =================================================================dense_Dense1 (Dense)         [null,1]                  2         =================================================================Total params: 2Trainable params: 2Non-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.01055908203125Value: 6 Predicted: 42.033267974853516Value: 12 Predicted: 54.101402282714844

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注