昇思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)
相关推荐
上海潭龙东海科技有限公司18 分钟前
客运站管控之动态孪生技术方案(新增一屏管控模块
数据仓库·人工智能·算法·重构·动态规划
民乐团扒谱机38 分钟前
Agent-Skills 新手入门与实战部署指南
人工智能·ai·agent·skill
打工仔折腾 AI1 小时前
Changedetection.io 网页变动监控:Docker 部署、通知配置与公网暴露实践
运维·人工智能·后端·python·docker·容器·性能优化
深圳雨林凯AI2 小时前
商品详情长图的自动生成:版块模板、要素抽取与版式约束的实现思路
人工智能·ai
lisw052 小时前
迁移学习(Transfer Learning):概念、历史、内容、挑战与展望
人工智能·机器学习·迁移学习
炫酷的大高个2 小时前
98% 连跑 20 次只剩 67%,我算了大模型重试账
人工智能·架构·开源
可乐ea2 小时前
智能体的模型列表为什么要从三处来源合成:一次让 GPT-6.1 Sol 选不上的发布延迟
人工智能·gpt·chatgpt·智能体框架·模型列表·多源数据合成·客户端工程
freewebsys2 小时前
【原始POSTMAN】最老一个版本的postman 大家还记得吗?官方已经不支持没有升级了,下载了组件用AI直接升级了下可以使用了!
人工智能
云南新人和数采集团3 小时前
云南靠谱软件开发公司选型指南:以昆明新腾科技为例
大数据·人工智能·科技
LGL6030A3 小时前
Java学习历程31——初识Spring Boot
java·spring boot·学习