【发布时间】: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 进行比较。
我想知道是否有人可以帮助我。
【问题讨论】:
-
你确定
x和y在 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