tf.expandDims(): adding a new dimension on tensors in TensorFlow,js
- Jorge Guerra Pires
- Mar 29, 2024
- 1 min read
Certainly! The tf.expandDims() function in TensorFlow.js is used to modify the shape of a tensor by inserting an additional dimension at a specified position. Let’s break down the details:
Function Signature:
tf.expandDims(x, axis?)
Parameters:
x: The input tensor whose size you want to extend. It can be of type tf.Tensor, TypedArray, or Array.
axis: The index at which a new dimension of size 1 will be inserted. By default, it’s the first dimension (axis 0).
Purpose:
The purpose of tf.expandDims() is to increase the rank (number of dimensions) of a tensor by adding a new dimension.
Example Usage:
import * as tf from "@tensorflow/tfjs"; // Example 1: Expand a 1D tensor const y = tf.tensor1d([5, 6, 7, 8]); const axs = 1; // Insert a new dimension at axis 1 const expandedY = y.expandDims(axs); expandedY.print(); // Output: Tensor [[5], [6], [7], [8]] // Example 2: Expand a 1D tensor with decimal values const decimalTensor = tf.tensor1d([1.2, 3.5, 7.6, 9.7]); const expandedDecimalTensor = tf.expandDims(decimalTensor, 1); expandedDecimalTensor.print(); // Output: Tensor [[1.2], [3.5], [7.6], [9.7]]
Reference:
Remember that tf.expandDims() is useful when you need to adjust the shape of a tensor to match the requirements of a specific operation or layer in your machine learning model. Feel free to experiment with different axes and tensor shapes to see how it affects your data! 🚀
Learn more: https://sl.bing.net/eBa18rXeSqG
Comentarios