美文网首页
动手学习RAG: 大模型向量模型微调 intfloat/e5-m

动手学习RAG: 大模型向量模型微调 intfloat/e5-m

作者: YueTan | 来源:发表于2024-09-14 15:45 被阅读0次

[图片上传失败...(image-acf3f5-1726386268674)]

1. 环境准备

pip install transformers
pip install open-retrievals

2. 使用Mistral作为向量模型

这里直接将query_instruction和document_instruction写进了text里

from retrievals import AutoModelForEmbedding

model_name = 'intfloat/e5-mistral-7b-instruct'
model = AutoModelForEmbedding.from_pretrained(
            model_name,
            pooling_method='last',
            use_fp16=True,
        )

texts = [
'Instruct: Given a web search query, retrieve relevant passages that answer the query\nQuery: how much protein should a female eat', 
'Instruct: Given a web search query, retrieve relevant passages that answer the query\nQuery: summit define', 
"As a general guideline, the CDC's average requirement of protein for women ages 19 to 70 is 46 grams per day. But, as you can see from this chart, you'll need to increase that if you're expecting or training for a marathon. Check out the chart below to see how much protein you should be eating each day.", 
'Definition of summit for English Language Learners. : 1  the highest point of a mountain : the top of a mountain. : 2  the highest level. : 3  a meeting or series of meetings between the leaders of two or more governments.'
]

embeds = model.encode(texts, normalize_embeddings=True)
print(embeds)

scores = (embeds[:2] @ embeds[2:].T) * 100
print(scores.tolist())

[图片上传失败...(image-565283-1726386268674)]

  • 也可以把prompt写在函数中
from retrievals import AutoModelForEmbedding

model_name = 'intfloat/e5-mistral-7b-instruct'
model = AutoModelForEmbedding.from_pretrained(
            model_name,
            pooling_method='last',
            use_fp16=True,
            query_instruction='Instruct: Given a web search query, retrieve relevant passages that answer the query\nQuery: ',
            document_instruction='',
        )


query_texts = ['how much protein should a female eat', 'summit define']
document_texts = ["As a general guideline, the CDC's average requirement of protein for women ages 19 to 70 is 46 grams per day. But, as you can see from this chart, you'll need to increase that if you're expecting or training for a marathon. Check out the chart below to see how much protein you should be eating each day.", 'Definition of summit for English Language Learners. : 1  the highest point of a mountain : the top of a mountain. : 2  the highest level. : 3  a meeting or series of meetings between the leaders of two or more governments.']

query_embeds = model.encode(query_texts, normalize_embeddings=True, is_query=True)
print(query_embeds)

doc_embeds = model.encode(document_texts, normalize_embeddings=True, is_query=False)
print(doc_embeds)

scores = (query_embeds @ doc_embeds.T) * 100
print(scores.tolist())

3. LoRA微调E5-mistral向量模型

数据还是按照惯例采用t2-ranking

MODEL_NAME="intfloat/e5-mistral-7b-instruct"
TRAIN_DATA="/root/kag101/src/open-retrievals/t2/t2_ranking.jsonl"
OUTPUT_DIR="/root/kag101/src/open-retrievals/t2/ft_out"


torchrun --nproc_per_node 1 \
  -m retrievals.pipelines.embed \
  --output_dir $OUTPUT_DIR \
  --overwrite_output_dir \
  --model_name_or_path $MODEL_NAME \
  --pooling_method last \
  --do_train \
  --data_name_or_path $TRAIN_DATA \
  --positive_key positive \
  --negative_key negative \
  --use_lora True \
  --query_instruction 'Instruct: Given a web search query, retrieve relevant passages that answer the query\nQuery: ' \
  --document_instruction '' \
  --learning_rate 1e-5 \
  --bf16 \
  --num_train_epochs 3 \
  --per_device_train_batch_size 2 \
  --gradient_accumulation_steps 16 \
  --dataloader_drop_last True \
  --query_max_length 64 \
  --document_max_length 256 \
  --train_group_size 2 \
  --logging_strategy steps \
  --logging_steps 100 \
  --temperature 0.02 \
  --use_inbatch_negative false \
  --save_total_limit 1

[图片上传失败...(image-6e9b11-1726386268674)]

由于trainer中可以使用多种方式使用多GPU,因此retrievals也都支持。

# torchrun --nnodes 1 --nproc-per-node 4
# deepspeed --include localhost:0,1,2,3
# CUDA_VISIBLE_DEVICES=1,2,3 python
# accelerate launch --config_file conf_ds.yaml \

accelerate launch \
    --config_file conf_llm.yaml \
    llm_finetune_for_embed.py \
    --model_name_or_path mistralai/Mistral-7B-v0.1 \
    --train_data  \
    --output_dir output \

4. 评测

微调前性能 c-mteb t2-ranking score
[图片上传失败...(image-2fc63a-1726386268674)]

微调后性能

[图片上传失败...(image-57f00a-1726386268674)]

相关文章

  • 任务1:词向量分布

    要点一:提问:词向量是学习得来的吗? 答:是的。段落+模型->词向量 要点二:什么是语言模型 ?训练语言模型是要让...

  • 微调模型

  • 【NLP】TextCNN

    模型 四种模式 CNN-rand: 单词向量是随机初始化,向量随着模型学习而改变 CNN-static: 使用预训...

  • python文本相似度计算

    步骤 分词、去停用词 词袋模型向量化文本 TF-IDF模型向量化文本 LSI模型向量化文本 计算相似度 理论知识 ...

  • 2020-05-22 第十三章 支持向量机模型(python)

    第十三章 支持向量机模型 01 支持向量机模型的介绍 模型介绍 SVM是Support Vector Machin...

  • 第四章 相似度分析算法——向量空间模型

    4.4 向量空间模型 向量空间模型是将文本转换为向量的代数模型,主要用于自然语言处理、文本分析等领域。目前,空间向...

  • BERT 详解(五)

    Fine-tuning 微调 微调阶段根据不同任务使用不同网络模型。在微调阶段,大部分模型的超参数跟预训练时差不多...

  • 如何将Bert句向量应用于深度神经网络中

    Bert开源了预训练的中文模型,如果你想直接使用Bert模型生成句子向量(当做一个黑盒),并用于深度学习模型中,本...

  • BERT微调模型

    使用BERT和Pytorch构建BERT微调模型,当然这里使用Pytorch的原因是用来比赛是比较方便的。 部分代...

  • 一个过程完整的机器学习项目

    基本步骤 纵览全局 获取数据 数据可视化、找规律 准备用于机器学习算法的数据 选择模型并进行训练 模型微调 展示解...

网友评论

      本文标题:动手学习RAG: 大模型向量模型微调 intfloat/e5-m

      本文链接:https://www.haomeiwen.com/subject/vkbbljtx.html