昇思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)
相关推荐
~kiss~1 分钟前
How OpenAI delivers low-latency voice AI at scale - OpenAI 如何规模化实现低延迟语音 AI
人工智能
后端小肥肠2 分钟前
白嫖小云雀 API 200 秒免费额度,封装 Skill,玩转 Seedance2.0 视频
人工智能·agent
河西石头2 分钟前
听AI的血的教训!PPOCRLabel部署与PyQt5的安装避坑-百分百成功!
开发语言·人工智能·python·pyqt5安装·ppocrlabel的部署
BU摆烂会噶4 分钟前
【LangGraph】 流式处理入门
人工智能·python·langchain·人机交互
星辰徐哥6 分钟前
人工智能:计算机视觉的基础与应用
人工智能·计算机视觉·ai·预处理·特征提取
发哥来了9 分钟前
东莞AI培训课程横向对比:五家机构教学与就业质量评测
大数据·人工智能·机器学习·ai·aigc
AI医影跨模态组学15 分钟前
(综述)J Transl Med 浙江大学医学院附属第二医院等团队:放射组学在胶质母细胞瘤复发中的应用:预测、定位及与治疗相关效应鉴别的进展
人工智能·深度学习·医学·医学影像·影像组学
神工坊17 分钟前
新闻︱神工坊受邀出席无锡人工智能产业园“AI赋能研发设计”主题活动,共探算力与AI时代下的CAE范式革新
人工智能·ai·hpc·cfd·cae·求解加速·智能修模
我是发哥哈21 分钟前
东莞AI培训主流方案横向评测:5大选型维度解析
大数据·人工智能·学习·机器学习·chatgpt·ai编程
千寻girling24 分钟前
机器学习 | 感知机 | 尚硅谷学习
人工智能·学习·机器学习