Tensorflow.js tf.tensor5d() Function
Last Updated :
25 May, 2021
Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment.
The .tensor5d() function is used to create a new 5-dimensional tensor with the parameters namely value, shape, and datatype.
Syntax :
tf.tensor5d(value, shape?, dataType?)
Parameters:
- value: The value of the tensor which can be a nested array of numbers, or a flat array, or a TypedArray.
- shape: It takes the shape of the tensor. The tensor will infer its shape from the value if it is not provided. It is an optional parameter.
- datatype: It can be a ‘float32’ or ‘int32’ or ‘bool’ or ‘complex64’ or ‘string’ value. It is an optional parameter.
Return Value: It returns the tensor of the same data type. The returned tensor will always be 5-dimensional.
Note: The 5d tensor functionality can also be achieved using the tf.tensor() function, but using tf.tensor5d() makes the code easily understandable and readable.
Example 1: Here, we are creating a 5d tensor and printing it. For creating a 5d tensor, we are using the .tensor5d() function, and we use .print() function to print the tensor. Here, we will pass the 5d array (i.e. nested array) to the value parameter.
Javascript
import * as tf from "@tensorflow/tfjs" ;
let example1 = tf.tensor5d([[[
[[1, 3], [2, 8]],
[[3, 9], [4, 2]]
]]]);
example1.print()
|
Output:
Tensor
[[[[[1, 3],
[2, 8]],
[[3, 9],
[4, 2]]]]]
Example 2:
In the above example, we are creating the tensor where we are passing the flat array and specifying the shape parameter of the tensor. Here we will see the usage of the shape parameter.
Javascript
import * as tf from "@tensorflow/tfjs"
var value = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
var shape = [1, 2, 6, 1, 1];
let example2 = tf.tensor5d(value, shape);
example2.print();
|
Output:
Tensor
[[[ [[1 ],],
[[2 ],],
[[3 ],],
[[4 ],],
[[5 ],],
[[6 ],]],
[ [[7 ],],
[[8 ],],
[[9 ],],
[[10],],
[[11],],
[[12],]]]]
Example 3:
Here, in this example, we will create a tensor by specifying the value, shape, and datatype. We will create the tensor where all the values are of string datatype.
Javascript
import * as tf from "@tensorflow/tfjs" ;
var value = [ "C" , "C++" , "Java" , "Python" ,
"PHP" , "JS" , "SQL" , "React" ];
var shape = [1, 2, 4, 1,1 ];
var example3 = tf.tensor5d(value, shape);
example3.print();
|
Output:
Tensor
[[[ [['C' ],],
[['C++' ],],
[['Java' ],],
[['Python'],]],
[ [['PHP' ],],
[['JS' ],],
[['SQL' ],],
[['React' ],]]]]
Reference: https://js.tensorflow.org/api/latest/#tensor5d
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...