Tensorflow.js tf.expandDims() Function
Last Updated :
25 May, 2021
Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.
The .expandDims() function is used to find tf.Tensor object whose rank is expanded with the help of tensor’s shape by inserting a dimension into it.
Syntax:
tf.expandDims(x, axis?)
Parameters:
- x: It is the stated tensor input whose size is to be extended, and it can be of type tf.Tensor, TypedArray, or Array.
- axis: It is the stated size index at which the shape of 1 is to be inserted. The by default value is zero i.e. the first dimension, and it is of type number.
Return Value: It returns tf.Tensor object.
Example 1:
Javascript
import * as tf from "@tensorflow/tfjs"
const y = tf.tensor1d([5, 6, 7, 8]);
const axs = 1;
y.expandDims(axs).print();
|
Output:
Tensor
[[5],
[6],
[7],
[8]]
Example 2:
Javascript
import * as tf from "@tensorflow/tfjs"
tf.expandDims(tf.tensor1d([1.2, 3.5, 7.6, 9.7]), 1).print();
|
Output:
Tensor
[[1.2 ],
[3.5 ],
[7.5999999],
[9.6999998]]
Reference: https://js.tensorflow.org/api/latest/#expandDims
Share your thoughts in the comments
Please Login to comment...