昇思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)
相关推荐
程序员cxuan7 小时前
Claude Code 为了封禁中国用户,竟然在代码里下毒
人工智能·后端·程序员
shushangyun_7 小时前
2026年AI知识库系统选型指南
人工智能
喵叔哟8 小时前
从聊天记录到真正的记忆:为 AI Agent 设计一套分层记忆系统
人工智能·langchain
道友可好8 小时前
Claude Code 泄露源码里的 89 个秘密
前端·人工智能·后端
大鱼>8 小时前
推荐系统原理与实现:协同过滤/内容推荐/深度学习
人工智能·深度学习
MartinYeung58 小时前
[论文学习]DP-Fusion:面向大语言模型的令牌级差分隐私推理-深度解析
人工智能·学习·语言模型
库拉大叔8 小时前
GPT提示词写法大全:不同场景的提问模板
人工智能·gpt
饼干哥哥8 小时前
我开发了一个「吃掉」别人Skill来升级的饕餮.Skill,现在免费开源
人工智能·设计模式·开源
阿拉斯攀登8 小时前
Embedding 模型选择与领域适配
人工智能·chatgpt·embedding·agent·ai编程·loop·rag
超级架构师8 小时前
Huiwen Han —— 论文与预印本目录 2026年7月
人工智能·架构·哲学