昇思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)
相关推荐
装不满的克莱因瓶几秒前
RLHF中的PPO算法——大语言模型对齐优化的核心引擎
人工智能·python·深度学习·算法·机器学习·语言模型·自然语言处理
程序员柒叔4 分钟前
Hermes Agent 一周动态-2026-W24
人工智能·github·agent·openclaw·hermes
fox_lht11 分钟前
14.6.将错误重定向到标准错误
开发语言·后端·学习·rust
KKKlucifer20 分钟前
数据分类分级产品排名解析:场景定制、规则联动、增量更新成核心能力
大数据·数据库·人工智能
王小王-12320 分钟前
深度学习赋能:基于机器学习的恶意 URL 检测系统
人工智能·机器学习·恶意网址检测·恶意url检测·异常网址检测·机器学习异常网址检测
fanged20 分钟前
Linux内核学习17--SPI子系统(TODO)
学习
2601_9557674226 分钟前
2026年iPhone17贴膜怎么选:偏振片缺失与磁控溅射AR镀膜技术解析及SGS认证观复盾方案
人工智能·智能手机·ar·护眼钢化膜·磁控溅射
金融RPA机器人丨实在智能26 分钟前
数据库运维Agent比价指南:国产自研产品适配国产数据库兼容性更好吗?
运维·数据库·人工智能·ai
2601_9557674228 分钟前
iPhone17偏振片缺失怎么补救:圆偏振光与磁控溅射AR技术解析及悟赫德观复盾方案
人工智能·ar·护眼钢化膜·圆偏振光·#观复盾护景贴
Black蜡笔小新32 分钟前
自动化AI算法训练服务器DLTM一体化训推平台构建企业专属AI能力中台
人工智能·算法·自动化