使用 huggingface pytorch-transformers GPT-2 进行分类任务
using huggingface's pytorch- transformers GPT-2 for classifcation tasks
我想用GPT-2做一个文本分类器模型。我不太确定通过 GPT-2 提取特征后应该添加什么头。例如我有一个序列。
import pytorch_transformers as pt
import torch
text=test.iloc[1,1]
text
'If a fire wanted fanning, it could readily be fanned with a newspaper, and as the government grew weaker, I have no doubt that leather and iron acquired durability in proportion, for, in a very short time, there was not a pair of bellows in all Rotterdam that ever stood in need of a stitch or required the assistance of a hammer.'
len(text)
74
tokenizer = pt.GPT2Tokenizer.from_pretrained('gpt2')
model = pt.GPT2Model.from_pretrained('gpt2')
zz = tokenizer.tokenize(text)
z1=torch.tensor([tokenizer.convert_tokens_to_ids(zz)])
z1
tensor([[ 1532, 257, 2046, 2227, 4336, 768, 11, 340, 714, 14704,
307, 277, 3577, 351, 257, 7533, 11, 290, 355, 262,
1230, 6348, 17642, 11, 314, 423, 645, 4719, 326, 11620,
290, 6953, 9477, 26578, 287, 9823, 11, 329, 11, 287,
257, 845, 1790, 640, 11, 612, 373, 407, 257, 5166,
286, 8966, 1666, 287, 477, 18481, 353, 11043, 326, 1683,
6204, 287, 761, 286, 257, 24695, 393, 2672, 262, 6829,
286, 257, 15554, 13]])
output,hidden=model(z1)
ouput.shape
torch.Size([1, 74, 768])
GPT2的输出对我来说是n x m x 768,n是batch size,m是seqence中的token个数(比如我可以pad/truncate到128),所以我不能按照论文中所说的分类任务,只需在我在 google 上搜索的 tail.And 中添加一个完全连接的层,很少提到 GPT-2 分类任务。
我不确定什么是正确的。我应该在全连接层之前做 flatten/max pooling/average 池化还是其他?
“所以我不能像论文所说的那样做分类任务,只是在尾部添加一个全连接层。” - 这是您问题的答案。
通常,像 BERT 和 Roberta 这样的转换器具有双向自注意力,并且它们具有 [CLS] 标记,我们可以在其中输入分类器。由于 GPT-2 是左右方向的,因此您需要提供嵌入序列的最终标记。
P.S - 你能把link写到纸上吗?
如果您使用 GPT-2 构建模型进行文本分类,请分享。
我想用GPT-2做一个文本分类器模型。我不太确定通过 GPT-2 提取特征后应该添加什么头。例如我有一个序列。
import pytorch_transformers as pt
import torch
text=test.iloc[1,1]
text
'If a fire wanted fanning, it could readily be fanned with a newspaper, and as the government grew weaker, I have no doubt that leather and iron acquired durability in proportion, for, in a very short time, there was not a pair of bellows in all Rotterdam that ever stood in need of a stitch or required the assistance of a hammer.'
len(text)
74
tokenizer = pt.GPT2Tokenizer.from_pretrained('gpt2')
model = pt.GPT2Model.from_pretrained('gpt2')
zz = tokenizer.tokenize(text)
z1=torch.tensor([tokenizer.convert_tokens_to_ids(zz)])
z1
tensor([[ 1532, 257, 2046, 2227, 4336, 768, 11, 340, 714, 14704,
307, 277, 3577, 351, 257, 7533, 11, 290, 355, 262,
1230, 6348, 17642, 11, 314, 423, 645, 4719, 326, 11620,
290, 6953, 9477, 26578, 287, 9823, 11, 329, 11, 287,
257, 845, 1790, 640, 11, 612, 373, 407, 257, 5166,
286, 8966, 1666, 287, 477, 18481, 353, 11043, 326, 1683,
6204, 287, 761, 286, 257, 24695, 393, 2672, 262, 6829,
286, 257, 15554, 13]])
output,hidden=model(z1)
ouput.shape
torch.Size([1, 74, 768])
GPT2的输出对我来说是n x m x 768,n是batch size,m是seqence中的token个数(比如我可以pad/truncate到128),所以我不能按照论文中所说的分类任务,只需在我在 google 上搜索的 tail.And 中添加一个完全连接的层,很少提到 GPT-2 分类任务。 我不确定什么是正确的。我应该在全连接层之前做 flatten/max pooling/average 池化还是其他?
“所以我不能像论文所说的那样做分类任务,只是在尾部添加一个全连接层。” - 这是您问题的答案。
通常,像 BERT 和 Roberta 这样的转换器具有双向自注意力,并且它们具有 [CLS] 标记,我们可以在其中输入分类器。由于 GPT-2 是左右方向的,因此您需要提供嵌入序列的最终标记。
P.S - 你能把link写到纸上吗?
如果您使用 GPT-2 构建模型进行文本分类,请分享。