用非线性数据计算方程



我正在使用Tensorflow.js进行我的第一步,我需要计算方程

y = [(x * 100) / a]/100

I.E.给定4个张量,如:

[1,1,1,0], [2,2,2,1], [0,0,1,0], [0,2,2,1]

每个张量的所有值的总和为:

3, 7, 1, 5

这些值的总和为:

15

上述方程式为:

y = [(3 * 100) / 15]/100
y = [(7 * 100) / 15]/100
y = [(1 * 100) / 15]/100
y = [(5 * 100) / 15]/100

所以输出张量应该是:

[0.19], [0.44], [0.06], [0.31]

我制作了下面的代码,试图训练一个模型来求解方程,但结果远不能被接受。我甚至尝试生成60对输入和输出示例,将训练时期增加到50k,并增加输入层的单元数量,但结果似乎更糟。你能帮我一下吗?我哪里搞错了?非常感谢。

<script>
async function predictOutput() {
const model = tf.sequential();
//config for the hidden layer
const config_hidden = {
inputShape:[4],
activation:'sigmoid',
units:4
}
//config for the output layer
const config_output={
units:1,
activation:'sigmoid'
}
//defining the hidden and output layer
const hidden = tf.layers.dense(config_hidden);
const output = tf.layers.dense(config_output);
//adding layers to model
model.add(hidden);
model.add(output);
//define an optimizer
const optimize=tf.train.sgd(0.1);
//config for model
const config={
optimizer:optimize,
loss:'meanSquaredError'
}
//compiling the model
model.compile(config);
//Dummy training data
const x_train = tf.tensor([
[1,0,0,3], [0,0,3,0], [1,0,0,0], [0,1,0,4],
[0,0,0,1], [2,0,2,1], [2,4,1,0], [0,2,0,1],
[1,1,1,0], [2,2,2,1], [0,0,1,0], [0,2,2,1],
[1,0,0,0], [0,1,0,0], [1,1,1,0], [2,2,2,2],
[2,5,7,9], [2,1,0,10], [22,5,7,9], [2,0,3,1],
[1,1,1,1], [2,2,2,2], [0,5,8,1], [5,5,8,1],
[3,4,1,5], [1,0,3,1], [5,5,1,0], [4,2,6,0],
[1,0,0,0], [1,1,2,1], [1,3,2,1], [1,2,0,0],
[1,0,0,2], [0,0,0,7], [0,1,0,0], [5,0,0,0],
[0,4,0,0], [1,0,7,0], [3,2,8,1], [0,10,9,0]
]);
//Dummy training labels
const y_train = tf.tensor([
[0.31], [0.23], [0.08], [0.38],
[0.07], [0.31], [0.44], [0.18],
[0.19], [0.44], [0.06], [0.31],
[0.08], [0.08], [0.23], [0.61],
[0.27], [0.15], [0.51], [0.07],
[0.09], [0.18], [0.31], [0.42],
[0.32], [0.12], [0.27], [0.29],
[0.07], [0.31], [0.44], [0.18],
[0.19], [0.44], [0.06], [0.31],
[0.09], [0.18], [0.31], [0.42]
]);
//Dummy testing data
const x_test = tf.tensor([
[1,0,0,1], [0,1,1,0], [2,0,1,2], [0,0,0,1]
]);
// expected result: [0.20], [0.20], [0.50], [0.10]

await model.fit(x_train, y_train, {
batchSize: 1,
epochs: 5000
});
// Test the model and display output 
document.getElementById("output").innerText = model.predict(x_test);
}
predictOutput();
</script>

如果线性方程已知,即y = 1/15x,则可以使用以下函数直接计算输出

const calc = (t) => t.add(tf.scalar(1/15))

如果斜率a和偏差b现在是预先已知的并且需要计算,则可以使用感知器神经网络。模型的inputShape是错误的,因为要对单个值进行预测。它应该是[1]而不是4。至于x_test,它应该看起来像一个长度为1的数组,这意味着:[[1], [2], [3], [56], [49]]

此外,使用较小的学习率可能会提高的准确性

编辑:

如果模型必须对输入的总和进行预测,那么inputShape应该是[4]。您的输入值很小,这会影响使用感知器模型的错误损失。在将数据提供给模型之前,可以处理例如乘以10的输入值。因此,模型预测将是正确值的10倍。

const model = tf.sequential()
model.add(tf.layers.dense({inputShape: [4], units: 1 }))
model.add(tf.layers.dense({units: 1}))
const x_train = tf.tensor([
[1,0,0,3], [0,0,3,0], [1,0,0,0], [0,1,0,4], [0,0,0,1]]);
const y = tf.tensor([
[0.31], [0.23], [0.08], [0.38],[0.07]
]);
const y_train = y.mul(tf.scalar(10))
const optimizer = tf.train.sgd( 0.01 )
model.compile({optimizer: optimizer, loss: 'meanSquaredError' })
model.fit(x_train, y_train, {epochs: 1000, 
callbacks: {
onEpochEnd: (epoch, log) => {
console.log(epoch, log.loss);
if (epoch === 100 ) {
model.optimizer.setLearningRate(.001);
}
}
}}).then(() => {
const y_predict = model.predict(x_train)
const y_correct = y_predict.div(tf.scalar(10)).print()
})
<html>
<head>
<!-- Load TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.13.3"> </script>
</head>
<body>
</body>
</html>

最新更新