昇思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)
相关推荐
('-')16 分钟前
《从根上理解MySQL是怎样运行的》第二十二章学习笔记
笔记·学习·mysql
搞科研的小刘选手17 分钟前
【高录用|快检索】第二届图像处理、多媒体技术与机器学习国际学术会议(IPMML 2025)
人工智能·机器学习·多媒体·学术会议
秋邱28 分钟前
AI + 社区服务:智慧老年康养助手(轻量化落地方案)
人工智能·python·重构·ar·推荐算法·agi
leijiwen34 分钟前
Bsin X BDCM:从流量驱动到价值激励驱动的智能增长引擎
大数据·人工智能·web3
人工智能训练34 分钟前
Linux 系统核心快捷键表(可打印版)
linux·运维·服务器·人工智能·ubuntu·容器·openeuler
得贤招聘官37 分钟前
AI 重构招聘:从效率到精准决策
人工智能·重构
高锰酸钾_1 小时前
机器学习基础 | KNN(K-近邻)
人工智能·机器学习
人邮异步社区1 小时前
想要系统地学习扩散模型,应该怎么去做?
人工智能·学习·程序员·扩散模型
光头程序员1 小时前
vue学习笔记
vue.js·笔记·学习
1***y1781 小时前
区块链跨链桥、 跨链桥到底在解决什么问题?
大数据·人工智能·区块链