Title: Embedding Layers — PyTorch main documentation
Description: Embedding layers in PyTorch C++ — Embedding and EmbeddingBag for sparse and dense lookups.
Keywords:
Domain: docs.pytorch.org
{
"@context": "https://schema.org",
"@type": "Article",
"name": "Embedding Layers",
"headline": "Embedding Layers",
"description": "Embedding layers in PyTorch C++ \u2014 Embedding and EmbeddingBag for sparse and dense lookups.",
"url": "/api/nn/embedding.html",
"articleBody": "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# class Embedding : public torch::nn::ModuleHolder\u003cEmbeddingImpl\u003e# A ModuleHolder subclass for EmbeddingImpl. See the documentation for EmbeddingImpl class to learn what methods it provides, and examples of how to use Embedding with torch::nn::EmbeddingOptions. See the documentation for ModuleHolder to learn about PyTorch\u2019s module storage semantics. Public Static Functions static inline Embedding from_pretrained(const torch::Tensor \u0026embeddings, const EmbeddingFromPretrainedOptions \u0026options = {})# See the documentation for torch::nn::EmbeddingFromPretrainedOptions class to learn what optional arguments are supported for this function. class EmbeddingImpl : public torch::nn::Cloneable\u003cEmbeddingImpl\u003e# Performs a lookup in a fixed size embedding table. See https://pytorch.org/docs/main/nn.html#torch.nn.Embedding to learn about the exact behavior of this module. See the documentation for torch::nn::EmbeddingOptions class to learn what constructor arguments are supported for this module. Example: Embedding model(EmbeddingOptions(10, 2).padding_idx(3).max_norm(2).norm_type(2.5).scale_grad_by_freq(true).sparse(true)); Public Functions inline EmbeddingImpl(int64_t num_embeddings, int64_t embedding_dim)# explicit EmbeddingImpl(EmbeddingOptions options_)# virtual void reset() override# reset() must perform initialization of all members with reference semantics, most importantly parameters, buffers and submodules. void reset_parameters()# virtual void pretty_print(std::ostream \u0026stream) const override# Pretty prints the Embedding module into the given stream. Tensor forward(const Tensor \u0026indices)# Performs a lookup on the embedding table stored in weight using the indices supplied and returns the result. Public Members EmbeddingOptions options# The Options used to configure this Embedding module. Changes to EmbeddingOptions after construction have no effect. Tensor weight# The embedding table. Example: 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-\u003eforward(indices); // [4, 256] EmbeddingBag# class EmbeddingBag : public torch::nn::ModuleHolder\u003cEmbeddingBagImpl\u003e# A ModuleHolder subclass for EmbeddingBagImpl. See the documentation for EmbeddingBagImpl class to learn what methods it provides, and examples of how to use EmbeddingBag with torch::nn::EmbeddingBagOptions. See the documentation for ModuleHolder to learn about PyTorch\u2019s module storage semantics. Public Static Functions static inline EmbeddingBag from_pretrained(const torch::Tensor \u0026embeddings, const EmbeddingBagFromPretrainedOptions \u0026options = {})# See the documentation for torch::nn::EmbeddingBagFromPretrainedOptions class to learn what optional arguments are supported for this function. class EmbeddingBagImpl : public torch::nn::Cloneable\u003cEmbeddingBagImpl\u003e# Computes sums or means of \u2018bags\u2019 of embeddings, without instantiating the intermediate embeddings. See https://pytorch.org/docs/main/nn.html#torch.nn.EmbeddingBag to learn about the exact behavior of this module. See the documentation for torch::nn::EmbeddingBagOptions class to learn what constructor arguments are supported for this module. Example: EmbeddingBag model(EmbeddingBagOptions(10, 2).max_norm(2).norm_type(2.5).scale_grad_by_freq(true).sparse(true).mode(torch::kSum).padding_idx(1)); Public Functions inline EmbeddingBagImpl(int64_t num_embeddings, int64_t embedding_dim)# explicit EmbeddingBagImpl(EmbeddingBagOptions options_)# virtual void reset() override# reset() must perform initialization of all members with reference semantics, most importantly parameters, buffers and submodules. void reset_parameters()# virtual void pretty_print(std::ostream \u0026stream) const override# Pretty prints the EmbeddingBag module into the given stream. Tensor forward(const Tensor \u0026input, const Tensor \u0026offsets = {}, const Tensor \u0026per_sample_weights = {})# Public Members EmbeddingBagOptions options# The Options used to configure this EmbeddingBag module. Tensor weight# The embedding table. Friends friend struct torch::nn::AnyModuleHolder",
"author": {
"@type": "Organization",
"name": "PyTorch Contributors",
"url": "https://pytorch.org"
},
"image": "https://pytorch.org/docs/stable/_static/img/pytorch_seo.png",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "/api/nn/embedding.html"
},
"datePublished": "2023-01-01T00:00:00Z",
"dateModified": "2023-01-01T00:00:00Z"
}
| docsearch:language | en |
| llm:site-type | documentation |
| llm:framework | PyTorch |
| llm:description | Embedding layers in PyTorch C++ — Embedding and EmbeddingBag for sparse and dense lookups. |
| llm:navigation-file | https://pytorch.org/docs/stable/llms.txt |
| llm:sitemap | https://pytorch.org/docs/stable/sitemap.xml |
| llm:version | main |
| llm:project | PyTorch |
| llm:page-type | documentation |
| og:image | https://docs.pytorch.org/docs/stable/_static/img/pytorch_seo.png |
| None | 3 |
Links:
Viewport: width=device-width, initial-scale=1