昇思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)
相关推荐
音视频牛哥1 分钟前
从“十五五”规划看中国视频基础设施的下一个五年:SmartMediaKit 的战略跃迁与时代机遇
人工智能·音视频·大牛直播sdk·十五五规划具身智能·十五五规划音视频·低空经济低延迟音视频方案·具身智能rtsp rtmp
ICscholar3 分钟前
生物突触功能总结
人工智能
深度学习lover15 分钟前
<项目代码>yolo螺丝螺母识别<目标检测>
人工智能·python·深度学习·yolo·目标检测·计算机视觉·螺丝螺母识别
老邓计算机毕设17 分钟前
SSM基于的少儿编程学习系统2lsiy(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
学习·ssm 框架·少儿编程学习系统·功能模块设计
3DVisionary24 分钟前
数字图像相关(DIC)技术:汽车板料成形极限(FLC)的精确测量与分析
人工智能·汽车·数字图像相关·材料力学性能·汽车板料成形·成形极限曲线·应变测量
阿民不加班31 分钟前
【React】打卡笔记,入门学习01:点击事件
笔记·学习·react.js
newxtc32 分钟前
【重庆政务服务网-注册_登录安全分析报告】
人工智能·selenium·测试工具·安全·政务
勘察加熊人1 小时前
python+ai智能根据doc教案文档生成ppt
人工智能·python·powerpoint
小苏兮1 小时前
【数据结构】二叉搜索树
开发语言·数据结构·c++·学习·1024程序员节
hrrrrb1 小时前
【机器学习】监督学习
人工智能·学习·机器学习