昇思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)
相关推荐
工业视觉老A1 分钟前
攻克西林瓶液位人工灯检短板,AI 视觉检测保障灌装剂量稳定
人工智能·计算机视觉·视觉检测
一直C1 分钟前
嵌入式 Linux 驱动开发学习笔记:中断、等待队列与工作队列
linux·驱动开发·嵌入式硬件·学习·imx6ull
lucas_AI3 分钟前
证不动的引理别再死拆了:Maestro 给 Lean 证明装上「总指挥」,研究级定理多证 63%
人工智能·llm
一个热爱拳击的玩家7 分钟前
本地实体如何构建面向 AI 检索的信任链?以杭州拳击行业案例分析
大数据·人工智能·信任链
u1301308 分钟前
GitHub 热榜项目:日榜(2026-10-20)
人工智能·github
GitCode官方10 分钟前
共筑昇腾算子开源生态|CANNBench Season1 沙龙收官,Season2 全新评测集发布
人工智能·ai·开源·cann·atomgit
byte轻骑兵10 分钟前
时序数据库选型指南:面向工业物联网大数据场景,Apache IoTDB落地实践
大数据·人工智能·物联网·时序数据库·人大金仓
愚公移码11 分钟前
蓝凌aiBPM:AI 该不该编排审批流程
人工智能·流程引擎·蓝凌
我命由我1234515 分钟前
5-2.Python 编码进阶 - 字符串进阶(拼接、普通格式化、普通格式化数字精度控制、快速格式化、遍历、常用方法)
java·开发语言·后端·python·学习·java-ee·学习方法
在所不辞兄18 分钟前
【零基础学智能仿真-57】OpenFOAM工作流自动化
人工智能·机器学习