昇思25天学习打卡营第23天|基于mindspore bert对话情绪识别

Interesting thing!

About Bert you just need to know that it is like gpt, but focus on pre-training Encoder instead of decoder. It has a mask method which enhances its precision remarkbably. (judge not only the word before the blank but the later one )

model : BertForSequenceClassfication constructs the model and load the config and set the sentiment classification to 3 kinds

python 复制代码
model = BertForSequenceClassification.from_pretrained('bert-base-chinese', num_labels = 3)
model = auto_mixed_precision(model, '01')
optimizer = nn.Adam(model.trainable_params(), learning_rate = 2e-5)
metric = Accuracy()
ckpoint_cb =  CheckpointCallback(save_path = 'checkpoint', ckpt_name = 'bert_emotect', epochs = 1, keep_checkpoint_max = 2)
best_model_cb = BestModelCallback(save_path = 'checkpoint', ckpt_name = 'bert_emotect_best', auto_load = True)
trainer = Trainer(network = model, train_dataset = dataset_train,
                    eval_dataset=dataset_val, metrics = metric,
                    epochs = 5, optimizer = optimizer, callback = [ckpoint_cb, best_model_cb])
trainer.run(tgt_columns = 'labels')

the model validation and prediction are the same mostly like Sentiment by any model:

python 复制代码
evaluator = Evaluator(network = model, eval_dataset = dataset_test, metrics= metric)
evaluator.run(tgt_columns='labels')

dataset_infer = SentimentDataset('data/infer.tsv')
def predict(text, label = None):
    label_map = {0:'消极', 1:'中性', 2:'积极'}
    text_tokenized = Tensor([tokenizer(text).input_ids])
    logits = model(text_tokenized)
    predict_label = logits[0].asnumpy().argmax()
    info = f"inputs:'{text}',predict:
'{label_map[predict_label]}'"
    if label is not None:
        info += f", label:'{label_map[label]}'"
    print(info)
相关推荐
万事可爱^1 分钟前
LangChain v1.0学习笔记(4)—— 核心组件Models
人工智能·笔记·学习·langchain·大模型
Frdbio4 分钟前
环腺苷酸(cAMP)ELISA检测试剂盒
linux·人工智能·python
dazzle6 分钟前
计算机视觉处理(OpenCV基础教学(二十二):霍夫变换技术详解)
人工智能·opencv·计算机视觉
狗狗学不会15 分钟前
RK3588 极致性能:使用 Pybind11 封装 MPP 实现 Python 端 8 路视频硬件解码
人工智能·python·音视频
Aevget15 分钟前
Kendo UI for jQuery 2025 Q4新版亮点 - AI 助手持续加持,主力开发更智能
人工智能·ui·jquery·界面控件·kendo ui
北京耐用通信16 分钟前
耐达讯自动化CANopen转Profibus网关在矿山机械RFID读写器应用中的技术分析
人工智能·科技·物联网·自动化·信息与通信
飞睿科技17 分钟前
UWB技术在机器人领域的创新应用与前景
网络·人工智能·机器人·定位技术·uwb技术
空山新雨后、18 分钟前
RAG:搜索引擎与大模型的完美融合
人工智能·搜索引擎·rag
sld16823 分钟前
以S2B2C平台重构快消品生态:效率升级与价值共生
大数据·人工智能·重构
love530love25 分钟前
EPGF 新手教程 21把“环境折磨”从课堂中彻底移除:EPGF 如何重构 AI / Python 教学环境?
人工智能·windows·python·重构·架构·epgf