昇思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)
相关推荐
星夜夏空998 分钟前
STM32单片机学习(21) —— I2C通信
stm32·单片机·学习
无忧智库23 分钟前
基于5G-A(通感一体)技术的城市低空飞行器实时航线监控底座建设方案(WORD)
大数据·人工智能·5g
IT_陈寒39 分钟前
为什么 Java 的 Optional 让我调试到深夜?
前端·人工智能·后端
有为少年41 分钟前
深度隐式层 | 隐式函数与自动微分
人工智能·神经网络·线性代数·机器学习·矩阵
大模型真好玩1 小时前
大模型训练全流程实战指南工具篇(十三)—— 大模型评测实战(数据集评测+自动化评测)
人工智能·agent·deepseek
ShGamu1 小时前
2026上半年链条输送机厂家全流程服务与选型参考
大数据·人工智能·链条输送机
charley.layabox1 小时前
大连理工,将 LayaAir AI 游戏设计带进校园
人工智能·游戏
Raink老师1 小时前
【AI面试临阵磨枪-76】社交 AI:内容生成、审核、智能回复、多模态理解、安全治理
人工智能·安全·面试
装不满的克莱因瓶1 小时前
SpringAI Alibaba Tool工具调用机制实战-注解注册与函数调用全流程
人工智能·ai·tools·智能体·springai·tool
ZhengEnCi1 小时前
09ab-无偏置线性层是什么?
人工智能