昇思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)
相关推荐
阿蒙Amon1 分钟前
TypeScript学习-第11章:配置文件(tsconfig.json)
学习·typescript·json
babe小鑫2 分钟前
中专学历转内容运营分析的学习路径
学习·产品运营·内容运营
风栖柳白杨7 分钟前
【语音识别】Qwen3-ASR原理及部署
人工智能·python·语音识别·xcode·audiolm
Wang2012201312 分钟前
2026流行的 AI Agent开发框架 (构建“智能体”)
人工智能
张人玉13 分钟前
VisionPro Blob、条码识别、OCR 结构化速记版
人工智能·算法·机器学习·vsionpro
Elastic 中国社区官方博客20 分钟前
Elasticsearch:使用 Elastic Workflows 构建自动化
大数据·数据库·人工智能·elasticsearch·搜索引擎·自动化·全文检索
●VON20 分钟前
React Native for OpenHarmony:贪吃蛇游戏的开发与跨平台适配实践
学习·react native·react.js·游戏·openharmony
跨境卫士-小汪24 分钟前
选品更稳的新打法:用“用户决策阻力”挑品——阻力越大,越有机会做出溢价
大数据·人工智能·产品运营·跨境电商·内容营销·跨境
空中楼阁,梦幻泡影44 分钟前
主流4 大模型(GPT、LLaMA、DeepSeek、QWE)的训练与推理算力估算实例详细数据
人工智能·gpt·llama
Dev7z1 小时前
基于改进YOLOv5n与OpenVINO加速的课堂手机检测系统设计与实现
人工智能·yolo·openvino·手机检测·课堂手机检测