昇思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)
相关推荐
大侠区块链2 分钟前
Hermes Agent 安全架构深度拆解:47 条危险命令规则 + 半个月新增的 14 条
人工智能·ai·claude·hermes
郑寿昌8 分钟前
低空经济:万亿级新赛道崛起
人工智能
anew___11 分钟前
【深度学习数学基础】从线性代数到信息论:核心概念一文速通
人工智能·深度学习·线性代数
宁雨桥17 分钟前
前端与AI结合实战分享
前端·人工智能
ROBOTGEEKER17 分钟前
越疆CR全系列工业协作臂|从3kg轻载到30kg重载,覆盖重复、高精、高危全制造场景
人工智能·机器人·自动化·制造
码农小河6618 分钟前
AI 一键生成 HTML/CSS/JS 静态网站【压缩包返回可直接提交】
css·人工智能·html
南湖渔歌20 分钟前
【成功实践版】workbuddy_把多张图片转成完整Markdown笔记
人工智能·笔记·workbuddy
byte轻骑兵23 分钟前
【HID】规范精讲[9]: SDP协议深度解析与实战应用
人工智能·人机交互·键盘·鼠标·hid
艾派森24 分钟前
深度学习实战-基于EfficientNetB5的家禽鸡病图像分类识别模型
人工智能·python·深度学习·神经网络·分类
研究点啥好呢28 分钟前
快手多模态算法工程师面试题精选:10道高频考题+答案解析
java·开发语言·人工智能·ai·面试·笔试