昇思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)
相关推荐
reddingtons8 分钟前
Magnific AI:拒绝“马赛克”?AI 幻觉重绘流,拯救 1024px 废片
图像处理·人工智能·设计模式·新媒体运营·aigc·设计师·教育电商
JXL18609 分钟前
Convolutional Neural Networks
人工智能·深度学习·机器学习
学编程的闹钟10 分钟前
PHP编程高手的信息检索与文档查阅秘籍
学习
政安晨16 分钟前
政安晨【人工智能项目随笔】Model Context Protocol(MCP)开发与资源完整指南
人工智能·mcp·模型上下文协议·mcp协议·mcp服务·ai模型上下文通信·mcp资源
GEO-optimize25 分钟前
2026北京GEO服务商评审指南:核心实力与适配指南
大数据·人工智能·机器学习·geo
cipher30 分钟前
Claude-Mem 自定义API支持:突破速率限制的解决方案
人工智能·ai编程·claude
im_AMBER36 分钟前
Leetcode 119 二叉树展开为链表 | 路径总和
数据结构·学习·算法·leetcode·二叉树
晓131337 分钟前
第六章 【若依框架:AI】AI若依框架实战项目
人工智能·若依
EriccoShaanxi38 分钟前
单轴MEMS陀螺仪:精准导航与稳定的核心
人工智能·机器人·无人机
Eloudy39 分钟前
SuiteSparse 的 README
人工智能·算法·机器学习·hpc