昇思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)
相关推荐
不爱土豆唯爱马铃薯23 分钟前
MC-026 | 实战·用MonkeyCode开发论文辅助工具
人工智能
程序员cxuan27 分钟前
姚顺雨这次访谈,腾讯终于把 AI 下半场讲明白了
人工智能·后端·程序员
甲维斯1 小时前
Claude Code中文界面版更一波!又改了5000+行!
人工智能·ai编程
腾讯云开发者1 小时前
从前沿洞见到落地实践:腾讯云TVP布道澳门,燃动AI Agent新思潮
人工智能
雪隐2 小时前
个人电脑玩AI-02让5060 Ti给你打工——Whisper语音识别篇(下)
人工智能·后端
HIT_Weston2 小时前
110、【Agent】【OpenCode】todowrite 工具提示词(示例)(四)
人工智能·agent·opencode
ECT-OS-JiuHuaShan2 小时前
什么是对和错?——“有针对性定义域的逻辑值的真伪”:认识论终极追问的公理化裁决
数据库·人工智能·算法·机器学习·数学建模
坤坤藤椒牛肉面2 小时前
实习日记--基础内容学习
学习
澹锦汐2 小时前
从 0 到 1 构建 AI 创意工具:独立开发者的 LLM 应用实战
人工智能