昇思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)
相关推荐
M78佐菲17 小时前
Linux学习笔记:进程
linux·笔记·学习·算法
ZISHU_98717 小时前
用 TLabel 给 SynTouch BioTac 数据做语义标注:从原始信号到结构化标注
开发语言·人工智能·python·数据·机器人触觉
hh95017 小时前
gent Plan x DeepSeek Harness:多 Agent 协作场景下的中央编排实践
人工智能·wpf·adg·火山引擎·agent plan·adg成都社区
cczixun18 小时前
2026 智能体平台落地实战:从 POC 验证到生产上线,避开选型与交付陷阱
人工智能·落地·选型·智能体·2026
IT_陈寒18 小时前
Redis内存警告竟是因为这个不起眼的配置项
前端·人工智能·后端
CHY_12818 小时前
VSO.ai 系列(二):AI验证回归优化实战
人工智能·synopsys·vso.ai·验证加速
2401_8900956118 小时前
武汉人工智能应用软件开发实施异常怎么排查?模拟环境与验证步骤
人工智能·案例复盘·武汉自动意志科技·智钳claw·企业ai智能体·ai漫剧生成
泰迪智能科技0118 小时前
三种人社职业技能等级证书报考指南对比:人工智能训练师 / 计算机程序设计员 / 服务机器人应用技术员
人工智能·百度·机器人
森山冶仁18 小时前
AI 原生数据治理技术栈:大模型 + 向量库 + 知识图谱怎么选
人工智能·知识图谱·数据治理
记忆张量MemTensor18 小时前
产品更新|MemOS 现已支持 DeepSeek Harness 长期记忆接入
人工智能·typescript·开源·agent