昇思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)
相关推荐
kkai人工智能41 分钟前
AI写作:从“废话”到“爆款”
开发语言·人工智能·ai·ai写作
炽烈小老头5 小时前
【每天学习一点算法 2025/12/19】二叉树的层序遍历
数据结构·学习·算法
xian_wwq5 小时前
【学习笔记】数据血缘
笔记·学习·数据血缘
づ安眠丶乐灬5 小时前
计算机视觉中的多视图几何 - 1
人工智能·vscode·计算机视觉
2503_928411565 小时前
项目中的一些问题(补充)
人工智能·python·tensorflow
MarkHD5 小时前
智能体在车联网中的应用 第1天 车联网完全导论:从核心定义到架构全景,构建你的知识坐标系
人工智能·架构
中科米堆5 小时前
塑料制品企业部署自动化三维扫描仪设备,解决注塑件变形问题-中科米堆CASAIM
人工智能
星图云6 小时前
从数据累积到精准解析:AI解译打造遥感数据高效利用新范式
人工智能·卫星遥感
map_vis_3d6 小时前
JSAPIThree LODModel 性能优化学习笔记:细节层次模型加载
笔记·学习·3d