◀
Previous
Slide 9.3: TensorFlow.js tutorial
Slide 9.5: Retrieving tensor values
Home
Print version
▶
Next
TensorFlow.js Tutorial (Cont.)
Creating a Tensor
The main data type in TensorFlow is the tensor, which is created from any N-dimensional array with the
tf
.
tensor
()
function:
<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); document.getElementById("demo").innerHTML = tensorA; </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 tensorA = tf.tensor(myArr); document.getElementById("demo").innerHTML = tensorA; </script> </body> </html>
<html> <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script> <body> <div id="demo" /> <script type="text/javascript"> const tensorA = tf.tensor([[1, 2], [3, 4], [5, 6]]); document.getElementById("demo").innerHTML = tensorA; </script> </body> </html>
Tensor Shape
A tensor can also be created from an array and a shape parameter.
<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; </script> </body> </html>
<html> <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script> <body> <div id="demo" /> <script type="text/javascript"> const tensorA = tf.tensor([1, 2, 3, 4], [2, 2]); document.getElementById("demo").innerHTML = tensorA; </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); document.getElementById("demo").innerHTML = tensorA; </script> </body> </html>
◀
Previous
Slide 9.3: TensorFlow.js tutorial
Slide 9.5: Retrieving tensor values
Home
Print version
▶
Next
I want to die peacefully in my sleep, like my grandfather..
Not screaming and yelling like the passengers in his car.