昇思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服务老曹2 小时前
车牌识别算法接入AI视频分析平台的流程和误报优化
人工智能·算法·音视频
GrowthRadar2 小时前
CNC柔性自动化工程验收标准:专业协作机器人集成商的四大核心技术能力
人工智能·机器人·自动化
a1117762 小时前
MuJoCo 机械臂仿真快速入门指南+文档
人工智能·开源
微三云生态系统架构师-彭丹2 小时前
排队免单风控系统设计:四层防套利机制与异常交易检测
人工智能
似水流年QC2 小时前
什么是 Skill?深入理解 AI Agent Skill 的工作原理与应用实践
人工智能·agent·skill
Zguigo2 小时前
【DL】LSTM|Cell State|三个门
人工智能·rnn·lstm
LadiesAndGentlemen2 小时前
GeoX 论文解读:不用人工标注,如何训练会空间推理的遥感大模型
人工智能·深度学习·机器学习
水管在开花.2 小时前
Agent范式与LangGraph④-零基础保姆级教程
人工智能·agent·rag
水如烟2 小时前
孤能子视角:EIS看宇宙创生寂灭假说——人类宇宙学假说的操作描述重显影
人工智能