昇思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)
相关推荐
夕除1 小时前
redis--010
笔记·分布式·学习
DolphinScheduler社区1 小时前
把 Apache DolphinScheduler 变成 Agent 的“手和脚”:从调度平台到自然语言数据入口
人工智能·开源·apache·agent·技术分享·海豚调度·大数据工作流调度
andrsted1 小时前
在线刷小程序推荐
学习·微信小程序·小程序·学习方法
weixin_446260851 小时前
基于熵的选择性智能体引导:从非完美视觉语言模型教师学习自主策略
人工智能·学习·语言模型
夜雪一千2 小时前
如何写一个数据分析 Skill
人工智能·数据挖掘·数据分析
桃西西呀2 小时前
RAG 接个向量库就完事?从切块到重排的 7 步流水线,我替你踩了 8 个深坑
人工智能·llm·ai编程
幸运小圣2 小时前
JavaScript 关键字与保留字学习笔记详细讲解,一篇get ✅【JavaScript查缺补漏】
javascript·笔记·学习
YOLO数据集集合2 小时前
无人机屋顶与地物分割数据集 | 屋顶分割 航拍识别 城市规划 材质分类 实例分割9036期
人工智能·分类·无人机·材质·中国城市建筑·建筑识别
zhangfeng11332 小时前
CodeBuddy‑CLI 默认授权(权限模式)命令行参数
人工智能·ai编程
道可云2 小时前
工赋·青听 | 从AI素养到专属智能体,共探化工行业数字化转型新路径
人工智能