昇思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)
相关推荐
youngerwang1 分钟前
【字节跳动 AI 原生 IDE TRAE 】
ide·人工智能·trae
youngerwang9 分钟前
AI 编程环境与主流 AI IDE 对比分析报告
ide·人工智能
猿小猴子9 分钟前
主流 AI IDE 之一的 Google Antigravity IDE 介绍
ide·人工智能·google·antigravity
Teacher.chenchong25 分钟前
GEE云端林业遥感:贯通森林分类、森林砍伐与退化监测、火灾评估、森林扰动监测、森林关键生理参数(树高/生物量/碳储量)反演等
人工智能·分类·数据挖掘
cmcm!1 小时前
学习笔记1
数据库·笔记·学习
Hcoco_me2 小时前
YOLO目标检测学习路线图
学习·yolo·目标检测
2501_941147422 小时前
人工智能赋能智慧城市互联网应用:智能交通、能源与公共管理优化实践探索》
人工智能
咚咚王者2 小时前
人工智能之数据分析 numpy:第十五章 项目实践
人工智能·数据分析·numpy
水月wwww3 小时前
深度学习——神经网络
人工智能·深度学习·神经网络
司铭鸿3 小时前
祖先关系的数学重构:从家谱到算法的思维跃迁
开发语言·数据结构·人工智能·算法·重构·c#·哈希算法