昇思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易山几秒前
扣子工作流实战:多节点串联打造 AI 内容自动化流水线
人工智能·自动化·coze·扣子工作流·米核ai易山
qxq_sunshine几秒前
千问(Qwen)模型Linux部署操作手册
人工智能·gpt·语言模型
刘一说几秒前
AI科技热点日报 | 2026年06月03日
人工智能·科技
papership几秒前
prompt 设计简介(AI对话技巧)
人工智能·prompt
蒟蒻的贤1 分钟前
为什么加入 ReLU 后,神经网络可以学习线性可分的特征?
人工智能·神经网络·学习
耶夫斯计1 分钟前
【xy_healthy_assistant:LLM+Mem0 +skills个性化助手开发手记】
人工智能·python·健康医疗
计算机科研狗@OUC3 分钟前
(icml26) Degradation-Aware Metric Prompting for Hyperspectral Image Restoration
人工智能·计算机视觉·图像修复
aigc201204083 分钟前
PS 怎么去水印?4 种实操方案解决 AI 生成图右下角水印问题
人工智能
competes3 分钟前
数据查询方式最左匹配原则
java·大数据·前端·人工智能·windows
踏着七彩祥云的小丑5 分钟前
AI学习——Docker 打包与部署
人工智能·学习·docker·ai