模型训练与验证minicpm-v

minicpm-v 模型进行微调并进行验证

训练使用混合数据集进行训练,对minicpm-V进行lora微调,微调后使用llama3_1对输出结果与标签值进行比对,计算准确率。
验证代码为:

python 复制代码
# URL = https://swift.readthedocs.io/zh-cn/latest/LLM/VLLM%E6%8E%A8%E7%90%86%E5%8A%A0%E9%80%9F%E4%B8%8E%E9%83%A8%E7%BD%B2.html

'''
使用swift进行部署的示例
model: llama3_1-8b-instruct
CUDA_VISIBLE_DEVICES=2 swift deploy --max_model_len 4096 --model_type llama3_1-8b-instruct --model_id_or_path /nas/share/model/huggingface/models--meta-llama--Meta-Llama-3.1-8B-Instruct/snapshots/8c22764a7e3675c50d4c7c9a4edb474456022b16
'''

# 客户端
# example of using swift client

import os
import json
from tqdm import tqdm
from swift.llm import get_model_list_client, XRequestConfig, inference_client

model_list = get_model_list_client()
model_type = model_list.data[0].id


def get_data_jsonl(data_path):
    datas =[]
    with open(data_path, 'r') as f:
        data = f.readlines()
        
    for i in range(len(data)):
        datas.append(json.loads(data[i]))
        
    return datas

def save_data_jsonl(save_path:str, datas: list):
    with open(save_path, 'w') as f:
        for data in datas:
            json.dump(data, f,ensure_ascii=False)
            f.write('\n')
    return save_path


def get_save_path(data_path:str):
    save_path =os.path.splitext(data_path)[0] + '_llm_eval.jsonl'
    if not os.path.exists(os.path.dirname(save_path)):
        os.makedirs(os.path.dirname(save_path))
    return save_path
    
def get_result_correct_path(data_path:str):
    result_correct_path =os.path.splitext(data_path)[0] + '_llm_eval_correct.json'
    if not os.path.exists(os.path.dirname(result_correct_path)):
        os.makedirs(os.path.dirname(result_correct_path))
    return result_correct_path

def save_result_correct(data_path:str,precision:float):
    result =  {
        'data_path': data_path,
        'precision': precision
    }
    with open(data_path, 'w') as f:
        json.dump(result, f,ensure_ascii=False)
    print(f'precision: {precision*100}%')
    return result


def main():
    
    def q_tempalte(response, reply):
        question_template = '''你可以作为一个语言专家,判断一下两个回答是否相同吗 \n
                "response": "{}",
                "reply": "{}",
                ----
                如果是,返回"YES",否则返回"NO"。
                '''.format(response, reply)
        return question_template

    # origin
    data_paths = 
    
    for data_path in tqdm(data_paths):
        datas = get_data_jsonl(data_path)

        save_data =[]
        total_correct = 0
        total = 0
        for data in tqdm(datas):
            
            response = data['response']
            reply = data['reply']
            
            question = q_tempalte(response, reply)
            
            request_config = XRequestConfig(max_tokens=32, temperature=0.1, seed=42)    
            resp = inference_client(model_type, question, request_config=request_config)
            response = resp.choices[0].message.content
            data['llm_response'] = response
            save_data.append(data)
            
            if response.lower() == 'yes':
                total_correct += 1
                
            total += 1
            
            precision = total_correct / total    

        # 保存数据
        save_path = get_save_path(data_path)
        save_data_jsonl(save_path, save_data)
        
        # 保存精度
        result_correct_path = get_result_correct_path(data_path)
        save_result_correct(result_correct_path, precision)
if __name__ == '__main__':
    main()
相关推荐
INDEMIND35 分钟前
INDEMIND:AI视觉赋能服务机器人,“零”碰撞避障技术实现全天候安全
人工智能·视觉导航·服务机器人·商用机器人
慕容木木39 分钟前
【全网最全教程】使用最强DeepSeekR1+联网的火山引擎,没有生成长度限制,DeepSeek本体的替代品,可本地部署+知识库,注册即可有750w的token使用
人工智能·火山引擎·deepseek·deepseek r1
南 阳44 分钟前
百度搜索全面接入DeepSeek-R1满血版:AI与搜索的全新融合
人工智能·chatgpt
企鹅侠客1 小时前
开源免费文档翻译工具 可支持pdf、word、excel、ppt
人工智能·pdf·word·excel·自动翻译
冰淇淋百宝箱1 小时前
AI 安全时代:SDL与大模型结合的“王炸组合”——技术落地与实战指南
人工智能·安全
Elastic 中国社区官方博客2 小时前
Elasticsearch Open Inference API 增加了对 Jina AI 嵌入和 Rerank 模型的支持
大数据·人工智能·elasticsearch·搜索引擎·ai·全文检索·jina
美狐美颜sdk2 小时前
直播美颜工具架构设计与性能优化实战:美颜SDK集成与实时处理
深度学习·美颜sdk·第三方美颜sdk·视频美颜sdk·美颜api
AWS官方合作商2 小时前
Amazon Lex:AI对话引擎重构企业服务新范式
人工智能·ai·机器人·aws
workflower2 小时前
Prompt Engineering的重要性
大数据·人工智能·设计模式·prompt·软件工程·需求分析·ai编程
curemoon3 小时前
理解都远正态分布中指数项的精度矩阵(协方差逆矩阵)
人工智能·算法·矩阵