模型:
这是一种修改后的GPT2模型。它在ACL'23论文的发现中被介绍为零-shot文本分类的无标签预训练模型,作者为Christopher Clarke、Yuzhao Heng、Yiping Kang、Krisztian Flautner、Lingjia Tang和Jason Mars。训练和评估该模型的代码可以在 here 处找到。
该模型旨在用于零-shot文本分类。它以生成分类框架为基线,使用了经过方面归一化的 UTCD 数据集进行训练。
安装我们的 python package :
pip install zeroshot-classifier
然后,您可以像这样使用该模型:
>>> import torch
>>> from zeroshot_classifier.models import ZsGPT2Tokenizer, ZsGPT2LMHeadModel
>>> training_strategy = 'vanilla'
>>> model_name = f'claritylab/zero-shot-{training_strategy}-gpt2'
>>> model = ZsGPT2LMHeadModel.from_pretrained(model_name)
>>> tokenizer = ZsGPT2Tokenizer.from_pretrained(model_name, form=training_strategy)
>>> text = "I'd like to have this track onto my Classical Relaxations playlist."
>>> labels = [
>>> 'Add To Playlist', 'Book Restaurant', 'Get Weather', 'Play Music', 'Rate Book', 'Search Creative Work',
>>> 'Search Screening Event'
>>> ]
>>> inputs = tokenizer(dict(text=text, label_options=labels), mode='inference-sample')
>>> inputs = {k: torch.tensor(v).unsqueeze(0) for k, v in inputs.items()}
>>> outputs = model.generate(**inputs, max_length=128)
>>> decoded = tokenizer.batch_decode(outputs, skip_special_tokens=False)[0]
>>> print(decoded)
<|question|>How is the text best described? : " Rate Book ", " Search Screening Event ", " Add To Playlist ", " Search Creative Work ", " Get Weather ", " Play Music ", " Book Restaurant "<|endoftext|><|text|>I'd like to have this track onto my Classical Relaxations playlist.<|endoftext|><|answer|>Play Media<|endoftext|>