如何编码多个输入和多个输出

数据挖掘 张量流 javascript
2022-02-08 22:44:17

我有一个比我现在能理解的更复杂的数学,一个动态图形可视化。它使用弹簧和磁铁的物理隐喻,其中顶点充当相互排斥的磁铁,而边缘充当将顶点拉回到一起的弹簧。这个模拟目前有六个工作参数,我每隔一段时间从运行模拟中绘制出来。

我正在尝试找到生成稳定可视化所需的模拟设置。

到目前为止,我的方法是为固定排列(简单的四面体)制作一组设置和一组边长。我不完全确定我的目标是什么,因为我对机器学习还是很陌生,但我认为通过在数据收集运行期间随机化设置,我将能够预测边缘的长度一组给定的设置,然后使用回归找到与稳定边缘长度相对应的设置。

为此,我开始改编 Google 的Tensorflow.js 教程,跳过第一个数据可视化部分,因为我有 6 个设置输入和 6 个边长标签。

该模型如下所示:


  function createModel() {
    // Create a sequential model
    const model = tf.sequential(); 

    // Add a single hidden layer
    model.add(tf.layers.dense({inputShape: [6], units: 1, useBias: true}));

    // Add an output layer
    model.add(tf.layers.dense({ units: 6, useBias: true}));

    return model;
  }

在图形可视化“爆炸”后,我让模型训练,即添加边后顶点从屏幕上消失,沿途记录边的长度。但我被困在 testModel 功能上:

function testModel(model, inputData, normalizationData) {
    console.log('inputData.shape', inputData.shape);

    const {inputMax, inputMin, labelMin, labelMax} = normalizationData;  

    // Generate predictions for a uniform range of numbers between 0 and 1;
    // We un-normalize the data by doing the inverse of the min-max scaling 
    // that we did earlier.
    const [xs, preds] = tf.tidy(() => {

      const xs = tf.linspace(0, 1, 100);
      console.log('xs', xs)
      const preds = model.predict(xs.reshape([null, 6]));      

      const unNormXs = xs
        .mul(inputMax.sub(inputMin))
        .add(inputMin);

      const unNormPreds = preds
        .mul(labelMax.sub(labelMin))
        .add(labelMin);

      // Un-normalize the data
      return [unNormXs.dataSync(), unNormPreds.dataSync()];
    });


    const predictedPoints = Array.from(xs).map((val, i) => {
      return {x: val, y: preds[i]}
    });

    const originalPoints = inputData.map(d => ({
      x: d.settings, y: d.lengths,
    }));


    tfvis.render.scatterplot(
      {name: 'Model Predictions vs Original Data'}, 
      {values: [originalPoints, predictedPoints], series: ['original', 'predicted']}, 
      {
        xLabel: 'settings',
        yLabel: 'edge lengths',
        height: 300
      }
    );
  }

但具体来说,正是这些行引发了错误:

const xs = tf.linspace(0, 1, 100);
console.log('xs', xs)
const preds = model.predict(xs.reshape([null, 6]));   

(重塑!)

所以我现在的问题是:我如何告诉 tensorflow 给我一个包含 6 个随机值的列表来预测函数,而不仅仅是一个值?

提前致谢!

完整的示例目前位于此 github 页面(点击 Construct Pyramid 并等待可视化炸毁,然后 tensorflow 将接管。)

也许整个方法是错误的,欢迎任何反馈。

0个回答
没有发现任何回复~