Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

加载GPT2LMHeadModel编码错误啊 #43

Closed
aRookieMan opened this issue Jun 9, 2020 · 8 comments
Closed

加载GPT2LMHeadModel编码错误啊 #43

aRookieMan opened this issue Jun 9, 2020 · 8 comments

Comments

@aRookieMan
Copy link

image

想单独加载出来,发现报错了,咋回事 ...

@aRookieMan
Copy link
Author

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

@michellemashutian
Copy link

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

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

@aRookieMan
Copy link
Author

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

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

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

@michellemashutian
Copy link

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

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

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

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

@aRookieMan
Copy link
Author

最新的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。
初学者,理解。

@michellemashutian
Copy link

最新的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词么,我以为有那种直接取的操作。谢谢!!

@aRookieMan
Copy link
Author

最新的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层有哪些,把第一层截取出来就行了。我没试过哦,加油!

@michellemashutian
Copy link

最新的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))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants