mirror of
https://github.com/pytorch/pytorch.git
synced 2026-11-20 09:04:25 +08:00
19da3da3d3
The fix-cpp-build branch is a major restructuring that: - Drops Exhale (which auto-generated API docs but caused OOM in CI) in favor of hand-written RST pages using Breathe directives - Reorganizes notes into a hierarchical API reference with proper categories - Adds new content: XPU docs, stable ABI docs, DeviceGuard/Stream docs - Always runs Doxygen instead of gating on an env var Pull Request resolved: https://github.com/pytorch/pytorch/pull/174096 Approved by: https://github.com/janeyx99, https://github.com/seemethere, https://github.com/albanD
56 lines
1.3 KiB
Markdown
56 lines
1.3 KiB
Markdown
---
|
|
myst:
|
|
html_meta:
|
|
description: Embedding layers in PyTorch C++ — Embedding and EmbeddingBag for sparse and dense lookups.
|
|
keywords: PyTorch, C++, Embedding, EmbeddingBag, lookup table, sparse, NLP
|
|
---
|
|
|
|
# Embedding Layers
|
|
|
|
Embedding layers map discrete tokens (words, categories, IDs) to dense vector
|
|
representations. They are the foundation of NLP models and recommendation systems.
|
|
|
|
- **Embedding**: Standard lookup table that maps indices to dense vectors
|
|
- **EmbeddingBag**: Computes sums or means of embeddings (efficient for sparse features)
|
|
|
|
**Key parameters:**
|
|
|
|
- `num_embeddings`: Size of the vocabulary (number of unique tokens)
|
|
- `embedding_dim`: Dimension of each embedding vector
|
|
- `padding_idx`: Index that outputs zeros (useful for padding tokens)
|
|
|
|
## Embedding
|
|
|
|
```{doxygenclass} torch::nn::Embedding
|
|
:members:
|
|
:undoc-members:
|
|
```
|
|
|
|
```{doxygenclass} torch::nn::EmbeddingImpl
|
|
:members:
|
|
:undoc-members:
|
|
```
|
|
|
|
**Example:**
|
|
|
|
```cpp
|
|
auto embedding = torch::nn::Embedding(
|
|
torch::nn::EmbeddingOptions(10000, 256) // num_embeddings, embedding_dim
|
|
.padding_idx(0));
|
|
|
|
auto indices = torch::tensor({1, 2, 3, 4});
|
|
auto embedded = embedding->forward(indices); // [4, 256]
|
|
```
|
|
|
|
## EmbeddingBag
|
|
|
|
```{doxygenclass} torch::nn::EmbeddingBag
|
|
:members:
|
|
:undoc-members:
|
|
```
|
|
|
|
```{doxygenclass} torch::nn::EmbeddingBagImpl
|
|
:members:
|
|
:undoc-members:
|
|
```
|