昇思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)
相关推荐
网络精创大傻15 分钟前
在 AWS 上启动您的 AI 代理:Bedrock、Lambda 和 API 网关
人工智能·云计算·aws
说私域21 分钟前
链动2+1模式、AI智能名片与S2B2C商城小程序:破解直播电商流量转化困局的创新路径
人工智能·小程序
武陵悭臾37 分钟前
Python应用开发学习:Pygame中实现切换开关及鼠标拖动连续填充功能
python·学习·程序人生·个人开发·pygame
inside80239 分钟前
11月10日学习总结--numpy的基本操作
学习·numpy
想暴富,学技术41 分钟前
AI提示词学习基础(一)
人工智能·学习
萤丰信息1 小时前
智慧园区:数字中国的“微缩实验室”如何重构城市未来
大数据·人工智能·科技·安全·重构·智慧园区
菠菠萝宝1 小时前
【AI应用探索】-7- LLaMA-Factory微调模型
人工智能·深度学习·大模型·llm·nlp·attention·llama
大模型真好玩2 小时前
低代码Agent开发框架使用指南(七)—Coze 数据库详解
人工智能·agent·coze
唐兴通个人2 小时前
金融保险银行营销AI数字化转型培训讲师培训老师唐兴通讲金融银保团队险年金险市场销售
大数据·人工智能
视界先声2 小时前
AIDAv2:重新定义DeFi的AI驱动金融基础设施
人工智能·金融