Advanced Selection from Tensors in Pytorch

-

Using torch.index_select, torch.gather and torch.take

In some situations, you’ll have to do some advanced indexing / selection with Pytorch, e.g. answer the query: “how can I choose elements from Tensor A following the indices laid out in Tensor B?”

On this post we’ll present the three commonest methods for such tasks, namely torch.index_select, torch.gather and torch.take. We’ll explain all of them intimately and contrast them with each other.

Foto von Jerin J auf Unsplash

Admittedly, one motivation for this post was me forgetting how and when to make use of which function, ending up googling, browsing Stack Overflow and the, in my view, relatively temporary and never too helpful official documentation. Thus, as mentioned, we here do a deep dive into these functions: we motivate when to make use of which, give examples in 2- and 3D, and show the resulting selection graphically.

I hope this post will bring clarity about said functions and take away the necessity for further exploration — thanks for reading!

And now, without further ado, let’s dive into the functions one after the other. For all, we first start with a 2D example and visualize the resulting selection, after which move to somewhat more complex example in 3D. Further, we re-implement the executed operation in easy Python — s.t. you’ll be able to have a look at pseudocode as one other source of data what these functions do. In the long run, we summarize the functions and their differences in a table.

torch.index_select selects elements along one dimension, while keeping the opposite ones unchanged. That’s: keep all elements from all other dimensions, but pick elements within the goal dimensions following the index tensor. Let’s display this with a 2D example, by which we select along dimension 1:

num_picks = 2

values = torch.rand((len_dim_0, len_dim_1))
indices = torch.randint(0, len_dim_1, size=(num_picks,))
# [len_dim_0, num_picks]
picked = torch.index_select(values, 1, indices)

The resulting tensor has shape [len_dim_0, num_picks]: for each element along dimension 0, we’ve picked the identical element from dimension 1. Let’s visualize this:

admin

What are your thoughts on this topic?
Let us know in the comments below.

1 COMMENT

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
temp mail
temp mail
1 month ago

Although I believe every thought you have for your post is excellent and will undoubtedly be successful, the postings are too brief for new readers. Maybe you could extend them a little bit the next time? I’m grateful for the post.

Share this article

Recent posts

MS invests KRW 4 trillion to strengthen Japan's AI and cloud… “The most important investment in Japan”

Microsoft (MS) plans to speculate $2.9 billion (about 4 trillion won) over two years to strengthen cloud computing and artificial intelligence (AI) infrastructure in...

Revolutionizing AI with Apple’s ReALM: The Way forward for Intelligent Assistants

Within the ever-evolving landscape of artificial intelligence, Apple has been quietly pioneering a groundbreaking approach that would redefine how we interact with our Iphones....

Microsoft attempts to sell open AI ‘Dali’ as a military tool

MS attempted to sell open AI 'Dali' as a military tool It was revealed that Myrosoft (MS) attempted to sell OpenAI's image-generating artificial intelligence...

Advanced Code Generation With LLMs — Constructing a Synthetic Data Generator

Applying the 6 steps of the INSPIRe framework to speed up your code generation (ChatGPT-4 — Claude 3 — Gemini)Imagine generated by the writer.I’ve...

“Crazy” response to the launch of music creation AI ‘Udio’

https://www.youtube.com/watch?v=aH8hOcq5J4g A latest AI that generates music so realistic that it is known as a rival to the favored music-generating artificial intelligence (AI) 'Suno' has...

Recent comments

binance тркелгсн жасау on One other homework left by ‘Chat GPT’…’Paid Search’
Vytvorenie úctu na binance on DALL·E now available in beta
Создать бесплатную учетную запись on AI isn’t here to exchange “me”, it’s here to exchange “you”
бнанс рестраця для США on Generative AI also changes the metaverse
Logar temizleme Ümraniye on Start using ChatGPT immediately
Учетная запись в binance on AI-written critiques help humans notice flaws
Ümraniye lavabo tıkanıklığı açma uzman servisi on A flying BMW…can fly 1000km on a runway
Зарегистрироваться в binance on Generative AI Appears… Who Is Nvidia?
hadise on
Şişli su tesisatçıları güvenilir mi on “Foreign students also take Korean language seminar classes.”
Petek temizleme fiyatları Şişli on Transformers: How Do They Transform Your Data?
biolean reviews on Track Your ML Experiments
откриване на профил в binance on Welcome to Discovery —Aimlabs’ generative AI for gaming.
Kanalizasyon sistemi temizleme Üsküdar on Random Walks Are Strange and Beautiful
Tıkalı lavabo açma servisi Üsküdar on Random Walks Are Strange and Beautiful
Beşiktaş su kaçağı uzmanı on Evolving Chess Puzzles
бнанс Створити акаунт on At Upfront Summit 2023, AI is the omnipresent celebrity
Регистрация на binance on 7 Concepts You Must Understand AI
Kadıköy Mutfak ve Lavabo Kanal Açma on When Do You Self Join? A Handy Trick
binance "oppna konto on OpenAI, ‘ChatGPT’ API released
Създаване на профил в binance on What Should Be Considered When Making a Custom Dataset for Working with YOLO?
kadıköy Noktasal Su Kaçağı bulma on Differentiable and Accelerated Spherical Harmonic Transforms
Ustvarite brezplacen racun on Our approach to alignment research
Joint Plus CBD reviews on An Overview of the LoRA Family
най-добър binance Препоръчителен код on Why you shouldn’t trust AI serps
Cel mai bun cod de recomandare Binance on Program teaches US Air Force personnel the basics of AI
開設binance帳戶 on Earndrop With DripDropz
Lumikha ng Binance Account on Introduction to Python for Data Science
Pieregistrējieties, lai sanemtu 100 USDT on Chinese tech giant Baidu just released its answer to ChatGPT
Stuart Jacobs on OpenAI and Elon Musk
binance us registrácia on The Path to AI Maturity – 2023 LXT Report
Do NeuroTest work on The Stacking Ensemble Method
AeroSlim Weight loss price on NIA holds AI Ethics Idea Contest Awards Ceremony
skapa binance-konto on LLMs and the Emerging ML Tech Stack
бнанс рестраця для США on Model Evaluation in Time Series Forecasting
Bonus Pendaftaran Binance on Meet Our Fleet
Créer un compte gratuit on About Me — How I give AI artists a hand
To tài khon binance on China completely blocks ‘Chat GPT’
Regístrese para obtener 100 USDT on Reducing bias and improving safety in DALL·E 2
crystal teeth whitening on What babies can teach AI
binance referral bonus on DALL·E API now available in public beta
www.binance.com prihlásení on Neural Networks and Life
Büyü Yapılmışsa Nasıl Bozulur on Introduction to PyTorch: from training loop to prediction
yıldızname on OpenAI Function Calling
Kısmet Bağlılığını Çözmek İçin Dua on Examining Flights within the U.S. with AWS and Power BI
Kısmet Bağlılığını Çözmek İçin Dua on How Meta’s AI Generates Music Based on a Reference Melody
Kısmet Bağlılığını Çözmek İçin Dua on ‘이루다’의 스캐터랩, 기업용 AI 시장에 도전장
uçak oyunu bahis on Thanks!
para kazandıran uçak oyunu on Make Machine Learning Work for You
medyum on Teaching with AI
aviator oyunu oyna on Machine Learning for Beginners !
yıldızname on Final DXA-nation
adet kanı büyüsü on ‘Fake ChatGPT’ app on the App Store
Eşini Eve Bağlamak İçin Dua on LLMs and the Emerging ML Tech Stack
aviator oyunu oyna on AI as Artist’s Augmentation
Büyü Yapılmışsa Nasıl Bozulur on Some Guy Is Trying To Turn $100 Into $100,000 With ChatGPT
Eşini Eve Bağlamak İçin Dua on Latest embedding models and API updates
Kısmet Bağlılığını Çözmek İçin Dua on Jorge Torres, Co-founder & CEO of MindsDB – Interview Series
gideni geri getiren büyü on Joining the battle against health care bias
uçak oyunu bahis on A faster method to teach a robot
uçak oyunu bahis on Introducing the GPT Store
para kazandıran uçak oyunu on Upgrading AI-powered travel products to first-class
para kazandıran uçak oyunu on 10 Best AI Scheduling Assistants (September 2023)
aviator oyunu oyna on 🤗Hugging Face Transformers Agent
Kısmet Bağlılığını Çözmek İçin Dua on Time Series Prediction with Transformers
para kazandıran uçak oyunu on How China is regulating robotaxis
bağlanma büyüsü on MLflow on Cloud
para kazandıran uçak oyunu on Can The 2024 US Elections Leverage Generative AI?
Canbar Büyüsü on The reverse imitation game
bağlanma büyüsü on The NYU AI School Returns Summer 2023
para kazandıran uçak oyunu on Beyond ChatGPT; AI Agent: A Recent World of Staff
Büyü Yapılmışsa Nasıl Bozulur on The Murky World of AI and Copyright
gideni geri getiren büyü on ‘Midjourney 5.2’ creates magical images
Büyü Yapılmışsa Nasıl Bozulur on Microsoft launches the brand new Bing, with ChatGPT inbuilt
gideni geri getiren büyü on MemCon 2023: We’ll Be There — Will You?
adet kanı büyüsü on Meet the Fellow: Umang Bhatt
aviator oyunu oyna on Meet the Fellow: Umang Bhatt
abrir uma conta na binance on The reverse imitation game
código de indicac~ao binance on Neural Networks and Life
Larry Devin Vaughn Wall on How China is regulating robotaxis
Jon Aron Devon Bond on How China is regulating robotaxis
otvorenie úctu na binance on Evolution of Blockchain by DLC
puravive reviews consumer reports on AI-Driven Platform Could Streamline Drug Development
puravive reviews consumer reports on How OpenAI is approaching 2024 worldwide elections
www.binance.com Registrácia on DALL·E now available in beta