昇思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)
相关推荐
小许好楠几秒前
java开发工程师-学习方式
java·开发语言·学习
小帅学编程12 分钟前
JDBC学习
数据库·学习·oracle
泰迪智能科技0121 分钟前
分享|深化产教融合丨图书联合编写招募直播
人工智能
沐雪架构师28 分钟前
OpenAgents:让AI智能体Agent像人类一样联网协作
人工智能
其美杰布-富贵-李35 分钟前
Python 反射完整学习笔记
笔记·python·学习·反射
我要充满正能量40 分钟前
拥抱AI Coding,让我更自信能胜任我的工作
人工智能·ai编程·claude
charlie1145141911 小时前
快速在WSL上开发一般的C++上位机程序
开发语言·c++·笔记·学习·环境配置·工程
安达发公司1 小时前
安达发|效率革命:APS自动排程,为“金属丛林”安装精准导航
大数据·运维·人工智能·aps高级排程·aps排程软件·安达发aps·aps自动排程
神州问学1 小时前
AI 智能体攻陷软件工程:从 SWE-Agent 到 SWE-Swiss,全景解析 AI4SE 最新战局
人工智能
森诺Alyson1 小时前
前沿技术借鉴研讨-2025.12.23(荟萃分析/信号提取/轻量级模型)
论文阅读·人工智能·经验分享·论文笔记·论文讨论