我正在从火炬中心加载语言模型(CamemBERT一种基于法国RoBERTa的法语模型,并使用它嵌入一些句子:
import torch
camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0')
camembert.eval() # disable dropout (or leave in train mode to finetune)
def embed(sentence):
tokens = camembert.encode(sentence)
# Extract all layer's features (layer 0 is the embedding layer)
all_layers = camembert.extract_features(tokens, return_all_hiddens=True)
embeddings = all_layers[0]
return embeddings
# Here we see that the shape of the embedding vector depends on the number of tokens in the sentence
u = embed("Bonjour, ça va ?")
u.shape # torch.Size([1, 7, 768])
v = embed("Salut, comment vas-tu ?")
v.shape # torch.Size([1, 9, 768])
现在想象一下,为了进行一些语义搜索,我想计算向量(在我们的例子中为张量)cosine distance
和u
之间的v
:
cos = torch.nn.CosineSimilarity(dim=1)
cos(u, v) #will throw an error since the shape of `u` is different from the shape of `v`
我在问什么是最好的方法,以使句子始终保持相同的嵌入形状 无论其标记的数量如何?]]
=>我要考虑的第一个解决方案是计算mean on axis=1
(句子的嵌入是嵌入其标记的平均值),因为axis = 0和axis = 2的大小始终相同:
cos = torch.nn.CosineSimilarity(dim=1) cos(u.mean(axis=1), v.mean(axis=1)) # works now and gives 0.7269
但是,在计算均值时,恐怕会伤害句子的嵌入,因为它为每个标记赋予相同的权重(可能乘以TF-IDF?)。
=>第二种解决方案是填充较短的句子,这意味着:
S
S
(该句子在其余维度中为0)您有什么想法?您还将使用其他哪些技术?为什么?
我正在从火炬中心加载语言模型(CamemBERT是基于法国RoBERTa的法语模型)并使用它嵌入一些句子:import torch camembert = torch.hub.load('pytorch / fairseq','camembert.v0' )...