如何为单输出 MLP 设计 Tensorflow Js 模型?
How to design a Tensorflow Js model for a single output MLP?
我正在尝试使用 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 进行比较。
我想知道是否有人可以帮助我。
model.fit()
中使用的 x/y-tensors 的维度必须比模型的 first/last 层的形状多一维才能表示多个训练数据集,因此 GPU 加速可以批量训练。
您的模型的另一个问题是高learningRate
(与训练值的大小有关)这会阻止模型收敛,因为它跳过了最优解并失去了控制。
要么减少 learningRate
要么将学习值归一化到较低的数量级。
我正在尝试使用 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 进行比较。
我想知道是否有人可以帮助我。
model.fit()
中使用的 x/y-tensors 的维度必须比模型的 first/last 层的形状多一维才能表示多个训练数据集,因此 GPU 加速可以批量训练。
您的模型的另一个问题是高learningRate
(与训练值的大小有关)这会阻止模型收敛,因为它跳过了最优解并失去了控制。
要么减少 learningRate
要么将学习值归一化到较低的数量级。