TensorFlow.js Tutorial (Cont.)
Retrieving Tensor Values
You can get the data behind a tensor using
tensor
.
data
()
method:
<html> <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script> <body> <div id="demo" /> <script type="text/javascript"> const myArr = [[1, 2], [3, 4]]; const tensorA = tf.tensor(myArr); tensorA.data().then(data => display(data)); // Result: 1,2,3,4 function display(data) { document.getElementById("demo").innerHTML = data; } </script> </body> </html>
You can get the array behind a tensor using
tensor
.
array
()
method:
<html> <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script> <body> <div id="demo" /> <script type="text/javascript"> const myArr = [[1, 2], [3, 4]]; const shape = [2, 2]; const tensorA = tf.tensor(myArr, shape); tensorA.array().then(array => display(array[0])); // Result: 1,2 function display(data) { document.getElementById("demo").innerHTML = data; } </script> </body> </html>
<html> <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script> <body> <div id="demo" /> <script type="text/javascript"> const myArr = [[1, 2], [3, 4]]; const shape = [2, 2]; const tensorA = tf.tensor(myArr, shape); tensorA.array().then(array => display(array[1])); // Result: 3,4 function display(data) { document.getElementById("demo").innerHTML = data; } </script> </body> </html>
You can get the rank of a tensor using
tensor
.
rank
()
method:
<html> <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script> <body> <div id="demo" /> <script type="text/javascript"> const myArr = [1, 2, 3, 4]; const shape = [2, 2]; const tensorA = tf.tensor(myArr, shape); document.getElementById("demo").innerHTML = tensorA.rank; </script> </body> </html>