使用LLM自动标记数据集并构建监督主题模型

2024年09月05日 由 alex 发表 64 0

在这篇文章中,我将探讨如何利用大型语言模型(LLM)来自动化创建有监督主题模型的标注过程。


为什么要对文本项目进行监督?

可解释的文本类别可以为数据集的讨论内容提供有价值的见解,而不仅仅是主题频率,你还可以捕捉随时间变化的趋势。这些信息有助于指导业务战略,甚至成为预测模型的有用特征。


Latent Dirichlet Allocation (LDA) 等无监督方法可以作为一个很好的起点,但当它们生成的主题与你实际感兴趣的主题不一致时,往往就会出现问题。例如,如果你关心的主题只占数据集的一小部分,那么它可能会与更常见的主题混在一起。要想获得精确的结果,理想的方法是使用标注良好的训练数据集的监督学习模型。


传统上,我会手动标记这些数据集,阅读并标记成百上千个文本。这样做虽然能深入理解数据,但却耗费大量人力。不过,它能确保主题直接相关。


输入 LLM

LLM 在没有先验数据的情况下也能很好地标记文本。但是,它们也有局限性,尤其是在处理多个主题时。例如,要求 LLM 在一个提示中区分 20 个不同的主题,可能会导致混乱和结果不一致。此外,为每个单独的主题设置不同的提示/查询可能非常耗时或昂贵。


解决方案: 我建议使用 LLM 来迭代标注数据集并建立监督模型。具体过程如下:


1. 建立模型:

  • 将所有文本嵌入到某种数字表示法中(这里我建议使用 LLM 的嵌入,但也可以使用 TF-IDF 等其他方法)
  • 嵌入主题并创建初始监督模型,将主题作为正类(1),所有其他文本作为负类(0)。


2. 迭代改进:

  • 预测所有文本的主题相关性。
  • 将前 K 个预测结果发送给 LLM 进行验证。如果验证通过,则调整标签,使其与这些文本属于正相关类相对应。
  • 重新训练模型。


3. 最佳截止:

  • 训练集完成后,通过分析预测百分位数和错误率来确定最佳截止值。如果该百分位数的错误率高于某个阈值(在此我使用 50%),则停止并使用之前的阈值。


这种方法既能减少人工标注,又能保持准确性和相关性,从而产生一个为你的特定需求量身定制的监督主题模型。


举例说明: 使用 20 个新闻组数据集

下面是使用 20 个新闻组数据集的流程示例。我嵌入主题,然后应用迭代分类过程自动标注与 “税收 ”相关的文本。


import numpy as np
import pandas as pd
import ollama 
from sklearn.datasets import fetch_20newsgroups
from LLMTopicLabeler import LLMTopicLabeler
#load data
newsgroups_train = fetch_20newsgroups(subset='train')
def embed_topic_text(topic_text: str) -> np.array:
    # Use ollama to embed the topic text
    response = ollama.embeddings(model="mxbai-embed-large", prompt=topic_text)
    temp_embedding = np.array(response["embedding"]).reshape(1,-1)
    return temp_embedding
#embed and append raw text to df
embeddings = [embed_topic_text(x) for x in newsgroups_train.data]
embeddings_pd = pd.DataFrame(np.array(embeddings)[:,0,:])
embeddings_pd.columns = ['embedding_' + str(x) for x in range(1024)]
embeddings_pd['paragraph'] = newsgroups_train.data
#build model
auto_classifier = LLMTopicLabeler()
auto_classifier.iterative_topic_classification('taxes', embeddings_pd, 
                                                                                            y_iterations = 5, quantiles_cutoff = [.999, .995,.99,.985])
#predict
predictions = auto_classifier.predict(embeddings_pd)
#print the top five predicted texts of the class
for x in np.where(predictions == 1)[0][:5]:
    print(newsgroups_train.data[x])
    print('\n')
    print('_______')


最终模型成功识别了相关文档,减少了主题标注的人工工作量。


...
>Why don't the Republicans get their act together, and say they
>will support a broad-based VAT that would have to be visible
>(the VAT in Canada is visible unlike the invisible VATS they
>have in Europe)
...


...
As it turned out, the taxes killed the Reagan expansion and the caps
on spending increases were dispelled by Clinton in his first act as
President (so that he could create his own new plan with more tax
increases).
...


结论

通过在迭代过程中利用 LLM,我们可以大大减轻人工标注的负担,同时建立更相关的监督主题模型。这种方法可以准确识别无监督方法可能会遗漏的利基主题,从而获得更好的洞察力,并推动更精确的数据驱动策略。

文章来源:https://medium.com/@samcarlos_14058/using-llms-to-automaticly-label-datasets-and-build-supervised-topic-models-41aee81f5b2b
欢迎关注ATYUN官方公众号
商务合作及内容投稿请联系邮箱:bd@atyun.com
评论 登录
写评论取消
回复取消