昇思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)
相关推荐
这是谁的博客?几秒前
AI 安全入门:从环境搭建到风险防护实战
人工智能·安全
MartinYeung5几秒前
[论文学习]隐私保护联邦学习于入侵侦测系统之调查研究
学习
踩着两条虫1 分钟前
VTJ.PRO v2.4.0 多人协作与 AI 批量识图实战评测
vue.js·人工智能·低代码·figma
AI设计小站2 分钟前
GPT Image2国内可用方案实测:创客贴AI生成+分层编辑能力解析
人工智能·gpt·php
Keller-Zhou2 分钟前
AI门店巡检系统架构设计:从数据采集到OpenAPI交付的全链路
人工智能
zhangfeng11335 分钟前
ai算力卡,Tenstorrent 公司Jim Keller 和 Ljubisa Bajic的故事,taals公司
人工智能·语言模型·架构·transformer·gpu算力
AI品信智慧数智人6 分钟前
数字人穿梭虚实幻境,智慧随心畅游,解锁AI元宇宙新生态✨
人工智能
Studying 开龙wu8 分钟前
OpenCV 报错 Assertion failed (s >= 0) in cv::setSize 的完整解决方案
人工智能·opencv·计算机视觉
网易Y3编辑器8 分钟前
AI全流程创游丨网易Y3编辑器Full Mode与Patch Mode双模式架构深度解析
人工智能·架构·编辑器
Data-Miner9 分钟前
休闲食品数据分析平台建设方案,70页ppt全解析
大数据·人工智能·数据分析