提交 2bf981c2 编写于 作者: Z Zeyu Chen

update Senta

上级 bb384c41
# senta # PaddleHub Senta
## 关于 ## 关于
本示例展示如何使用senta Module进行预测。 本示例展示如何使用PaddleHub Senta Module进行预测。
senta是中文情感分析模型,可以用于进行中文句子的情感分析,输出结果为`{正向/中性/负向}`中的一个,关于模型的训练细节,请查看[senta](https://github.com/baidu/senta) Senta是百度NLP开放的中文情感分析模型,可以用于进行中文句子的情感分析,输出结果为`{正向/中性/负向}`中的一个,关于模型的结构细节,请查看[Senta](https://github.com/baidu/senta), 本示例代码选择的是Senta-BiLSTM模型。
## 准备工作 ## 准备工作
...@@ -27,18 +27,25 @@ $ pip install --upgrade paddlepaddle ...@@ -27,18 +27,25 @@ $ pip install --upgrade paddlepaddle
## 命令行方式预测 ## 命令行方式预测
`infer.sh`给出了使用命令行调用Module预测的示例脚本 `cli_demo.sh`给出了使用命令行接口 (Command Line Interface) 调用Module预测的示例脚本
通过以下命令验下效果 通过以下命令验下效果
```shell ```shell
$ sh infer.sh $ sh cli_demo.sh
``` ```
## 通过python API预测 ## 通过python API预测
`infer_by_code.py`给出了使用python API调用Module预测的示例代码 `senta_demo.py`给出了使用python API调用Module预测的示例代码
通过以下命令试验下效果 通过以下命令试验下效果
```shell ```shell
python infer_by_code.py python senta_demo.py
```
## 通过PaddleHub Finetune API微调
`senta_finetune.py` 给出了如何使用Senta模型的句子特征进行Fine-tuning的实例代码。
可以运行以下命令在ChnSentiCorp数据集上进行Fine-tuning.
```shell
$ sh run_finetune.sh
``` ```
import os
import paddlehub as hub
def infer_with_input_text():
# get senta module
senta = hub.Module(name="senta")
test_text = ["这家餐厅很好吃", "这部电影真的很差劲"]
# get the input keys for signature 'sentiment_classify'
data_format = senta.processor.data_format(sign_name='sentiment_classify')
key = list(data_format.keys())[0]
# set input dict
input_dict = {key: test_text}
# execute predict and print the result
results = senta.sentiment_classify(data=input_dict)
for index, result in enumerate(results):
hub.logger.info("sentence %d segmented result: %s" %
(index + 1, result['sentiment_key']))
def infer_with_input_file():
# get senta module
senta = hub.Module(name="senta")
# get the input keys for signature 'sentiment_classify'
data_format = senta.processor.data_format(sign_name='sentiment_classify')
key = list(data_format.keys())[0]
# parse input file
test_file = os.path.join("test", "test.txt")
test_text = hub.io.parser.txt_parser.parse(test_file)
# set input dict
input_dict = {key: test_text}
results = senta.sentiment_classify(data=input_dict)
for index, result in enumerate(results):
hub.logger.info("sentence %d segmented result: %s" %
(index + 1, result['sentiment_key']))
if __name__ == "__main__":
infer_with_input_text()
export CUDA_VISIBLE_DEVICES=0 export CUDA_VISIBLE_DEVICES=0
# User can select chnsenticorp, nlpcc_dbqa, lcqmc for different task
DATASET="chnsenticorp" DATASET="chnsenticorp"
CKPT_DIR="./ckpt_${DATASET}" CKPT_DIR="./ckpt_${DATASET}"
python -u text_classifier.py \ python -u senta_finetune.py \
--batch_size=24 \ --batch_size=24 \
--use_gpu=False \ --use_gpu=False \
--checkpoint_dir=${CKPT_DIR} \ --checkpoint_dir=${CKPT_DIR} \
--num_epoch=10 --num_epoch=3
# coding: utf-8
import os
import paddlehub as hub
if __name__ == "__main__":
# Load Senta-BiLSTM module
senta = hub.Module(name="senta")
test_text = ["这家餐厅很好吃", "这部电影真的很差劲"]
input_dict = {"text": test_text}
results = senta.sentiment_classify(data=input_dict)
for index, result in enumerate(results):
print(test_text[index], result['sentiment_key'])
部电影真的很赞 家餐厅很好吃
售后太差! 这部电影真的很差劲
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册