【问题标题】:How to increase dimension-vector size of BERT sentence-transformers embedding如何增加 BERT 句子转换器嵌入的维向量大小
【发布时间】:2021-10-11 14:59:55
【问题描述】:

我正在使用句子转换器进行语义搜索,但有时它不理解上下文含义并返回错误结果 例如。 BERT problem with context/semantic search in italian language

默认情况下,句子嵌入的向量边是78列,那么如何增加这个维度,以便它能够深入理解上下文的含义。

代码:

# Load the BERT Model
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('bert-base-nli-mean-tokens')

# Setup a Corpus
# A corpus is a list with documents split by sentences.

sentences = ['Absence of sanity', 
             'Lack of saneness',
             'A man is eating food.',
             'A man is eating a piece of bread.',
             'The girl is carrying a baby.',
             'A man is riding a horse.',
             'A woman is playing violin.',
             'Two men pushed carts through the woods.',
             'A man is riding a white horse on an enclosed ground.',
             'A monkey is playing drums.',
             'A cheetah is running behind its prey.']

# Each sentence is encoded as a 1-D vector with 78 columns 
sentence_embeddings = model.encode(sentences) ### how to increase vector dimention 

print('Sample BERT embedding vector - length', len(sentence_embeddings[0]))

print('Sample BERT embedding vector - note includes negative values', sentence_embeddings[0])

【问题讨论】:

  • 默认情况下,BERT(所谓的 BERT-base)词嵌入有 768 个维度,而不是 78 个。句子嵌入是句子中词向量的加权和。

标签: machine-learning nlp artificial-intelligence bert-language-model


【解决方案1】:

不幸的是,以有意义的方式增加嵌入维度的唯一方法是重新训练模型。 :(

但是,也许这不是您需要的……也许您应该考虑微调模型:

我建议你看看来自 UKPLabs 的sentence-transformers。 他们为超过 100 种语言的句子嵌入预训练了模型。最好的部分是您可以fine tune 这些模型。

祝你好运!

【讨论】:

    【解决方案2】:

    增加训练模型的维度是不可能的(没有很多困难和重新训练模型)。您使用的模型是使用 768 维度预训练的,即模型的所有权重矩阵都有相应数量的训练参数。增加维度意味着添加需要学习的参数。

    此外,模型的维度并不能反映句子表示中语义或上下文信息的数量。模型维度的选择更多地体现了模型容量、训练数据量和合理推理速度之间的权衡。

    如果您使用的模型没有提供足够丰富的语义表示,您可能需要搜索更好的模型,例如 RoBERTa 或 T5。

    【讨论】:

      猜你喜欢
      • 2020-04-07
      • 2020-12-07
      • 2020-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多