昇思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 小时前
什么?有人手写 Skill?Agent Skill?Skill?
人工智能·llm·agent
俊哥V5 小时前
每日 AI 研究简报 · 2026-05-21
人工智能·ai
玛丽莲茼蒿6 小时前
Linux/Unix学习笔记(四)—— 进程管理
linux·学习·unix
2601_957884846 小时前
深度拆解:大模型RAG架构下,GEO优化的技术实现路径
人工智能·架构
这个DBA有点耶6 小时前
DBA的AI助手:向量检索与NL2SQL入门
数据库·人工智能·postgresql·学习方法·dba
YOLO数据集集合6 小时前
无人机航拍林业树种分割|单木树冠检测|三维点云|遥感影像数据集10059期
人工智能·yolo·目标检测·无人机
richxu202510016 小时前
学完了江科大STM32,下一步该怎么学?
stm32·单片机·嵌入式硬件·学习
Pocker_Spades_A6 小时前
工业智能化的时序选型指南:当数据底座遇见机器学习
人工智能·机器学习
2601_955781986 小时前
飞书远程控机:OpenClaw配置全攻略
人工智能·开源·github·飞书·open claw安装·open claw部署
Inhand陈工6 小时前
游轮WiFi覆盖方案复盘:6台5G CPE + AP实现全船高速上网
人工智能·物联网·网络协议·网络安全·信息与通信·iot