admin管理员组文章数量:1422437
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
5 Answers
Reset to default 2I 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>
本文标签: javascriptGetting the result into a variableStack Overflow
版权声明:本文标题:javascript - Getting the result into a variable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745359560a2655232.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论