昇思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)
相关推荐
victory043112 小时前
从 2025-05 至 2026-05-15按时间顺序整理的“主线模型/技术报告”时间线
人工智能
广州灵眸科技有限公司12 小时前
瑞芯微(EASY EAI)RV1126B 模型部署API说明
linux·开发语言·网络·人工智能·深度学习·算法·yolo
哩哩橙12 小时前
分支电路对限时电流速断保护的影响
人工智能·笔记·数据挖掘
星夜夏空9912 小时前
STM32单片机学习(13) —— 串口通信协议
stm32·单片机·学习
a7520662813 小时前
钉钉+OpenClaw本地AI智能体:从开发者后台到消息互通全流程
人工智能·openclaw·小龙虾·openclaw部署·ai 办公自动化
每日新鲜事13 小时前
郎朗乐境音乐会定档7月5日深圳:以破界之姿,开启全维感官盛宴
人工智能
互联网科技看点13 小时前
2026年,园世Yuansea:以专业之名,重塑运动音频边界
大数据·人工智能·音视频
2601_9577867713 小时前
AI 原生营销矩阵系统:分布式素材管理与多租户权限控制技术实现
人工智能·分布式·矩阵
柿柿快乐13 小时前
用户认证系统实现文档
linux·服务器·网络·学习·基础教学
知识浅谈13 小时前
人工智能日报 每日AI新闻(2026年5月15日):OpenAI推进移动端Codex,Anthropic加码中小企业与公益合作,AI产品继续向真实工作流落地
人工智能·chatgpt