带有Google的word嵌入式T5?

发布于 2025-02-03 03:10:44 字数 455 浏览 5 评论 0原文

是否可以使用Google的T5生成单词嵌入?

我假设这是可能的。但是,我找不到我需要能够在相关github上生成词嵌入的代码( https://github.com/google-research/text-text-text-text-text-transfer-transformer )或huggingface( https://huggingface.co/docs/transformers/model_doc/t5 )页面。

Is it possible to generate word embeddings with Google's T5?

I'm assuming that this is possible. However, I cannot find the code I would need to be able to generate word embeddings on the relevant Github (https://github.com/google-research/text-to-text-transfer-transformer) or HuggingFace (https://huggingface.co/docs/transformers/model_doc/t5) pages.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山人契 2025-02-10 03:10:44

是的,这是可能的。只需将单词的ID馈送到单词嵌入层:

from transformers import T5TokenizerFast, T5EncoderModel

tokenizer = T5TokenizerFast.from_pretrained("t5-small")
model = T5EncoderModel.from_pretrained("t5-small")
i = tokenizer(
    "This is a meaningless test sentence to show how you can get word embeddings", return_tensors="pt", return_attention_mask=False, add_special_tokens=False
)

o = model.encoder.embed_tokens(i.input_ids)

输出张量具有以下形状:

#print(o.shape)
torch.Size([1, 19, 512])

19个向量是每个令牌的表示。根据您的任务,您可以用 word_ids

i.word_ids()

输出:

[0, 1, 2, 2, 3, 3, 3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 12, 12]

Yes, that is possible. Just feed the ids of the words to the word embedding layer:

from transformers import T5TokenizerFast, T5EncoderModel

tokenizer = T5TokenizerFast.from_pretrained("t5-small")
model = T5EncoderModel.from_pretrained("t5-small")
i = tokenizer(
    "This is a meaningless test sentence to show how you can get word embeddings", return_tensors="pt", return_attention_mask=False, add_special_tokens=False
)

o = model.encoder.embed_tokens(i.input_ids)

The output tensor has the following shape:

#print(o.shape)
torch.Size([1, 19, 512])

The 19 vectors are the representations of each token. Depending on your task, you can map them back to the individual words with word_ids:

i.word_ids()

Output:

[0, 1, 2, 2, 3, 3, 3, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 12, 12]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文