【问题标题】:Getting the result into a variable将结果放入变量中
【发布时间】:2018-04-24 11:20:40
【问题描述】:

我有以下示例代码。我可以通过打印功能在控制台中看到正确的结果。

  // Define a model for linear regression.
  const model = tf.sequential();
  model.add(tf.layers.dense({units: 1, inputShape: [1]}));
  model.add(tf.layers.dense({units: 4, inputShape: [1]}));
  model.add(tf.layers.dense({units: 10, inputShape: [1]}));

  model.add(tf.layers.dense({units: 1, inputShape: [1]}));  

  // Prepare the model for training: Specify the loss and the optimizer.
  model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});

  // Generate some synthetic data for training.
  const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
  const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);

  // Train the model using the data.
  model.fit(xs, ys).then(() => {
    // Use the model to do inference on a data point the model hasn't seen before:
    // Open the browser devtools to see the output
    answer = model.predict(tf.tensor2d([3], [1, 1]));
    answer.print()

  });

我想做的是将答案放入一个数字变量中,以便我可以在其他地方使用它。我得到的答案是:

Tensor [[4.9999123],]

但我想将 4.9999 放入一个变量中,以便将其四舍五入为 5 并在屏幕上打印(以 html 格式)。

【问题讨论】:

  • 任何帮助将不胜感激。

标签: javascript tensorflow.js


【解决方案1】:

我发现答案是:

    answer.data().then((d)=>{
      console.log(d[0])
    })

answer 有一个返回承诺的数据方法。您可以从 Promise 中获取数据。

我搜索了导致我这个问题的stackoverflow: Get data from 2D tensor with tensorflow js

Rocksetta 在以下网站上发布了他们代码的链接:

https://hpssjellis.github.io/beginner-tensorflowjs-examples-in-javascript/beginner-examples/tfjs02-basics.html

【讨论】:

    【解决方案2】:

    最简单的方法是使用answer.dataSync(),但它会阻塞主线程。如果您对 async / await 感到满意,answer.data() 就是解决方案。

    【讨论】:

    • 谢谢 知道有同步方式也很有用。
    【解决方案3】:

    有时

    最简单的方法是使用 answer.dataSync(),但它会阻塞 主线程。如果您对 async / await 感到满意,请 answer.data() 是解决方案。

    工作正常,但其他时候

    answer.dataSync()
    

    返回一个数组。当面对数组时,你需要尝试

    answer.dataSync()[0]
    

    或其他数组编号。同样的问题

    await answer.data()[0]
    

    【讨论】:

      【解决方案4】:

      这是我最喜欢的方式:

      var answerdata = await answer.data()
      var answerArray = Array.from(answerdata);
      

      answerArray 将被展平,但它又快又简单。如果您正在加载 Keras 模型或执行各种其他异步操作,那么您通常处于异步函数中。

      【讨论】:

        【解决方案5】:

        要将Tensor的值获取到普通的JavaScript变量中,可以使用TensorflowJs的两个内置函数:一个是同步的dataSync(),另一个是异步的data()

        dataSync() 会阻塞 UI 线程。因此,应尽可能首选异步 data()

        const x = tf.tensor1d([45, 48]);
        x.print();
        /* Async way */
        (async () => {
          const val = await x.data()
           // get the first element
          console.log(val[0])
        })()
        /* Sync way */
        const val = x.dataSync()[0]
        console.log(val)
        <html>
          <head>
            <!-- Load TensorFlow.js -->
            <script src="https://cdnjs.cloudflare.com/ajax/libs/tensorflow/0.12.4/tf.js"> </script>
          </head>
        
          <body>
          </body>
        </html>

        【讨论】:

          猜你喜欢
          • 2016-01-07
          • 1970-01-01
          • 2018-11-30
          • 2015-06-22
          • 2015-12-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-17
          相关资源
          最近更新 更多