Tensorflow.js tf.tensor4d() Function
Last Updated :
12 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 .tensor4d() function is used to create a new 4-dimensional tensor with the parameters namely value, shape, and datatype.
Syntax:
tf.tensor4d(value, shape, dataType)
Parameters: This function accepts the following three parameters.
- 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 4-dimensional.
Note: The 4d tensor functionality can also be achieved using tf.tensor() function, but using tf.tensor4d() makes the code easily understandable and readable.
Example 1: Here, we are creating a 4d tensor and printing it. For creating a 4d tensor we are using the .tensor4d() function, and we use .print() function to print the tensor. Here, we will pass the 4d array (i.e. nested array) to the value parameter.
Javascript
import * as tf from "@tensorflow/tfjs" ;
let example1 = tf.tensor4d([[
[[1, 3], [2, 8]],
[[3, 9], [4, 2]]
]]);
example1.print()
|
Output:
Tensor
[[[[1 , 3],
[20, 8]],
[[3 , 9],
[4 , 2]]]]
Example 2: 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.
Javascript
import * as tf from "@tensorflow/tfjs"
const value = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
const shape = [1, 2, 6, 1];
let example2 = tf.tensor4d(value, shape);
example2.print();
|
Output:
Tensor
[[[[1 ],
[2 ],
[3 ],
[4 ],
[5 ],
[6 ]],
[[7 ],
[8 ],
[9 ],
[10],
[11],
[12]]]]
In the above example, we have created a tensor of dimensions 1 x 2 x 6 x 1.
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" ;
const value = [ "C" , "C++" , "Java" , "Python" ,
"PHP" , "JS" , "SQL" , "React" ];
const shape = [1, 2, 4, 1];
let example3 = tf.tensor4d(value, shape);
example3.print();
|
Output:
Tensor
[[[['C' ],
['C++' ],
['Java' ],
['Python']],
[['PHP' ],
['JS' ],
['SQL' ],
['React' ]]]]
Reference: https://js.tensorflow.org/api/latest/#tensor4d
Similar Reads
Tensorflow.js tf.tensor2d() 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 .tensor2d() function is used to create a new 2-dimensional tensor with the parameter namely value, shape, and datatype. Syntax: tf
3 min read
Tensorflow.js tf.tensor() 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 .tensor() function is used to create a new tensor with the help of value, shape, and data type. Syntax : tf.tensor( value, shape,
3 min read
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. Syntax: t
3 min read
Tensorflow.js tf.tensor5d() 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 .tensor5d() function is used to create a new 5-dimensional tensor with the parameters namely value, shape, and datatype. Syntax :
3 min read
Tensorflow.js tf.tensor6d() 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 .tensor6d() function is used to create a new 6-dimensional tensor with the parameters namely value, shape, and datatype. Syntax: t
3 min read
Tensorflow.js tf.norm() 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 tf.norm() function is used compute the norm of matrices, vectors, and scalar. This function can also compute several other vector
2 min read
Tensorflow.js tf.tan() Function
Tensorflow.js is an open-source library that is being developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment. The .tan() function is used to find the tangent of the stated tensor input and is done element wise. Syntax: tf.t
2 min read
Tensorflow.js tf.prod() 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 tf.prod() function is used to calculate product of the elements of a specified Tensor across its dimension. It reduces the given i
2 min read
Tensorflow.js tf.step() 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 tf.step() function is used to return the step of the input tensor's elements i.e. it returns 1 if the element is greater than 0 el
1 min read
Tensorflow.js tf.ones() Function
Tensorflow.js is an open-source library for running machine learning models and deep learning neural networks in the browser or node environment. The tf.ones() function is used to create a new tensor where all elements are set to 1. Syntax: tf.ones(shape, dtype, name) Parameters: shape: It takes the
2 min read