昇思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)
相关推荐
西红柿土豆3 分钟前
基于BP神经网络的杂草智能识别系统
人工智能
风清扬雨5 分钟前
计算机视觉中的基于网格的卷绕算法全解析
人工智能·算法·计算机视觉
-嘟囔着拯救世界-11 分钟前
1️⃣ 智能体基础入门教学(2025年全新版本)
人工智能·python·aigc·教程·ai agent·智能体·coze
欣然~25 分钟前
OpenCV 在树莓派上进行实时人脸检测
人工智能·opencv·计算机视觉
Love__Tay30 分钟前
【学习笔记】Power BI 初级知识
笔记·学习·数据分析·powerbi
Ombré_mi33 分钟前
多模态技术概述(一)
人工智能·语言模型·aigc
LaughingZhu36 分钟前
PH热榜 | 2025-04-03
前端·数据库·人工智能·经验分享·mysql·开源·产品运营
浪淘沙jkp1 小时前
大模型学习五:‌DeepSeek Janus-Pro-7B 多模态半精度本地部署指南:环境是腾讯cloudstudio高性能GPU 16G免费算力
学习·deepseek·janus-pro·janus-pro-7b
硬水果糖1 小时前
神经网络之损失函数
人工智能·深度学习·神经网络