Tensorflow.js tf.tensor3d() Function
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 .tensor3d() function is used to create a new 3-dimensional tensor with the parameters namely value, shape, and datatype.
tf.tensor3d (value, shape, datatype)
- value: The value of the tensor which can be 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 3-dimensional.
Note: The 3d tensor functionality can also be achieved using tf.tensor() function, but using tf.tensor3d() makes the code easily understandable and readable.
Here, we are creating a 3d tensor and printing it. For creating a 3d tensor we are using the .tensor3d() function, and we use .print() function to print the tensor. Here, we will pass the 3d array (i.e. nested array) to the value parameter.
Tensor [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
Here, in this example, we are creating the tensor where we are passing the flat array and specifying the shape parameter of the tensor. We will see the usage of shape parameter here.
Tensor [[[1 , 2 ], [3 , 4 ], [5 , 6 ]], [[7 , 8 ], [9 , 10], [11, 12]]]
In the above example, we have created a tensor of dimensions 2 x 3 x 2.
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.
Tensor [[['C' , 'C++' ], ['Java', 'Python']], [['PHP' , 'JS' ], ['SQL' , 'React' ]]]
In the above example, the printed values of the tensor are of string datatype.