GOOGLE ADS

martes, 3 de mayo de 2022

Incrustación de datos 3D en Pytorch

Quiero implementar incrustaciones a nivel de carácter.

Esta es la incrustación de palabras habitual.

Incrustación de palabras

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

Esto es lo que quiero hacer.

Incrustación de personajes

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.

Aunque lo probé, no estoy seguro de cómo implementar la incrustación 3D. ¿Sabes cómo implementar esos datos?

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

El código anterior obtuvo un error porque la salida de self.embeddinges 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)

Actualizar

Yo podría hacer esto. Pero forno es efectivo para lotes. ¿Conocen una forma más eficiente?

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)

Actualizar2

Este es mi código final. ¡Gracias, Wasi Ahmad!

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


Solución del problema

Supongo que tienes un tensor de forma 3d BxSxWdonde:

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

Y ha declarado la capa de incrustación de la siguiente manera.

self.embedding = nn.Embedding(dict_size, emsize)

Donde:

dict_size = No. of unique characters in the training corpus
emsize = Expected size of embeddings

Entonces, ahora necesita convertir el tensor de forma 3d en un tensor de forma BxSxW2d BSxWy dárselo a la capa de incrustación.

emb = self.embedding(input_rep.view(-1, input_rep.size(2)))

La forma de embserá BSxWxEdonde Eestá el tamaño de incrustación. Puede convertir el tensor 3d resultante en un tensor 4d de la siguiente manera.

emb = emb.view(*input_rep.size(), -1)

La forma final de embserá BxSxWxEla que esperas.

No hay comentarios.:

Publicar un comentario

Flutter: error de rango al acceder a la respuesta JSON

Estoy accediendo a una respuesta JSON con la siguiente estructura. { "fullName": "FirstName LastName", "listings...