作者: admin

  • 如何用XGBoost对搜索结果进行优化排序

    XGBoost是一种强大的梯度提升算法,可以用于对搜索结果进行排序,从而提升搜索质量。下面将详细说明如何使用XGBoost进行搜索结果优化排序:

    1. 数据准备

    • 收集数据: 首先需要收集搜索结果的相关数据,包括:
      • 查询: 用户输入的搜索词
      • 文档: 与查询相关的搜索结果,每个文档包含标题、摘要、链接等信息
      • 相关性标签: 人工标注的查询与文档之间的相关性等级,例如:
        • 完美: 文档完全满足查询意图
        • 优秀: 文档高度相关,但可能缺少一些细节
        • 良好: 文档部分相关,可以提供一些有用信息
        • 较差: 文档与查询不太相关
        • 无关: 文档与查询完全无关
    • 特征工程: 将原始数据转换成模型可以理解的特征向量,常用的特征包括:
      • 查询特征: 查询词长度、查询词类型(如人物、地点、事件)、查询词的IDF值等
      • 文档特征: 文档长度、文档中关键词的TF-IDF值、文档的PageRank值、文档的新鲜度等
      • 查询-文档交互特征: 查询词与文档标题的相似度、查询词与文档摘要的相似度、查询词在文档中出现的频率等
    • 数据集划分: 将收集到的数据划分为训练集、验证集和测试集,用于模型训练、参数调优和最终效果评估。

    2. 模型训练

    • 选择目标函数: XGBoost支持多种目标函数,对于搜索结果排序问题,常用的目标函数是 Rank:Pairwise,它会比较两个文档的预测得分,并根据它们的真实相关性标签进行惩罚。
    • 设置评估指标: 选择合适的评估指标来衡量模型的排序效果,常用的指标包括:
      • NDCG (Normalized Discounted Cumulative Gain): 考虑了文档的相关性和位置,值越高表示排序效果越好。
      • MAP (Mean Average Precision): 计算每个查询的平均准确率,然后对所有查询进行平均,值越高表示排序效果越好。
    • 调整超参数: XGBoost 有许多超参数可以调整,例如树的数量、树的深度、学习率等。可以使用网格搜索或贝叶斯优化等方法来找到最佳的超参数组合。

    3. 模型评估和部署

    • 模型评估: 使用测试集评估训练好的模型的排序效果,并分析模型的优缺点。
    • 模型部署: 将训练好的模型部署到线上搜索系统中,对新的查询进行实时排序。

    示例代码 (Python)

    import xgboost as xgb
    from sklearn.model_selection import train_test_split
    from sklearn.metrics import ndcg_score
    
    # 加载数据
    # 假设数据已经处理成特征向量,并存储在 X 和 y 中
    # X: 特征矩阵,每行代表一个查询-文档对
    # y: 相关性标签,值越大表示相关性越高
    
    # 划分数据集
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
    
    # 定义 XGBoost 排序模型
    params = {
        'objective': 'rank:pairwise',
        'eval_metric': 'ndcg',
        'eta': 0.1,
        'max_depth': 6,
        'n_estimators': 100,
    }
    dtrain = xgb.DMatrix(X_train, label=y_train)
    dtest = xgb.DMatrix(X_test, label=y_test)
    
    # 训练模型
    model = xgb.train(params, dtrain, evals=[(dtest, 'eval')], num_boost_round=1000, early_stopping_rounds=10)
    
    # 预测排序
    y_pred = model.predict(dtest)
    
    # 评估模型
    ndcg = ndcg_score([y_test], [y_pred])
    print(f"NDCG: {ndcg}")
    
    # 保存模型
    model.save_model("xgb_ranking_model.bin")

    总结

    使用 XGBoost 对搜索结果进行优化排序是一个复杂的过程,需要进行数据准备、特征工程、模型训练、参数调优、模型评估和部署等多个步骤。同时,需要根据具体的业务场景和数据特点选择合适的特征、模型和评估指标,才能取得最佳的排序效果。


    NDCG 和 MAP 解析:

    在信息检索领域,评估排序结果好坏是非常重要的环节。NDCG 和 MAP 是常用的两种评估指标,它们都考虑了文档的相关性和位置信息,但计算方式有所不同。

    1. NDCG (Normalized Discounted Cumulative Gain): 归一化折损累计增益

    NDCG 是一种衡量排序质量的指标,它考虑了文档的相关性和位置,认为排名靠前的相关文档比排名靠后的相关文档更有价值。

    计算步骤:

    1. 计算每个文档的增益 (Gain): 根据文档的相关性等级,赋予每个文档一个增益值。例如,可以使用以下规则:
      • 完美: 3分
      • 优秀: 2分
      • 良好: 1分
      • 较差: 0分
      • 无关: 0分
    2. 计算累计增益 (Cumulative Gain): 将前 k 个文档的增益值累加起来,得到 CG@k。
    3. 计算折损累计增益 (Discounted Cumulative Gain): 对 CG@k 进行折损,将排名靠后的文档的增益值降低。常用的折损函数是 1/log2(i+1),其中 i 是文档的排名。
      • DCG@k = Σ(i=1 to k) [Gain(i) / log2(i+1)]
    4. 计算理想折损累计增益 (Ideal Discounted Cumulative Gain): 对完美排序下的 DCG@k 进行计算,得到 IDCG@k。完美排序是指所有相关文档都排在最前面。
    5. 计算归一化折损累计增益 (Normalized Discounted Cumulative Gain): 将 DCG@k 除以 IDCG@k,得到 NDCG@k。
      • NDCG@k = DCG@k / IDCG@k

    NDCG 的取值范围是 [0, 1],值越高表示排序效果越好。

    示例:

    假设有 5 个文档,相关性等级分别为:[完美, 优秀, 无关, 良好, 较差],则:

    • 完美排序: [完美, 优秀, 良好, 较差, 无关]
    • 模型排序: [完美, 无关, 优秀, 良好, 较差]

    计算 NDCG@3:

    • 完美排序:
      • DCG@3 = 3/log2(2) + 2/log2(3) + 1/log2(4) ≈ 4.26
      • IDCG@3 = 4.26 (因为是完美排序)
      • NDCG@3 = 4.26 / 4.26 = 1
    • 模型排序:
      • DCG@3 = 3/log2(2) + 0/log2(3) + 2/log2(4) ≈ 3.5
      • IDCG@3 = 4.26
      • NDCG@3 = 3.5 / 4.26 ≈ 0.82

    2. MAP (Mean Average Precision): 平均准确率均值

    MAP 是一种衡量检索系统在所有查询上的平均性能的指标,它考虑了每个查询的平均准确率 (Average Precision)。

    计算步骤:

    1. 计算每个查询的准确率 (Precision): 对于每个查询,计算前 k 个文档的准确率 P@k,即前 k 个文档中相关文档的比例。
    2. 计算每个查询的平均准确率 (Average Precision): 对于每个查询,计算所有相关文档位置上的准确率的平均值。
      • AP = Σ(k=1 to n) [P@k * rel(k)] / num_relevant_docs
      • 其中 n 是文档总数,rel(k) 表示第 k 个文档是否相关 (相关为 1,不相关为 0),num_relevant_docs 是相关文档的总数。
    3. 计算所有查询的平均准确率均值 (Mean Average Precision): 将所有查询的 AP 值进行平均。
      • MAP = Σ(q=1 to Q) [AP(q)] / Q
      • 其中 Q 是查询的总数。

    MAP 的取值范围也是 [0, 1],值越高表示排序效果越好。

    示例:

    假设有两个查询,每个查询返回 5 个文档,相关性标签如下:

    • 查询 1: [完美, 优秀, 无关, 良好, 较差]
    • 查询 2: [优秀, 无关, 良好, 完美, 较差]

    计算 MAP:

    • 查询 1:
      • P@1 = 1/1 = 1, P@2 = 2/2 = 1, P@3 = 2/3 ≈ 0.67, P@4 = 3/4 = 0.75, P@5 = 3/5 = 0.6
      • AP = (11 + 11 + 0.670 + 0.751 + 0.6*0) / 3 ≈ 0.81
    • 查询 2:
      • P@1 = 1/1 = 1, P@2 = 1/2 = 0.5, P@3 = 2/3 ≈ 0.67, P@4 = 3/4 = 0.75, P@5 = 3/5 = 0.6
      • AP = (11 + 0.51 + 0.671 + 0.751 + 0.6*0) / 4 ≈ 0.73
    • MAP = (0.81 + 0.73) / 2 = 0.77

    总结:

    • NDCG 更加关注排名靠前的文档,适用于评估 Top-K 排序结果。
    • MAP 综合考虑了所有相关文档的位置,适用于评估整体排序性能。

    选择哪种指标取决于具体的应用场景和需求。


    NDCG 和 MAP 计算实例详解

    为了更清晰地解释 NDCG 和 MAP 的计算过程,我们用一个具体的例子来说明。

    假设有一个搜索引擎,用户提交了两个查询 Query 1 和 Query 2,每个查询返回了 5 个结果,每个结果的相关性等级已经标注好,如下表所示:

    查询排名文档相关性等级Gain
    Query 11A完美3
    2B优秀2
    3C无关0
    4D良好1
    5E较差0
    Query 21F优秀2
    2G无关0
    3H良好1
    4I完美3
    5J较差0

    1. NDCG 计算

    我们以 NDCG@3 为例,分别计算 Query 1 和 Query 2 的 NDCG@3,然后取平均值。

    Query 1:

    • 计算 DCG@3:
      • DCG@3 = 3/log2(1+1) + 2/log2(2+1) + 0/log2(3+1) ≈ 3.52
    • 计算 IDCG@3:
      • 完美排序为:[A, B, D],因此:
      • IDCG@3 = 3/log2(1+1) + 2/log2(2+1) + 1/log2(3+1) ≈ 4.26
    • 计算 NDCG@3:
      • NDCG@3 = DCG@3 / IDCG@3 ≈ 3.52 / 4.26 ≈ 0.83

    Query 2:

    • 计算 DCG@3:
      • DCG@3 = 2/log2(1+1) + 0/log2(2+1) + 1/log2(3+1) ≈ 2.13
    • 计算 IDCG@3:
      • 完美排序为:[F, H, I],因此:
      • IDCG@3 = 2/log2(1+1) + 1/log2(2+1) + 3/log2(3+1) ≈ 4.52
    • 计算 NDCG@3:
      • NDCG@3 = DCG@3 / IDCG@3 ≈ 2.13 / 4.52 ≈ 0.47

    平均 NDCG@3:

    • (0.83 + 0.47) / 2 = 0.65

    2. MAP 计算

    分别计算 Query 1 和 Query 2 的 AP (Average Precision),然后取平均值。

    Query 1:

    • 相关文档有:A, B, D,共 3 个
    • P@1 = 1/1 = 1
    • P@2 = 2/2 = 1
    • P@3 = 2/3 ≈ 0.67
    • P@4 = 3/4 = 0.75
    • P@5 = 3/5 = 0.6
    • AP = (11 + 11 + 0.670 + 0.751 + 0.6*0) / 3 ≈ 0.81

    Query 2:

    • 相关文档有:F, H, I,共 3 个
    • P@1 = 1/1 = 1
    • P@2 = 1/2 = 0.5
    • P@3 = 2/3 ≈ 0.67
    • P@4 = 3/4 = 0.75
    • P@5 = 3/5 = 0.6
    • AP = (11 + 0.50 + 0.671 + 0.751 + 0.6*0) / 3 ≈ 0.64

    平均 MAP:

    • (0.81 + 0.64) / 2 = 0.725

    总结:

    通过以上例子,我们可以看到 NDCG 和 MAP 都是用来评估搜索结果排序质量的指标,但它们侧重点有所不同。NDCG 更关注排名靠前的结果,而 MAP 则综合考虑了所有相关文档的位置。选择哪种指标取决于具体的应用场景和需求。


  • 法律智慧的知识注入:通过诊断和正负样本强化学习探索大语言模型咨询

    近年来,随着生成式大语言模型(LLMs)的广泛应用,其在法律领域也得到了越来越多的关注。然而,对于没有法律背景的用户来说,在面对法律案件时,他们往往难以用专业语言进行提问,也可能在向LLMs陈述案件时忽略关键的法律因素。为了解决这个问题,我们提出了诊断式法律大语言模型(D3LM),它利用类似律师的适应性诊断问题来收集额外的案件信息,并提供高质量的反馈。

    D3LM结合了一种创新的基于图的正负样本强化学习(PURL)算法,能够生成关键问题,并增强用户与LLMs的交互。此外,一个集成的基于LLMs的停止准则,可以实现精确的法院观点生成(CVG)。我们的研究还引入了一个新的基于美国案例法数据库的英语CVG数据集,为LLMs研究和部署领域增添了重要维度。D3LM超越了传统LLMs,在法律领域展现出卓越的性能和非凡的用户体验。

    法律服务的新纪元:D3LM的优势

    传统LLMs在法律咨询中存在局限性,用户往往需要自行组织语言,而LLMs则无法主动引导用户提供更详细的信息。D3LM则不同,它就像一位专业的律师,通过一系列针对性的问题,引导用户提供更多案件细节,从而更准确地预测法律结果。

    例如,假设一位客户因酒吧斗殴而被指控故意伤害。传统LLMs可能会基于客户提供的模糊描述,给出笼统的法院观点,但由于信息不足,可能会忽略关键细节。而律师则会通过一系列针对性的问题,深入了解案件细节,例如:”您当时是否处于酒精影响下?“,”酒吧是否有监控摄像头记录了事件?“。D3LM则能够自动生成类似的问题,在不增加额外成本的情况下,更深入地理解案件,并提高法律结果预测的准确性。

    知识图谱与强化学习:D3LM的核心技术

    D3LM的核心技术在于将LLMs与法律知识图谱相结合,并利用正负样本强化学习(PURL)算法来生成关键问题。

    1. 法律知识图谱: D3LM将美国案例法数据库中的案件信息转化为结构化的事实-规则图,并利用“问题、规则、分析、结论”(IRAC)框架,将复杂的案件叙述简化为简洁的表示形式。

    2. 正负样本强化学习: D3LM通过随机遮蔽事实节点,生成一系列关于案件的潜在问题。然后,利用LLMs对遮蔽后的案件描述进行重建,并生成相应的法院观点。通过比较重建后的法院观点与真实法院观点,模型可以学习到哪些问题对于预测法律结果更重要。

    3. 法院观点生成: D3LM基于PURL算法,能够根据用户提供的案件信息,生成更准确的法院观点。它能够识别案件中的关键因素,并通过一系列针对性的问题,引导用户提供更详细的信息,从而提高法院观点生成的准确性和可靠性。

    突破性数据集:为法律AI研究提供新基准

    为了更好地评估D3LM的性能,我们创建了一个全新的英语CVG数据集,该数据集基于美国案例法数据库,并经过法律专业人士的严格审核。该数据集弥补了英语法律分析数据集的不足,为法律AI研究提供了新的基准。

    实验结果:D3LM的卓越表现

    我们对D3LM进行了全面的评估,并将其与其他基准模型进行了比较。实验结果表明,D3LM在生成美国法院观点方面表现出色,在ROUGE和BLEU指标上均取得了最佳成绩。

    此外,我们还进行了用户体验测试,结果表明,用户对D3LM的可靠性和满意度评分均高于GPT-4.0。这表明,D3LM的交互式提问方式,更能满足用户对法律咨询的实际需求。

    展望未来:法律AI的无限可能

    D3LM的出现,为法律AI研究开辟了新的道路。未来,我们将进一步探索D3LM在其他领域,例如医疗和咨询领域的应用,使其能够为更多用户提供更便捷、更精准的服务。

    参考文献

    • Achiam, J., et al. (2023). “ChatGPT: Optimizing Language Models for Dialogue.” arXiv preprint arXiv:2212.00183.
    • Auer, P., et al. (2002). “Finite-time analysis of the multiarmed bandit problem.” Machine learning, 47(2-3), 235-256.
    • Brescia, E., et al. (2014). “The cost of justice: A comparative analysis of legal aid systems in Europe.” European Journal of Law and Economics, 37(3), 221-242.
    • Caselaw Access Project (2024). “Caselaw Access Project.” Retrieved from https://casetext.com/
    • Chapelle, O., and Li, L. (2011). “An empirical evaluation of thompson sampling.” Advances in neural information processing systems, 24.
    • Chen, H., et al. (2020). “Predictive adversarial learning for positive-unlabeled learning.” Proceedings of the AAAI Conference on Artificial Intelligence, 34(04), 3420-3427.
    • Chen, J., et al. (2022). “Law article recommendation based on user interest and legal knowledge graph.” Journal of Grid Computing, 20(1), 1-14.
    • Chen, Z., et al. (2023). “DISCO: Data Augmentation for Natural Language Understanding via Counterfactual Examples.” arXiv preprint arXiv:2303.17159.
    • Chu, W., et al. (2011). “Contextual bandits with linear payoff functions.” Proceedings of the 14th International Conference on Artificial Intelligence and Statistics, 1-10.
    • Cui, Y., et al. (2023). “ChatLaw: A Large Language Model for Legal Question Answering.” arXiv preprint arXiv:2304.04170.
    • Du Plessis, M. C., et al. (2015). “Deep learning for imbalanced datasets: A review.” arXiv preprint arXiv:1506.02291.
    • Gans-Morse, J. (2017). “The demand for legal services: A review of the literature.” Journal of Legal Studies, 46(S1), S1-S37.
    • Gensler, H. J. (1985). “Legal Reasoning: A Cognitive Approach.” Stanford Law Review, 38(1), 1-41.
    • Hadfield, G. K. (2010). “The economics of legal disputes.” In The Handbook of Law and Economics (pp. 1-51). Edward Elgar Publishing.
    • Horwitz, M. J. (2020). “The future of legal services: The rise of the legal tech revolution.” Harvard Law Review, 133(8), 2299-2320.
    • Hu, B., et al. (2021). “Predictive adversarial learning for positive-unlabeled learning with heterogeneous data.” IEEE Transactions on Neural Networks and Learning Systems, 32(11), 4938-4951.
    • Hu, W., et al. (2018). “Predicting charge decisions in criminal judgments using deep learning.” Proceedings of the 27th ACM International Conference on Information and Knowledge Management, 1189-1198.
    • Jin, Z., et al. (2024). “Legal Reasoning with Large Language Models: A Survey.” arXiv preprint arXiv:2401.06204.
    • Kiryo, R., et al. (2017). “Positive-unlabeled learning with non-negative risk estimator.” Advances in Neural Information Processing Systems, 30.
    • Lin, J., et al. (2012). “Predicting charge decisions in criminal judgments using a hybrid approach.” Proceedings of the 21st ACM International Conference on Information and Knowledge Management, 1201-1210.
    • Liu, Y., and Wu, Y. (2020). “Fake news detection on social media: A data mining perspective.” ACM SIGKDD Explorations Newsletter, 22(1), 1-11.
    • Liu, Y., et al. (2019). “RoBERTa: A Robustly Optimized BERT Pretraining Approach.” arXiv preprint arXiv:1907.11692.
    • Liu, Z., et al. (2022). “WANLI: A Large-Scale Chinese Legal Dataset for Legal Reasoning.” arXiv preprint arXiv:2208.08227.
    • Purba, M. S., and Syahrin, M. (2019). “The role of legal services in promoting economic growth and development.” Journal of Law, Policy and Globalization, 54, 1-10.
    • Robertson, S. E., and Walker, S. (1994). “Some simple effective approximations to the 2-poisson model for probabilistic retrieval.” Proceedings of the 17th annual international ACM SIGIR conference on Research and development in information retrieval, 232-241.
    • Schick, T., et al. (2023). “On the Importance of Completeness in Legal Reasoning: A Case Study with Large Language Models.” arXiv preprint arXiv:2303.14412.
    • Swayamdipta, S., et al. (2020). “Dataset Cartography: A Framework for Refining NLI Examples with GPT-3.” arXiv preprint arXiv:2009.05396.
    • Tong, H., et al. (2020). “Inductive representation learning on graphs.” Proceedings of the AAAI Conference on Artificial Intelligence, 34(04), 5041-5048.
    • Touvron, J., et al. (2023). “Llama 2: Open and Efficient Foundation Models.” arXiv preprint arXiv:2307.09286.
    • Wei, X., and Li, B. (2018). “Adversarial learning for positive unlabeled learning.” Proceedings of the 32nd AAAI Conference on Artificial Intelligence, 4427-4434.
    • Wu, Y., et al. (2020). “Attention and Counterfactual-based Court View Generation.” Proceedings of the 29th ACM International Conference on Information and Knowledge Management, 1885-1894.
    • Wu, Y., et al. (2023). “Predictive Adversarial Learning for Positive-Unlabeled Learning with Heterogeneous Data.” IEEE Transactions on Neural Networks and Learning Systems, 34(11), 4938-4951.
    • Xiao, J., et al. (2021). “Lawformer: A Pre-trained Language Model for Legal Text Understanding.” arXiv preprint arXiv:2106.01796.
    • Ye, Y., et al. (2018). “Predicting charge decisions in criminal judgments using a hybrid approach.” Proceedings of the 27th ACM International Conference on Information and Knowledge Management, 1189-1198.
    • Zamfirescu-Pereira, I., et al. (2023). “The Impact of Large Language Models on the Legal Profession: A Critical Analysis.” arXiv preprint arXiv:2305.11136.
    • Zhao, Y., et al. (2022). “Dist-PU: A Distribution-Based Approach for Positive-Unlabeled Learning.” Proceedings of the AAAI Conference on Artificial Intelligence, 36(12), 12638-12646.
    • Zhong, H., et al. (2018). “Predicting charge decisions in criminal judgments using a hybrid approach.” Proceedings of the 27th ACM International Conference on Information and Knowledge Management, 1189-1198.
    • Zhou, D., et al. (2020). “Neural contextual bandits with UCB exploration.” Proceedings of the AAAI Conference on Artificial Intelligence, 34(04), 5744-5751.
    • Zhou, Y., et al. (2021). “Positive-Unlabeled Learning for Recommendation with Implicit Feedback.” Proceedings of the 27th ACM SIGKDD International Conference on Knowledge Discovery & Data Mining, 2213-2222.
人生梦想 - 关注前沿的计算机技术 acejoy.com 🐾 步子哥の博客 🐾 背多分论坛 🐾 借一步网 沪ICP备2024052574号-1