javascript - Getting the result into a variable - Stack Overflow

I have the following example code. I'm able to see the correct result in the console from the prin

I have the following example code. I'm able to see the correct result in the console from the print function.

  // 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.
  modelpile({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()

  });

What I'd like to be able to do is to put answer into a number var so that I can use it elsewhere. The answer I get is:

Tensor [[4.9999123],]

But I'd like to get the 4.9999 into a variable so that I can round it up to 5 and print it on screen (in html).

I have the following example code. I'm able to see the correct result in the console from the print function.

  // 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.pile({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()

  });

What I'd like to be able to do is to put answer into a number var so that I can use it elsewhere. The answer I get is:

Tensor [[4.9999123],]

But I'd like to get the 4.9999 into a variable so that I can round it up to 5 and print it on screen (in html).

Share Improve this question edited Aug 14, 2018 at 17:27 edkeveked 18.4k10 gold badges59 silver badges95 bronze badges asked Apr 24, 2018 at 11:20 K-DawgK-Dawg 3,3293 gold badges38 silver badges54 bronze badges 1
  • Any help would be appreciated. – K-Dawg Commented Apr 24, 2018 at 11:20
Add a ment  | 

5 Answers 5

Reset to default 2

I found the answer to be:

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

answer has a data method which returns a promise. You can get the data from the promise.

I searched stackoverflow which lead me to this question: Get data from 2D tensor with tensorflow js

Rocksetta kindly posted a link to their code on the following site:

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

The easiest way is to use answer.dataSync(), but it will block the main thread. If you are fortable with async / await, answer.data() is the solution.

Sometimes

The easiest way is to use answer.dataSync(), but it will block the main thread. If you are fortable with async / await, answer.data() is the solution.

works fine but other times

answer.dataSync()

returns an array. When faced with the array then you need to try

answer.dataSync()[0]

or some other array number. Same issue with

await answer.data()[0]

Here's my favorite way:

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

answerArray will be flattened, but it's fast and simple. You're usually in an async function anyways if you're loading a Keras model or doing a variety of other async things.

To get the value of a Tensor into normal JavaScript variable, one can use two built-in functions of TensorflowJs: one is synchronous dataSync() and the other is asynchronous data().

dataSync() will block the UI thread. So whenever possible the asynchronous data() should be preferred.

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./ajax/libs/tensorflow/0.12.4/tf.js"> </script>
  </head>

  <body>
  </body>
</html>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745359560a4624297.html

相关推荐

  • javascript - Getting the result into a variable - Stack Overflow

    I have the following example code. I'm able to see the correct result in the console from the prin

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信