【问题标题】:Sentence embedding using T5使用 T5 进行句子嵌入
【发布时间】:2021-02-11 04:43:44
【问题描述】:

我想使用最先进的 LM T5 来获取句子嵌入向量。 我找到了这个存储库https://github.com/UKPLab/sentence-transformers 据我所知,在 BERT 中,我应该将第一个标记作为 [CLS] 标记,它将是句子嵌入。 在这个存储库中,我在 T5 模型上看到了相同的行为:

cls_tokens = output_tokens[:, 0, :]  # CLS token is first token

这种行为正确吗?我从 T5 中获取了编码器并用它编码了两个短语:

"I live in the kindergarden"
"Yes, I live in the kindergarden"

它们之间的余弦相似度只有“0.2420”。

我只需要了解句子嵌入的工作原理 - 我应该训练网络找到相似性以达到正确的结果吗?或者我的基础预训练语言模型就足够了?

【问题讨论】:

    标签: nlp pytorch word-embedding


    【解决方案1】:

    为了从 T5 中获取句子嵌入,需要从 T5 编码器输出中取 last_hidden_state:

    model.encoder(input_ids=s, attention_mask=attn, return_dict=True)
    pooled_sentence = output.last_hidden_state # shape is [batch_size, seq_len, hidden_size]
    # pooled_sentence will represent the embeddings for each word in the sentence
    # you need to sum/average the pooled_sentence
    pooled_sentence = torch.mean(pooled_sentence, dim=1)
    

    你现在有一个来自 T5 的句子嵌入

    【讨论】:

      猜你喜欢
      • 2022-01-27
      • 2020-12-26
      • 2020-01-29
      • 1970-01-01
      • 2020-04-07
      • 2021-07-15
      • 2020-12-16
      • 2023-03-23
      • 1970-01-01
      相关资源
      最近更新 更多