Emotion_diff模型
简介:本模型是基于swift框架,对qwen-7b-chat模型进行微调获得的。模型的功能是评估用户输入的一段话,模型能够准确给出这句话是消极的还是积极的。
1.实验环境
swift框架 ubuntu 22.04 cuda 12.0.1 python 3.10 pytorch 2.1.2
2.训练方法
数据集:jdsentimentzh
微调的模型:qwen-7b-chat
3.训练的步骤
3.1使用modelscope上的免费的GPU算力
3.2启动后进入命令行界面
3.3安装swift
通过以下命令安装
git clone https://github.com/modelscope/swift.git
cd swift
pip install -e .[llm]
4.微调代码
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
import torch
from swift.llm import (
DatasetName, InferArguments, ModelType, SftArguments,
infer_main, sft_main, app_ui_main, merge_lora_main
)
model_type = ModelType.qwen_7b_chat
sft_args = SftArguments(
model_type=model_type,
train_dataset_sample=2000,
dataset=[DatasetName.jd_sentiment_zh],
output_dir='output')
result = sft_main(sft_args)
best_model_checkpoint = result['best_model_checkpoint']
print(f'best_model_checkpoint: {best_model_checkpoint}')
torch.cuda.empty_cache()
infer_args = InferArguments(
ckpt_dir=best_model_checkpoint,
show_dataset_sample=10)
# merge_lora_main(infer_args)
result = infer_main(infer_args)
5.示例代码
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
import torch
from swift.llm import (
DatasetName, InferArguments, ModelType, SftArguments,
infer_main, sft_main, app_ui_main, merge_lora_main
)
best_model_checkpoint = "/mnt/workspace/output/qwen-7b-chat/v0-20240123-102915/checkpoint-100"
print(f'best_model_checkpoint: {best_model_checkpoint}')
torch.cuda.empty_cache()
infer_args = InferArguments(
ckpt_dir=best_model_checkpoint,
show_dataset_sample=10)
# merge_lora_main(infer_args)
infer_args.system="You are an expert in analyzing emotions. The user will give some Chinese or English sentences, and you only need to answer whether the emotion in the sentence is 'positive' or 'nagetive'. For example, the user inputs {Quality is very good, the material is very good, the workmanship is exquisite, the style is nice, the clothes are very beautiful}. Your answer is {positive}. Besides the emotional type of the sentence (positive, negative), you don't need to answer any other redundant words."
print(f"infer_args-->{infer_args.system}")
result = infer_main(infer_args)
torch.cuda.empty_cache()
评论