python - 用于文本分类的预训练模型

标签 python machine-learning keras text-classification pre-trained-model

所以我没有标签的单词很少,但我需要将它们分为 4-5 类。 我可以明显地说这个测试集可以分类。虽然我没有训练数据所以我需要使用预训练模型来对这些词进行分类。哪种模型适合这种范式?它已经在哪个数据集上接受过训练?

谢谢

最佳答案

我们正在执行的任务称为零样本主题分类 - 预测模型尚未接受过训练的主题。此范式由 Hugging Face 库支持,您可以阅读更多 here .最常见的预训练模型是 Bart Large MNLI - bart-large 的检查点在接受 MNLI dataset 训练后. 这是一个简单的例子,显示了短语“我喜欢热狗”的分类,没有任何初步训练:

  1. 首先,请安装 transformers 库:

    pip install --upgrade transformers
    
  2. 然后导入并初始化管道:

    from transformers import pipeline
    
    classifier = pipeline('zero-shot-classification', model='facebook/bart-large-mnli')
    
  3. 输入我们的玩具数据集:

     labels = ["artifacts", "animals", "food", "birds"]
     hypothesis_template = 'This text is about {}.'
     sequence = "I like hot dogs"
    
  4. 预测标签:

    prediction = classifier(sequence, labels, hypothesis_template=hypothesis_template, multi_class=True)
    
    print(prediction)
    

输出会是这样的

`{'sequence': 'i like hot dogs', 
'labels': ['food', 'animals', 'artifacts', 'birds'], 
'scores': [0.9971900582313538, 0.00529429130256176, 0.0020991512574255466, 
0.00023589911870658398]}`

可以解释为,模型将最高概率 (0.997..) 分配给标签“食物”,这是正确答案。

关于python - 用于文本分类的预训练模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65262832/

相关文章:

Python3+Kivy+Plyer 推送通知图标问题

python - 在 Pandas 中查找符合标准的比例最高的类别

algorithm - 为什么过度拟合给出了错误的假设函数

machine-learning - 启发式搜索和知情搜索之间的区别

python - Keras 关于数组形状的错误,但形状似乎是正确的

python - 如何在自动完成的 Eclipse PyDev 插件中加载 PyGTK 文档?

python - 如何为 MacOSX 后端设置 matplotlib 窗口大小?

machine-learning - "dynamic population"的进化算法

python-3.x - 在 Keras 中使用单热编码创建模型

python - 具有多个输入特征和多个输出的 LSTM