How to expand dimensions of a tensor

This recipe helps you expand dimensions of a tensor

Recipe Objective

How to expand dimensions of a tensor?

This is achieved using the function "tf.expand_dims" available in tensorflow. This function will return a tensor with a length 1 axis inserted at the index axis. At the dimension index axis of input shape a length 1 is inserted by this operation. The dimension in this follows the python indexing rules which is: It's zero based, a negative index it is counted backward from the end.

Applications for this is: It align the axes for broadcasting. for adding a inner vector length axis to a tensor of scalars. It will also add outer batch dimension to a single element.

Step 1 - Import library

import tensorflow as tf

Step 2 - Take a Sample data

Sample_data = tf.ones([20, 20, 4]) print("This is shape of Sample data before expanding the dimensions:",Sample_data.shape)

This is shape of Sample data before expanding the dimensions: (20, 20, 4)

Step 3 - Expand Dimensions

print("This will add a outer batch axis:",tf.expand_dims(Sample_data, axis=0).shape) print("This is for new axis location axis which matches the python list:",tf.expand_dims(Sample_data, axis=1).shape) print("This is for negative axis which adds an inner most dimension:",tf.expand_dims(Sample_data, axis=-1).shape)

This will add a outer batch axis: (1, 20, 20, 4)
This is for new axis location axis which matches the python list: (20, 1, 20, 4)
This is for negative axis which adds an inner most dimension: (20, 20, 4, 1)

{"mode":"full","isActive":false}

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Hands-On Approach to Causal Inference in Machine Learning
In this Machine Learning Project, you will learn to implement various causal inference techniques in Python to determine, how effective the sprinkler is in making the grass wet.

Azure Deep Learning-Deploy RNN CNN models for TimeSeries
In this Azure MLOps Project, you will learn to perform docker-based deployment of RNN and CNN Models for Time Series Forecasting on Azure Cloud.

Build a Text Generator Model using Amazon SageMaker
In this Deep Learning Project, you will train a Text Generator Model on Amazon Reviews Dataset using LSTM Algorithm in PyTorch and deploy it on Amazon SageMaker.

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.

Build a Hybrid Recommender System in Python using LightFM
In this Recommender System project, you will build a hybrid recommender system in Python using LightFM .

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

Predictive Analytics Project for Working Capital Optimization
In this Predictive Analytics Project, you will build a model to accurately forecast the timing of customer and supplier payments for optimizing working capital.

Build CNN Image Classification Models for Real Time Prediction
Image Classification Project to build a CNN model in Python that can classify images into social security cards, driving licenses, and other key identity information.

Model Deployment on GCP using Streamlit for Resume Parsing
Perform model deployment on GCP for resume parsing model using Streamlit App.

Deep Learning Project for Time Series Forecasting in Python
Deep Learning for Time Series Forecasting in Python -A Hands-On Approach to Build Deep Learning Models (MLP, CNN, LSTM, and a Hybrid Model CNN-LSTM) on Time Series Data.