昇思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)
相关推荐
陆枫Larry9 分钟前
MCP 实践(二):让自己开发的 MCP Server 调用真实 HTTP 接口
人工智能
阿里云大数据AI技术11 分钟前
从空间圈选到区域决策:EMR Serverless StarRocks(Stella)的 GIS 架构与空间索引实践
人工智能
掘金酱1 小时前
X-Lab 年度大会线上同步活动 | 带上你的 AI 作品,来 X-Lab 年度大会聊聊 AI 新生意!
人工智能
阿里云大数据AI技术1 小时前
Hologres 长记忆服务发布知识库:好记性,也要懂业务
人工智能
AllData公司负责人1 小时前
实战二:AllData大模型与AI数据底座-数据传输平台-DBSyncer,搭建 Http 接口同步至 Doris,API 业务数据自动拉取入数仓
大数据·数据库·数据仓库·人工智能·算法·http·doris
 梦晓天明1 小时前
1.Spring学习笔记_IOC入门案例
笔记·学习·spring
掘金安东尼1 小时前
Cloudflare 推出 Search for Agents:一键给网站加上面向智能体的搜索接口(全文转译 + 评论)
人工智能·agent·ai编程
Cosolar2 小时前
需求做完了,经验归零了:Agent 真正缺的是第二条闭环
人工智能·后端·agent
武子康2 小时前
火系被选七成,就应该削弱吗?
人工智能·算法·机器学习
云起无垠2 小时前
关掉笔记本,让 AI agent 在远程服务器上继续跑完一夜:DevAgent 的 SSH 远程工作区实战
服务器·人工智能·ssh