【问题标题】:How to design a Tensorflow Js model for a single output MLP?如何为单输出 MLP 设计一个 Tensorflow Js 模型?
【发布时间】:2018-06-22 14:48:19
【问题描述】:

我正在尝试使用 Tensorflow Js 实现和测试单个输出 MLP,其中我的数据如下所示:

dataset = [[x_1, x_2, ..., x_n, y], ...]

这是我的代码:

     for (var i = 0; i < dataset.length; ++i) {
         x[i] = dataset[i].slice(0, inputLength);
         y[i] = dataset[i][inputLength];
     }

     const xTrain = tf.tensor2d(x.slice(1));
     const yTrain = tf.tensor1d(y.slice(1));

     const model = tf.sequential();
     model.add(tf.layers.dense({inputShape: [inputLength], units: 10}));
     model.add(tf.layers.dense({units: 1}));
     const learningRate = 0.1;
     const optimizer = tf.train.sgd(learningRate);
     model.compile({loss: 'meanSquaredError', optimizer});

     return model.fit(
         xTrain,
         yTrain,
         {
             batchSize: 10,
             epochs: 5
         }
     )

问题是我的模型没有收敛,我在每一步都得到了null 损失函数的值。另外,请注意,我知道我可以使用多元回归来解决这个问题,但我想将结果与 MLP 进行比较。

我想知道是否有人可以帮助我。

【问题讨论】:

  • 你确定 xy 在 for 循环之后包含正确的值,因为它看起来很奇怪。 yTrain 也应该是二维的(shape:[trainingSize,1]),如 xTrain (shape:[trainingSize,inputLength])。
  • @SebastianSpeitel 感谢您的回复。我很确定for,是的。另外,我也试过这个:const xTrain = tf.tensor2d(x.slice(1), [dataset.length-1, inputLength]);const yTrain = tf.tensor2d(y.slice(1), [dataset.length-1, 1]); 仍然没有收敛。
  • 尝试降低学习率
  • @SebastianSpeitel 你是对的!!我必须将学习率降低到0.000000001 才能使其正常工作。我想我需要先对数据进行规范化,然后再使用正则化值。再次感谢!
  • 你同意我把评论作为答案吗?形状是否也会导致错误?

标签: node.js neural-network tensorflow.js


【解决方案1】:

model.fit() 中使用的 x/y-张量的维度必须比模型的第一层/最后一层的形状多一个以表示多个训练数据集,以便 GPU 加速的批量训练成为可能。

您的模型的另一个问题是高 learningRate(与训练值的大小相关),这会阻止模型收敛,因为它会跳过最优解并失控。

要么减少learningRate,要么将学习值归一化到一个较低的量级。

【讨论】:

    猜你喜欢
    • 2013-05-06
    • 2016-03-02
    • 2019-11-15
    • 1970-01-01
    • 2022-11-18
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多