我想实现字符级嵌入.
这是通常的单词嵌入.
单词嵌入
Input: [ [‘who’, ‘is’, ‘this’] ]
-> [ [3, 8, 2] ] # (batch_size, sentence_len)
-> // Embedding(Input)
# (batch_size, seq_len, embedding_dim)
Run Code Online (Sandbox Code Playgroud)
这就是我想要做的.
字符嵌入
Input: [ [ [‘w’, ‘h’, ‘o’, 0], [‘i’, ‘s’, 0, 0], [‘t’, ‘h’, ‘i’, ‘s’] ] ]
-> [ [ [2, 3, 9, 0], [ 11, 4, 0, 0], [21, 10, 8, 9] ] ] # (batch_size, sentence_len, word_len)
-> // Embedding(Input) # (batch_size, sentence_len, word_len, embedding_dim)
-> // sum each character embeddings # (batch_size, sentence_len, embedding_dim)
The final output shape is same as Word embedding. Because I want to concat them later.
Run Code Online (Sandbox Code Playgroud)
虽然我尝试过,但我不确定如何实现3-D嵌入.你知道如何实现这样的数据吗?
def forward(self, x):
print('x', x.size()) # (N, seq_len, word_len)
bs = x.size(0)
seq_len = x.size(1)
word_len = x.size(2)
embd_list = []
for i, elm in enumerate(x):
tmp = torch.zeros(1, word_len, self.embd_size)
for chars in elm:
tmp = torch.add(tmp, 1.0, self.embedding(chars.unsqueeze(0)))
Run Code Online (Sandbox Code Playgroud)
上面的代码出错了,因为输出self.embedding是Variable.
TypeError: torch.add received an invalid combination of arguments - got (torch.FloatTensor, float, Variable), but expected one of:
* (torch.FloatTensor source, float value)
* (torch.FloatTensor source, torch.FloatTensor other)
* (torch.FloatTensor source, torch.SparseFloatTensor other)
* (torch.FloatTensor source, float value, torch.FloatTensor other)
didn't match because some of the arguments have invalid types: (torch.FloatTensor, float, Variable)
* (torch.FloatTensor source, float value, torch.SparseFloatTensor other)
didn't match because some of the arguments have invalid types: (torch.FloatTensor, float, Variable)
Run Code Online (Sandbox Code Playgroud)
我能做到这一点.但for对批次无效.你们知道更有效率的方式吗?
def forward(self, x):
print('x', x.size()) # (N, seq_len, word_len)
bs = x.size(0)
seq_len = x.size(1)
word_len = x.size(2)
embd = Variable(torch.zeros(bs, seq_len, self.embd_size))
for i, elm in enumerate(x): # every sample
for j, chars in enumerate(elm): # every sentence. [ [‘w’, ‘h’, ‘o’, 0], [‘i’, ‘s’, 0, 0], [‘t’, ‘h’, ‘i’, ‘s’] ]
chars_embd = self.embedding(chars.unsqueeze(0)) # (N, word_len, embd_size) [‘w’,‘h’,‘o’,0]
chars_embd = torch.sum(chars_embd, 1) # (N, embd_size). sum each char's embedding
embd[i,j] = chars_embd[0] # set char_embd as word-like embedding
x = embd # (N, seq_len, embd_dim)
Run Code Online (Sandbox Code Playgroud)
这是我的最终代码.谢谢,瓦斯艾哈迈德!
def forward(self, x):
# x: (N, seq_len, word_len)
input_shape = x.size()
bs = x.size(0)
seq_len = x.size(1)
word_len = x.size(2)
x = x.view(-1, word_len) # (N*seq_len, word_len)
x = self.embedding(x) # (N*seq_len, word_len, embd_size)
x = x.view(*input_shape, -1) # (N, seq_len, word_len, embd_size)
x = x.sum(2) # (N, seq_len, embd_size)
return x
Run Code Online (Sandbox Code Playgroud)
我假设你有一个3d张量的形状BxSxW,其中:
B = Batch size
S = Sentence length
W = Word length
Run Code Online (Sandbox Code Playgroud)
并且您已声明嵌入层如下.
self.embedding = nn.Embedding(dict_size, emsize)
Run Code Online (Sandbox Code Playgroud)
哪里:
dict_size = No. of unique characters in the training corpus
emsize = Expected size of embeddings
Run Code Online (Sandbox Code Playgroud)
所以,现在你需要将3d张量的形状BxSxW转换为2d张量的形状BSxW并将其赋予嵌入层.
emb = self.embedding(input_rep.view(-1, input_rep.size(2)))
Run Code Online (Sandbox Code Playgroud)
的形状emb将是BSxWxE其中E是嵌入的大小.您可以将生成的3d张量转换为4d张量,如下所示.
emb = emb.view(*input_rep.size(), -1)
Run Code Online (Sandbox Code Playgroud)
最终的形状emb将BxSxWxE是你期待的.
| 归档时间: |
|
| 查看次数: |
1168 次 |
| 最近记录: |