詞嵌入的作用,意義什麼的我就不多說了!
你們可以去看看這篇文章:
https://www.cnblogs.com/lindaxin/p/7977406.html
我這裏主要是記錄了一下pytorch中關於Embedding的一些自我理解。
首先是導入包
import torch
import torch.nn as nn
from torch.autograd import Variable
然後定義你的數據
word_to_ix={"hello":0,"world":1}#用0代表hello,用1代表world
然後我們可以在這下面先加一行代碼
print(torch.LongTensor([0]))
其輸出爲:
tensor([0])#意思就是我們可以通過torch.LongTensor([0])直接構建一個Tensor
然後開始創建初始詞向量
embeds=nn.Embedding(2,5)#第一步定義詞向量大小,參數一爲單詞個數,二爲單詞長度
print(embeds)
其輸出爲:
Embedding(2, 5)
然後構建完這個詞向量
hello_idx=torch.LongTensor([word_to_ix["hello"]])#獲取到能夠表示hello的數字,並轉爲longtensor好計算
print(hello_idx)
hello_idx=Variable(hello_idx)#獲得一個Variable
print(hello_idx)
hello_embeds=embeds(hello_idx)#獲取到這個與“hello”對應的詞向量,
print(hello_embeds)
輸出爲:
tensor([0])
tensor([0])
tensor([[-0.2350, -0.3750, -0.2022, -1.7609, -1.1226]],
grad_fn=<EmbeddingBackward>)
由此我們可以得出,前面的word_to_ix[“hello”]就真的只是0而已,只是我們人爲的讓0來代表hello,多了一條對應關係。
然後我們再輸出一下下面的東西:
print(embeds(torch.LongTensor([0])))
print(embeds(Variable(torch.LongTensor([0]))))
print(embeds(Variable(torch.LongTensor([word_to_ix["hello"]]))))
輸出爲:
tensor([[-0.2350, -0.3750, -0.2022, -1.7609, -1.1226]],
grad_fn=<EmbeddingBackward>)#
tensor([[-0.2350, -0.3750, -0.2022, -1.7609, -1.1226]],
grad_fn=<EmbeddingBackward>)
tensor([[-0.2350, -0.3750, -0.2022, -1.7609, -1.1226]],
grad_fn=<EmbeddingBackward>)
我們可以發現這三個東西的輸出是一樣的,所以word_to_ix[“hello”]的作用就是給我們一個對應“hello”的數值,實際的詞向量並不是由這個“hello”來生成的。因爲如果你多輸出幾次,你就會發現詞向量每次的輸出都是不一樣的。
而Variable的作用就只是給tensor增加一點新的功能,輸出表現上並沒有什麼大的區別,所以我們可以把它就看成一個tensor。
最後實際上創建一個詞向量的代碼爲:
word_to_ix={"hello":0,"world":1}
embeds=nn.Embedding(2,5)#第一步定義詞向量大小,一爲單詞個數,二爲單詞長度
hello_idx=torch.LongTensor([word_to_ix["hello"]])#獲取到能夠表示hello的數字,並轉爲longtensor好計算
hello_idx=Variable(hello_idx)#獲得一個Variable
hello_embeds=embeds(hello_idx)#獲取到這個與“hello”對應的詞向量,
如果只是想使用的話,只要會這些就夠了。