昇思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)
相关推荐
兰亭妙微UI设计公司2 分钟前
兰亭妙微ui设计公司分享:用AI原型提前发现复杂交互问题
人工智能
AAwangzong4 分钟前
企业级AI智能体厂商对比:模型服务、Agent平台与流程自动化如何选择?
人工智能·经验分享·rpa·智能体
Gu_WenYun4 分钟前
市场波动应对策略:灵活与坚定的平衡
人工智能·金融·区块链
Allen_LVyingbo6 分钟前
三甲医院AI的1163实施路径(上)
人工智能·自然语言处理·自动化·健康医疗·数据库架构
平头哥技术团队7 分钟前
把端到端延迟当成走时误差:高端腕表主题里的“AI 端渲“
人工智能·microsoft
吃鱼的灰太狼8 分钟前
Qwen-image-2.1 本地部署保姆级教程 | 首页
人工智能·语言模型·开源
victory04319 分钟前
2026年10月26日 论文中期 简历准备 算法能力建设与GPT讨论
人工智能
m4Rk_21 分钟前
【论文阅读】Agent记忆机制(112):MemFit——保留对话原文,用多路检索降低长期记忆成本
论文阅读·人工智能·学习·开源·github
AI日报派送佬22 分钟前
2026年10月21日AI行业日报|DeepSeek开源推理框架DeepInfer,谷歌TorchTPU开源破局TPU生态
人工智能·开源·openai·谷歌·英伟达·deepseek·ai日报
CAIE研习社22 分钟前
CAIE一级有学历和专业门槛吗?报考条件、考试内容与适合人群解析
大数据·人工智能