昇思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)
相关推荐
创世宇图19 分钟前
【AI入门知识点】Harness 是什么?为什么 DeepSeek 要组建 Harness 团队?
人工智能·ai·harness
Henry-SAP24 分钟前
SAP MRP核心概念与学习路线解析
人工智能·sap
Terrence Shen2 小时前
Agent面试八股文(系列之二)
人工智能·大模型·agent·rag
爱睡懒觉的焦糖玛奇朵6 小时前
【从视频到数据集:焦糖玛奇朵的魔法工具使用说明】
人工智能·python·深度学习·学习·算法·yolo·音视频
oy_mail7 小时前
2026教程:用Gemini解决PCB设计与EMC/EMI问题,工程师效率跃升指南(国内直访)
人工智能
Runawayliquor7 小时前
opbase:CANN 所有算子的公共地基
大数据·数据库·人工智能·算法
英辰朗迪AI获客7 小时前
AI动态简报之算力基建篇(2026.05.22)
人工智能
徐安安ye7 小时前
FlashAttention 为什么对序列长度这么“敏感”?
人工智能·算法
天行健,君子而铎7 小时前
2026国内政务数据安全平台排名评析:基于AI降噪、全链路、动态性
人工智能·政务
智塑未来7 小时前
app应用怎么接入广告?标准流程与落地实操方案全解析
大数据·网络·人工智能