快捷方式

EmbeddingBag

class torch.nn.EmbeddingBag(num_embeddings, embedding_dim, max_norm=None, norm_type=2.0, scale_grad_by_freq=False, mode='mean', sparse=False, _weight=None, include_last_offset=False, padding_idx=None, device=None, dtype=None)[source][source]

計算嵌入‘袋’(bags)的總和或平均值,而無需例項化中間嵌入。

對於長度固定的‘袋’,沒有 per_sample_weights,沒有等於 padding_idx 的索引,並且輸入是 2D 的情況,此類

  • mode="sum" 時,等價於 Embedding 後接 torch.sum(dim=1)

  • mode="mean" 時,等價於 Embedding 後接 torch.mean(dim=1)

  • mode="max" 時,等價於 Embedding 後接 torch.max(dim=1)

然而,EmbeddingBag 比使用這些操作鏈條更節省時間和記憶體。

EmbeddingBag 還支援將 per-sample weights 作為 forward 傳遞的引數。這會在執行由 mode 指定的加權歸約(reduction)之前縮放 Embedding 的輸出。如果傳遞了 per_sample_weights,唯一支援的 mode"sum",它根據 per_sample_weights 計算加權總和。

引數
  • num_embeddings (int) – 詞嵌入字典的大小

  • embedding_dim (int) – 每個嵌入向量的大小

  • max_norm (float, 可選) – 如果給定,範數大於 max_norm 的每個嵌入向量將被重新規範化,使其範數為 max_norm

  • norm_type (float, 可選) – 用於 max_norm 選項計算 p-範數時的 p 值。預設值 2

  • scale_grad_by_freq (bool, 可選) – 如果給定,這將按 mini-batch 中詞語頻率的倒數來縮放梯度。預設值 False。注意:當 mode="max" 時不支援此選項。

  • mode (str, 可選) – "sum", "mean""max"。指定聚合(reduce)‘袋’的方式。"sum" 計算加權總和,考慮了 per_sample_weights"mean" 計算‘袋’中值的平均值,"max" 計算每個‘袋’中的最大值。預設值:"mean"

  • sparse (bool, 可選) – 如果為 True,關於 weight 矩陣的梯度將是稀疏張量。有關稀疏梯度的更多詳細資訊,請參見注意事項(Notes)。注意:當 mode="max" 時不支援此選項。

  • include_last_offset (bool, 可選) – 如果為 Trueoffsets 將包含一個額外的元素,其中最後一個元素等於 indices 的大小。這與 CSR 格式相匹配。

  • padding_idx (int, 可選) – 如果指定,位於 padding_idx 的條目不參與梯度計算;因此,位於 padding_idx 的嵌入向量在訓練期間不會更新,即它保持為固定的“填充”。對於新構造的 EmbeddingBag,位於 padding_idx 的嵌入向量預設為全零,但可以更新為其他值以用作填充向量。請注意,位於 padding_idx 的嵌入向量被排除在歸約(reduction)之外。

變數

weight (Tensor) – 模組的可學習權重,形狀為 (num_embeddings, embedding_dim),從 N(0,1)\mathcal{N}(0, 1) 初始化。

示例

>>> # an EmbeddingBag module containing 10 tensors of size 3
>>> embedding_sum = nn.EmbeddingBag(10, 3, mode='sum')
>>> # a batch of 2 samples of 4 indices each
>>> input = torch.tensor([1, 2, 4, 5, 4, 3, 2, 9], dtype=torch.long)
>>> offsets = torch.tensor([0, 4], dtype=torch.long)
>>> embedding_sum(input, offsets)
tensor([[-0.8861, -5.4350, -0.0523],
        [ 1.1306, -2.5798, -1.0044]])

>>> # Example with padding_idx
>>> embedding_sum = nn.EmbeddingBag(10, 3, mode='sum', padding_idx=2)
>>> input = torch.tensor([2, 2, 2, 2, 4, 3, 2, 9], dtype=torch.long)
>>> offsets = torch.tensor([0, 4], dtype=torch.long)
>>> embedding_sum(input, offsets)
tensor([[ 0.0000,  0.0000,  0.0000],
        [-0.7082,  3.2145, -2.6251]])

>>> # An EmbeddingBag can be loaded from an Embedding like so
>>> embedding = nn.Embedding(10, 3, padding_idx=2)
>>> embedding_sum = nn.EmbeddingBag.from_pretrained(
        embedding.weight,
        padding_idx=embedding.padding_idx,
        mode='sum')
forward(input, offsets=None, per_sample_weights=None)[source][source]

EmbeddingBag 的前向傳播(forward pass)。

引數
  • input (Tensor) – 包含嵌入矩陣索引‘袋’的 Tensor。

  • offsets (Tensor, 可選) – 僅當 input 為 1D 時使用。offsets 決定了 input 中每個‘袋’(序列)的起始索引位置。

  • per_sample_weights (Tensor, 可選) – 一個 float / double 權重的 Tensor,或 None 表示所有權重都應視為 1。如果指定,per_sample_weights 必須與 input 的形狀完全相同,並且如果 offsetsNone,則視為具有相同的 offsets。僅支援 mode='sum'

返回值

Tensor 輸出形狀為 (B, embedding_dim)

返回型別

Tensor

注意

關於 inputoffsets 的注意事項

  • inputoffsets 必須是相同型別,要麼是 int,要麼是 long

  • 如果 input 是形狀為 (B, N) 的 2D Tensor,它將被視為 B 個固定長度為 N 的‘袋’(序列),並且將返回 B 個值,其聚合方式取決於 mode。在這種情況下,offsets 被忽略且必須為 None

  • 如果 input 是形狀為 (N) 的 1D Tensor,它將被視為多個‘袋’(序列)的拼接。offsets 必須是包含 input 中每個‘袋’起始索引位置的 1D Tensor。因此,對於形狀為 (B)offsetsinput 將被視為包含 B 個‘袋’。空‘袋’(即長度為 0 的‘袋’)將返回由零填充的向量。

classmethod from_pretrained(embeddings, freeze=True, max_norm=None, norm_type=2.0, scale_grad_by_freq=False, mode='mean', sparse=False, include_last_offset=False, padding_idx=None)[source][source]

從給定的二維 FloatTensor 建立 EmbeddingBag 例項。

引數
  • embeddings (Tensor) – 包含 EmbeddingBag 權重的 FloatTensor。第一個維度被傳遞給 EmbeddingBag 作為 ‘num_embeddings’,第二個維度作為 ‘embedding_dim’。

  • freeze (bool, 可選) – 如果為 True,則該 Tensor 在學習過程中不會更新。等價於設定 embeddingbag.weight.requires_grad = False。預設值:True

  • max_norm (float, 可選) – 參見模組初始化文件。預設值:None

  • norm_type (float, 可選) – 參見模組初始化文件。預設值 2

  • scale_grad_by_freq (bool, 可選) – 參見模組初始化文件。預設值 False

  • mode (str, 可選) – 參見模組初始化文件。預設值:"mean"

  • sparse (bool, 可選) – 參見模組初始化文件。預設值:False

  • include_last_offset (bool, 可選) – 參見模組初始化文件。預設值:False

  • padding_idx (int, 可選) – 參見模組初始化文件。預設值:None

返回型別

EmbeddingBag

示例

>>> # FloatTensor containing pretrained weights
>>> weight = torch.FloatTensor([[1, 2.3, 3], [4, 5.1, 6.3]])
>>> embeddingbag = nn.EmbeddingBag.from_pretrained(weight)
>>> # Get embeddings for index 1
>>> input = torch.LongTensor([[1, 0]])
>>> embeddingbag(input)
tensor([[ 2.5000,  3.7000,  4.6500]])

文件

獲取 PyTorch 的全面開發者文件

檢視文件

教程

獲取針對初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源