昇思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)
相关推荐
创可贴治愈心灵2 分钟前
AI浪潮下C#就业前景剖析:深耕C#为主,按需选修Java与Python
java·人工智能·c#
子非鱼@Itfuture2 分钟前
端侧AI(On-Device AI / Edge AI)|边缘 AI|云端 AI 探索报告
人工智能·ai·agi·端侧ai
愚公搬代码10 分钟前
【愚公系列】《移动端AI应用开发》014-DeepSeek API开发与集成(处理多轮对话与动态请求)
人工智能·中间件·架构
真上帝的左手13 分钟前
19. 大数据- BI - AI 应用1-融合场景解析
大数据·人工智能·ai·bi
wgc2k17 分钟前
Oops Framework-6-项目中如何使用AI的思路
人工智能·游戏·cocos2d
Jump 不二23 分钟前
Memory-os 7 层记忆架构深度解析:让 Hermes Agent 真正 “记住并使用“ 知识
人工智能·语言模型·系统架构
程序猿阿伟24 分钟前
《无需额外付费的OpenClaw Agent部署指南》
人工智能
DS随心转APP27 分钟前
AI导出鸭:AI 文档排版与一键导出实战指南
人工智能·ai·chatgpt·deepseek·ai导出鸭
geneculture29 分钟前
语(暨各级各类字组)对接外来的词和句以及本土的言和语:言和语的关系及双重形式化彻底解决问题
人工智能·语言学·融智学应用场景·哲学与科学统一性·融智时代(杂志)
凯丨29 分钟前
agentmemory on NAS 完整部署文档(Tailscale + DeepSeek 压缩 + 局域网 viewer)
人工智能