将结果转换为变量

时间:2018-04-24 11:20:40

标签: javascript tensorflow.js

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

  // 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()

  });

我希望能够将答案放入数字var中,以便我可以在其他地方使用它。我得到的答案是:

Tensor [[4.9999123],]

但是我想将4.9999变成一个变量,这样我就可以将其四舍五入并在屏幕上打印(在html中)。

5 个答案:

答案 0 :(得分:2)

最简单的方法是使用answer.dataSync(),但它会阻止主线程。如果您对async / await感到满意,Record ID:23155736 G8N9 - Select User >> Beatriz Pinho ------- 2018-04-26T09:25:55:55.0000000 2 - Question2 >> Answer2 ------- 2018-04-26T09:25:55:55.0000000 3 - Question3 >> Answer3 ------- 2018-04-26T09:25:55:55.0000000 4 - Question4 >> Answer4 ------- 2018-04-26T09:25:55:55.0000000 5 - Question5 >> Answer5 ------- 2018-04-26T09:25:55:55.0000000 6 - Question6 >> Answer6 ------- 2018-04-26T09:25:55:55.0000000 7 - Question7 >> Answer7 ------- 2018-04-26T09:25:55:55.0000000 8 - Question8 >> Answer8 ------- 2018-04-26T09:25:55:55.0000000 9 - Question9 >> Answer9 ------- 2018-04-26T09:25:55:55.0000000 10 - Question10 >> Answer10 ------- 2018-04-26T09:25:55:55.0000000 11 - Question11 >> Answer11 ------- 2018-04-26T09:25:55:55.0000000 Record ID:2 G8N9 - Select User >> Silent Bob ------- 2018-04-26T09:25:55:55.0000000 2 - Question2 >> Answer2 ------- 2018-04-26T09:25:55:55.0000000 3 - Question3 >> Answer3 ------- 2018-04-26T09:25:55:55.0000000 4 - Question4 >> Answer4 ------- 2018-04-26T09:25:55:55.0000000 5 - Question5 >> Answer5 ------- 2018-04-26T09:25:55:55.0000000 6 - Question6 >> Answer6 ------- 2018-04-26T09:25:55:55.0000000 7 - Question7 >> Answer7 ------- 2018-04-26T09:25:55:55.0000000 8 - Question8 >> Answer8 ------- 2018-04-26T09:25:55:55.0000000 9 - Question9 >> Answer9 ------- 2018-04-26T09:25:55:55.0000000 10 - Question10 >> Answer10 ------- 2018-04-26T09:25:55:55.0000000 11 - Question11 >> Answer11 ------- 2018-04-26T09:25:55:55.0000000 就是解决方案。

答案 1 :(得分:2)

有时候

  

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

工作正常,但其他时间

answer.dataSync()

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

answer.dataSync()[0]

或其他一些数组编号。与

相同的问题
await answer.data()[0]

答案 2 :(得分: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

答案 3 :(得分:0)

要将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>

答案 4 :(得分:0)

这是我最喜欢的方式:

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

answerArray将被展平,但是它既快速又简单。无论如何,如果要加载Keras模型或执行其他各种异步操作,通常都处于异步函数中。

相关问题