文章目錄
- 1、nn.Embedding
- 2、使用場景
1、nn.Embedding
- 首先我們講解一下關于嵌入向量embedding vector的概念
1)在自然語言處理NLP領域,是將單詞、短語或其他文本單位映射到一個固定長度的實數向量空間中。嵌入向量具有較低的維度,通常在幾十到幾百維之間,且每個維度都包含一定程度上的語義信息。這意味著在嵌入向量空間中,語義上相似的單詞在向量空間中也更加接近。
2)在計算機視覺領域,是將圖像或圖像中的區域映射到一個固定長度的實數向量空間中。嵌入向量在計算機視覺任務中起到了表示和提取特征的作用。通過將圖像映射到嵌入向量空間,可以捕捉到圖像的語義信息、視覺特征以及圖像之間的相似性。
- 總之,嵌入向量是具有固定維度的,而不論是在NLP領域還是CV領域,都需要生成多個嵌入向量,因此也有固定數量。
- 于是,我們就可以簡單理解該類為:
CLASS torch.nn.Embedding(num_embeddings, embedding_dim, padding_idx=None, max_norm=None,
norm_type=2.0, scale_grad_by_freq=False, sparse=False, _weight=None, _freeze=False, device=None, dtype=None)
''
一個簡單的查找表,用于存儲固定詞典和尺寸的embeddings:其實就是存儲了固定數量的具有固定維度的嵌入向量
該模塊需要使用索引檢索嵌入向量:也就是說模塊的輸入是索引列表,輸出是相應存儲的嵌入向量。
1) num_embeddings: 嵌入向量的數量
2) embedding_dim: 嵌入向量的維度
注意:
1)它的成員變量weight:具有shape為 (num_embeddings, embedding_dim) 的可學習的參數
2)輸入為:任意形狀[*]的IntTensor或LongTensor,內部元素為索引值,即0到num_embeddings-1之間的值輸出為:[*, H]的嵌入向量,H為embedding_dim
''
- 例如:
from torch import nn
import torch# an Embedding module containing 10 tensors of size 3
embedding = nn.Embedding(10, 3)
# a batch of 2 samples of 4 indices each
input = torch.LongTensor([[1, 2, 4, 5], [4, 3, 2, 9]])
print(embedding(input))
print(embedding.weight)
''
輸出為:
tensor([[[ 0.4125, 0.1478, 0.3764],[ 0.5272, -0.4960, 1.5926],[ 0.2231, -0.7653, -0.5333],[ 2.8278, 1.5299, 1.4080]],[[ 0.2231, -0.7653, -0.5333],[-0.3996, 0.3626, -0.3369],[ 0.5272, -0.4960, 1.5926],[ 0.6222, 1.3385, 0.6861]]], grad_fn=<EmbeddingBackward>)
Parameter containing:
tensor([[-0.1316, -0.2370, -0.8308],[ 0.4125, 0.1478, 0.3764],[ 0.5272, -0.4960, 1.5926],[-0.3996, 0.3626, -0.3369],[ 0.2231, -0.7653, -0.5333],[ 2.8278, 1.5299, 1.4080],[-0.4182, 0.4665, 1.5345],[-1.2107, 0.3569, 0.9719],[-0.6439, -0.4095, 0.6130],[ 0.6222, 1.3385, 0.6861]], requires_grad=True)
''
2、使用場景
- transformer decoder輸入的嵌入向量Output Embedding
- DETR中的decoder的object queries