Git Product home page Git Product logo

Comments (8)

aRookieMan avatar aRookieMan commented on May 20, 2024 1

最新的transformer只需要输入目录,并且把名改成pytorch_model.bin
huggingface/transformers#1620 (comment)

from gpt2-chitchat.

michellemashutian avatar michellemashutian commented on May 20, 2024

最新的transformer只需要输入目录,并且把名改成pytorch_model.bin
huggingface/transformers#1620 (comment)

我想问一下,怎么把里面的词向量和词取出来。。。

from gpt2-chitchat.

aRookieMan avatar aRookieMan commented on May 20, 2024

最新的transformer只需要输入目录,并且把名改成pytorch_model.bin
huggingface/transformers#1620 (comment)

我想问一下,怎么把里面的词向量和词取出来。。。

你用我图里面的代码读进模型,然后就可以使用huggingface/transformers的例子操作了

from gpt2-chitchat.

michellemashutian avatar michellemashutian commented on May 20, 2024

最新的transformer只需要输入目录,并且把名改成pytorch_model.bin
huggingface/transformers#1620 (comment)

我想问一下,怎么把里面的词向量和词取出来。。。

你用我图里面的代码读进模型,然后就可以使用huggingface/transformers的例子操作了

我读进去了,但是不知道怎么取。。我就想把那个词向量搞出来,变成word[空格]embedding的格式,不好意思啊,可能问题有点弱智。。。

from gpt2-chitchat.

aRookieMan avatar aRookieMan commented on May 20, 2024

最新的transformer只需要输入目录,并且把名改成pytorch_model.bin
huggingface/transformers#1620 (comment)

我想问一下,怎么把里面的词向量和词取出来。。。

你用我图里面的代码读进模型,然后就可以使用huggingface/transformers的例子操作了

我读进去了,但是不知道怎么取。。我就想把那个词向量搞出来,变成word[空格]embedding的格式,不好意思啊,可能问题有点弱智。。。

根据huggingface/transformers的例子就好了啊:

tokenizer = tokenizer_class.from_pretrained(pretrained_weights)
model = model_class.from_pretrained(pretrained_weights)

# Encode text
input_ids = torch.tensor([tokenizer.encode("Here is some text to encode", add_special_tokens=True)])  # Add special tokens takes care of adding [CLS], [SEP], <s>... tokens in the right way for each model.
with torch.no_grad():
    last_hidden_states = model(input_ids)[0]  # Models outputs are now tuples

这个repo是基于huggingface的。推荐你阅读huggingface/transformers这个库,首页就有Quick tour。
初学者,理解。

from gpt2-chitchat.

michellemashutian avatar michellemashutian commented on May 20, 2024

最新的transformer只需要输入目录,并且把名改成pytorch_model.bin
huggingface/transformers#1620 (comment)

我想问一下,怎么把里面的词向量和词取出来。。。

你用我图里面的代码读进模型,然后就可以使用huggingface/transformers的例子操作了

我读进去了,但是不知道怎么取。。我就想把那个词向量搞出来,变成word[空格]embedding的格式,不好意思啊,可能问题有点弱智。。。

根据huggingface/transformers的例子就好了啊:

tokenizer = tokenizer_class.from_pretrained(pretrained_weights)
model = model_class.from_pretrained(pretrained_weights)

# Encode text
input_ids = torch.tensor([tokenizer.encode("Here is some text to encode", add_special_tokens=True)])  # Add special tokens takes care of adding [CLS], [SEP], <s>... tokens in the right way for each model.
with torch.no_grad():
    last_hidden_states = model(input_ids)[0]  # Models outputs are now tuples

这个repo是基于huggingface的。推荐你阅读huggingface/transformers这个库,首页就有Quick tour。
初学者,理解。

嗯,我知道这个,所以我如果要搞词向量出来,我就一个一个encode词么,我以为有那种直接取的操作。谢谢!!

from gpt2-chitchat.

aRookieMan avatar aRookieMan commented on May 20, 2024

最新的transformer只需要输入目录,并且把名改成pytorch_model.bin
huggingface/transformers#1620 (comment)

我想问一下,怎么把里面的词向量和词取出来。。。

你用我图里面的代码读进模型,然后就可以使用huggingface/transformers的例子操作了

我读进去了,但是不知道怎么取。。我就想把那个词向量搞出来,变成word[空格]embedding的格式,不好意思啊,可能问题有点弱智。。。

根据huggingface/transformers的例子就好了啊:

tokenizer = tokenizer_class.from_pretrained(pretrained_weights)
model = model_class.from_pretrained(pretrained_weights)

# Encode text
input_ids = torch.tensor([tokenizer.encode("Here is some text to encode", add_special_tokens=True)])  # Add special tokens takes care of adding [CLS], [SEP], <s>... tokens in the right way for each model.
with torch.no_grad():
    last_hidden_states = model(input_ids)[0]  # Models outputs are now tuples

这个repo是基于huggingface的。推荐你阅读huggingface/transformers这个库,首页就有Quick tour。
初学者,理解。

嗯,我知道这个,所以我如果要搞词向量出来,我就一个一个encode词么,我以为有那种直接取的操作。谢谢!!

额 ... 我明白你的意思是。BERT是动态语言模型,token对应的emb只是浅层的,过了这12层MHA的词向量才是准确的词向量。如果你想提取出最初的token对应的emb,这个理论上是可以直接提取的,你分析一下它的nn层有哪些,把第一层截取出来就行了。我没试过哦,加油!

from gpt2-chitchat.

michellemashutian avatar michellemashutian commented on May 20, 2024

最新的transformer只需要输入目录,并且把名改成pytorch_model.bin
huggingface/transformers#1620 (comment)

我想问一下,怎么把里面的词向量和词取出来。。。

你用我图里面的代码读进模型,然后就可以使用huggingface/transformers的例子操作了

我读进去了,但是不知道怎么取。。我就想把那个词向量搞出来,变成word[空格]embedding的格式,不好意思啊,可能问题有点弱智。。。

根据huggingface/transformers的例子就好了啊:

tokenizer = tokenizer_class.from_pretrained(pretrained_weights)
model = model_class.from_pretrained(pretrained_weights)

# Encode text
input_ids = torch.tensor([tokenizer.encode("Here is some text to encode", add_special_tokens=True)])  # Add special tokens takes care of adding [CLS], [SEP], <s>... tokens in the right way for each model.
with torch.no_grad():
    last_hidden_states = model(input_ids)[0]  # Models outputs are now tuples

这个repo是基于huggingface的。推荐你阅读huggingface/transformers这个库,首页就有Quick tour。
初学者,理解。

嗯,我知道这个,所以我如果要搞词向量出来,我就一个一个encode词么,我以为有那种直接取的操作。谢谢!!

额 ... 我明白你的意思是。BERT是动态语言模型,token对应的emb只是浅层的,过了这12层MHA的词向量才是准确的词向量。如果你想提取出最初的token对应的emb,这个理论上是可以直接提取的,你分析一下它的nn层有哪些,把第一层截取出来就行了。我没试过哦,加油!

好像这样就可以了。里面的29522是词的indx,如果那个vocab文件是按照indx排列的话,就可以生成我那个文件了。

model = BertModel.from_pretrained(model_path)
embeds = model.get_input_embeddings()
print(embeds)
input_idx = Variable(torch.LongTensor([29522]))
print(embeds(input_idx))

from gpt2-chitchat.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.