昇思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)
相关推荐
酒尘&3 分钟前
Hook学习-上篇
前端·学习·react.js·前端框架·react
Dcs13 分钟前
你的 Prompt 都该重写?
人工智能·ai编程
木卫二号Coding17 分钟前
第五十三篇-Ollama+V100+Qwen3:4B-性能
人工智能
飞哥数智坊20 分钟前
AI 不只是聊天:聊聊我最近在做的新方向
人工智能
qq_381454991 小时前
Python学习技巧
开发语言·python·学习
学生高德1 小时前
小模型结合大模型的加速方法关键笔记
人工智能·深度学习·机器学习
im_AMBER1 小时前
算法笔记 18 二分查找
数据结构·笔记·学习·算法
蓝耘智算1 小时前
GPU算力租赁与算力云平台选型指南:从需求匹配到成本优化的实战思路
大数据·人工智能·ai·gpu算力·蓝耘
liliangcsdn1 小时前
如何用bootstrap模拟估计pass@k
大数据·人工智能·bootstrap
van久1 小时前
.Net Core 学习: Razor Pages -- EF Core简介
学习·.netcore