用非线性数据计算方程

时间:2018-11-09 17:32:10

标签: javascript tensorflow tensorflow.js

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

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

I.E。给出了四个张量,如:

[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>

1 个答案:

答案 0 :(得分:0)

如果已知线性方程,即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>

相关问题