昇思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)
相关推荐
ValidationExpression几秒前
学习:企业标准的容器化 CI,CD 发布流程
学习·ci/cd
式5164 分钟前
大模型学习基础(九)LoRA微调原理
人工智能·深度学习·学习
GISer_Jing4 分钟前
2026年前端开发目标(From豆包)
前端·学习·aigc
鄭郑5 分钟前
【Playwright学习笔记 02】CSS-selector定位
笔记·学习
CCPC不拿奖不改名6 分钟前
python基础面试编程题汇总+个人练习(入门+结构+函数+面向对象编程)--需要自取
开发语言·人工智能·python·学习·自然语言处理·面试·职场和发展
菜鸟‍7 分钟前
【论文学习】一种用于医学图像分割单源域泛化的混合双增强约束框架 || 视觉 Transformer 在通用图像分割中的 “缺失环节”
人工智能·深度学习·计算机视觉
五度易链-区域产业数字化管理平台8 分钟前
数观丨2026年半导体集成电路产业融资分析
大数据·人工智能
应用市场8 分钟前
机器学习中的正向反馈循环:从原理到实战应用
人工智能·深度学习·机器学习
Nan_Shu_61415 分钟前
学习: Threejs (17)
学习·three.js
Engineer邓祥浩24 分钟前
设计模式学习(18) 23-16 迭代器模式
学习·设计模式·迭代器模式