昇思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 分钟前
当AI学会“讨好”:政务智能化的“泛娱乐化”陷阱与防治
人工智能
Mr.朱鹏2 分钟前
科技周报(第2026-08-17):开源与变现
人工智能·科技·开源
TechEdu2026062 分钟前
[人工智能]Tianshou强化学习框架:概念、架构与工程实践
人工智能·ai·rl
苏苏susuus4 分钟前
从像素到汉字:OCR 模型是如何依靠 CNN “学会“识字的?
人工智能·cnn·ocr
努力搬砖的咸鱼9 分钟前
意图理解:让Agent从需求描述自动生成Pytest测试策略
人工智能·python·ai·单元测试·pytest·agent
ZJU_统一阿萨姆12 分钟前
【推理优化进阶】性能实验科学:工作负载、统计显著性与尾延迟归因
开发语言·人工智能·语言模型·系统架构·vllm
weixin_5091383418 分钟前
【无标题】
人工智能·agi·智能体·认知动力学·智能体认知
IT_陈寒20 分钟前
Java Stream并行处理让我数据库崩了两次
前端·人工智能·后端
2601_9563198822 分钟前
2026年下半年,量化学习要把交易认知和技术实现接起来
人工智能·python
jay神22 分钟前
深度学习为什么需要反向传播
人工智能·深度学习·yolo·目标检测·cnn·毕业设计