昇思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)
相关推荐
宅小年7 分钟前
DeepSeek Harness 的插件,到底该怎么用?
人工智能
仙魁XAN14 分钟前
【WorkBuddy · 三件套:技能/专家/技能】第 17 章 · 配置 MCP 服务器
人工智能·workbuddy·workbuddy三件套·mcp 配置与使用
无忧.芙桃17 分钟前
AI 生产力工具实践(四):豆包如何成为日常学习与写作助手
人工智能
IT_陈寒24 分钟前
Python的GIL把我坑惨了,多线程跑得比单线程还慢
前端·人工智能·后端
L@ncor28 分钟前
第二章可能出现的问题
人工智能·python
XGeFei31 分钟前
【Skills:SQL Assistant】
人工智能·langchain
子非鱼eva39 分钟前
昇腾开源仓Issue分析解答-mindspore精选(一)
人工智能·ai
SL_staff39 分钟前
ERP排程总在纸上谈兵?JVS-APS如何用真实产能约束打通计划与执行闭环
java·人工智能·开源
jsl_jsl_jsl1 小时前
《Tauri 桌面端的 Vue 3 前端:瘦客户端 + SSE 流式消费的实现细节》
人工智能
七牛云行业应用1 小时前
GPT-6 Sol突发曝光?将于本周发布,从 API 线索、三弹实测到 OpenAI 的 RSI 竞速
人工智能·ai编程