昇思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 分钟前
AI+有限元:复合材料研发的“时间魔法”,从10年到3周的范式革命
人工智能·机器学习·材料工程·复合材料
人工智能AI技术5 分钟前
GitHub Copilot 2026新功能实操:C++跨文件上下文感知开发,效率翻倍技巧
c++·人工智能
航Hang*14 分钟前
Photoshop 图形与图像处理技术——第9章:实践训练1——绘制禁烟标志和奥运五环
图像处理·笔记·学习·ui·photoshop
国冶机电安装17 分钟前
一道看不见的防线:生物安全洁净工程如何守住风险底线
人工智能
轻竹办公PPT17 分钟前
2026 年 AI 办公趋势:AI 生成 PPT 工具谁在领先
人工智能·python
Coder_Boy_19 分钟前
基于SpringAI的在线考试系统-核心业务流程图(续)
java·大数据·人工智能·spring boot·流程图
人工智能培训19 分钟前
如何大幅降低大模型的训练和推理成本?
人工智能·深度学习·大模型·知识图谱·强化学习·智能体搭建·大模型工程师
人工智能AI技术19 分钟前
类脑智能核心算法拆解:从统计智能到类脑智能的模型改造实战
人工智能
之之为知知24 分钟前
NLP进化史:一场「打补丁」的技术接力赛
人工智能·深度学习·机器学习·自然语言处理·大模型
Francek Chen25 分钟前
【自然语言处理】初探自然语言处理
人工智能·自然语言处理·nlp·easyui