gpt4 book ai didi

nlp - 在 Pytorch 中嵌入 3D 数据

转载 作者:行者123 更新时间:2023-12-04 06:07:02 24 4
gpt4 key购买 nike

我想实现字符级嵌入。

这是通常的词嵌入。

词嵌入

Input: [ [‘who’, ‘is’, ‘this’] ] 
-> [ [3, 8, 2] ] # (batch_size, sentence_len)
-> // Embedding(Input)
# (batch_size, seq_len, embedding_dim)

这就是我想要做的。

字符嵌入
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.

虽然我尝试过,但我不确定如何实现 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)))

由于 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)

更新

我可以做到这一点。但是 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)

更新2

这是我的最终代码。谢谢你,瓦西艾哈迈德!
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

最佳答案

我假设你有一个形状为 BxSxW 的 3d 张量在哪里:

B = Batch size
S = Sentence length
W = Word length

并且您已按如下方式声明嵌入层。
self.embedding = nn.Embedding(dict_size, emsize)

在哪里:
dict_size = No. of unique characters in the training corpus
emsize = Expected size of embeddings

所以,现在你需要转换形状为 BxSxW 的 3d 张量到形状为 BSxW 的二维张量并将其交给嵌入层。
emb = self.embedding(input_rep.view(-1, input_rep.size(2)))
emb的形状将是 BSxWxE哪里 E是嵌入大小。您可以将生成的 3d 张量转换为 4d 张量,如下所示。
emb = emb.view(*input_rep.size(), -1)
emb的最终形状将是 BxSxWxE这就是你所期待的。

关于nlp - 在 Pytorch 中嵌入 3D 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47205762/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com