What is EmbeddingBag in pytorch

This recipe explains what is EmbeddingBag in pytorch

Recipe Objective

What is EmbeddingBag in pytorch?

Here the EmbeddingBag is nothing but a function which computes the means or sums of "bags" of embeddings, without noticing the intermediate embeddings. There are no "per_sample_weights" for the bags with constant length, as there are some classes:
-- with the mode = "sum" which is similar to the embedding followed by "torch.sum(dim=0)"
-- with the mode = "mean" which is similar to the embedding followed by "torch.mean(dim=0)"
-- with the mode = "max" which is similar to the embedding followed by "torch.max(dim=0)" As using the "EmbeddingBag" is always better than using this chain of operations, the EmbeddingBag is time and memory effici

PyTorch vs Tensorflow - Which One Should You Choose For Your Next Deep Learning Project ?

ent than these operations.

Step 1 - Import library

import torch

Step 2 - Take embedding module

module_embedding = torch.nn.EmbeddingBag(15, 3, mode="sum")
print("This is a Embedding module which is containing 15 tensors of size 3 and the mode of operation is Sum here:", module_embedding)

This is a Embedding module which is containing 15 tensors of size 3 and the mode of operation is Sum here: EmbeddingBag(15, 3, mode=sum)

Step 3 - Input for embedding bag

input_data = torch.LongTensor([10,20,30,40,50,60,70,80,90,100,110,120,130,140,150])
offsets_data = torch.LongTensor([0,5])

Step 4 - Apply Embedding

print("This is the output for applying embedding bag on input data:","\n",module_embedding(input_data, offsets_data))

This is the output for applying embedding bag on input data: 
 tensor([[5.4618e-35, 0.0000e+00, 4.2039e-44],
        [0.0000e+00,        nan, 1.6255e-43]], grad_fn=)

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP

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.

Build Portfolio Optimization Machine Learning Models in R
Machine Learning Project for Financial Risk Modelling and Portfolio Optimization with R- Build a machine learning model in R to develop a strategy for building a portfolio for maximized returns.

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Build a Review Classification Model using Gated Recurrent Unit
In this Machine Learning project, you will build a classification model in python to classify the reviews of an app on a scale of 1 to 5 using Gated Recurrent Unit.

Locality Sensitive Hashing Python Code for Look-Alike Modelling
In this deep learning project, you will find similar images (lookalikes) using deep learning and locality sensitive hashing to find customers who are most likely to click on an ad.

Build Deep Autoencoders Model for Anomaly Detection in Python
In this deep learning project , you will build and deploy a deep autoencoders model using Flask.

Build an Image Segmentation Model using Amazon SageMaker
In this Machine Learning Project, you will learn to implement the UNet Architecture and build an Image Segmentation Model using Amazon SageMaker