这个函数的作用是,对你的词表产生一个嵌入表,这个嵌入只保证唯一性,不保证相关性(所以和word2vec还是有区别的)。
self.embedding = nn.Embedding(num_embeddings=vocab_size, embedding_dim=embed_dim)
是他的定义式。其中num_embeddings
定义了词表的长度,embedding_dim
定义了嵌入的长度。
在使用时,直接调用:
embedding = self.embedding(input)
这里的 input 应为一个下标列表,输出即为对应的嵌入。
The input to the module is a list of indices, and the output is the corresponding
word embeddings.