---
title: sentiment_classification
canonical_url: "https://www.modelscope.cn/models/Marmeladov/sentiment_classification"
md_url: "https://www.modelscope.cn/models/Marmeladov/sentiment_classification.md"
repository: Marmeladov/sentiment_classification
chinese_name: "中文评论正负情感分类"
last_updated: 2024-01-25
license: "Apache License 2.0"
pipeline_tag: text-classification
tasks:
  - text-classification
parameters: 1.6M
tensor_type:
  - F32
library_name:
  - pytorch
  - safetensors
frameworks:
  - Pytorch
downloads: 449
stars: 4
---

# sentiment_classification

> sentiment_classification - Marmeladov 在 ModelScope 开源的模型。判断用户评论情感是积极的还是消极的。

Marmeladov/sentiment_classification 是 ModelScope 魔搭社区上的 1.6M 参数text-classification模型，采用 Apache License 2.0 许可。

- **Repository**: Marmeladov/sentiment_classification
- **License**: Apache License 2.0
- **Tasks**: text-classification
- **Parameters**: 1.6M
- **Downloads**: 449
- **Stars**: 4
- **Last updated**: 2024-01-25

Source: https://www.modelscope.cn/models/Marmeladov/sentiment_classification

---

# 情感分类模型
## 模型简介
此文件夹主要包含微调qwen_1_8b模型得到的模型参数。微调后模型能够判断中文评论或语句包含积极情感还是消极情感.  
微调数据集数据集参考了StructBERT情感分类：  
https://modelscope.cn/models/iic/nlp_structbert_sentiment-classification_chinese-tiny/summary  
情感分类任务，通常为输入一段句子或一段话，返回该段话正向/负向的情感极性，在用户评价，观点抽取，意图识别中往往起到重要作用。本中文情感分类模型是基于dianping、jd binary两个数据集部分数据训练出来的情感分类模型。其实这上一句话也是从StructBERT那抄的。

#### Clone with HTTP
```bash
 git clone https://www.modelscope.cn/Marmeladov/sentiment_classification.git
```

<br>

## 实验环境
+ python>=3.8 
+ pytorch>=2.0.0
+ transformers==4.32.0
+ swift==1.5.3
+ CUDA 11.4

<br>

## 训练方法
> + 数据集:  
        商品评论情感预测：https://www.modelscope.cn/datasets/DAMO_NLP/jd  
        大众点评评价数据：https://www.modelscope.cn/datasets/DAMO_NLP/yf_dianping  
> + 数据集处理：
```
df=pd.read_csv('dev.csv')
df=df.drop(['dataset'],axis=1)
df['label']=df['label'].replace(1.0,"积极")
df['label']=df['label'].replace(0.0,"消极")
df = df.rename(columns={'sentence': 'query', 'label': 'response'})
df.to_csv("jd.csv",index=False,encoding="utf-8")
```
> + 微调模型：ModelType.qwen_1_8b：https://modelscope.cn/models/qwen/Qwen-1_8B
> + 微调方法：swift
```
    sft_args = SftArguments(
      model_type='ModelType.qwen_1_8b',  #使用quen_1_8b模型
      sft_type='lora',  #使用lora方法微调
      train_dataset_sample=10000, #训练数据集采样为10000
      custom_train_dataset_path=['/mnt/workspace/jd/train.csv','/mnt/workspace/yf_dianping/train.csv'], #自定义训练集
      custom_val_dataset_path=['/mnt/workspace/jd/dev.csv', '/mnt/workspace/yf_dianping/dev.csv'], #自定义验证集
      output_dir='output')
```

## 示例代码
```
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
import torch
from swift.llm import (InferArguments, infer_main)

torch.cuda.empty_cache()
infer_args = InferArguments(
ckpt_dir='../sentiment_classification',
load_dataset_config=True,
eval_human=True,
do_sample=False)
result = infer_main(infer_args)

...

<<<裙子不错
积极
--------------------------------------------------

<<<你去死吧
消极
--------------------------------------------------
```
