昇思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 分钟前
从 SEO 到 GEO:AI 搜索如何理解网页内容?一次完整的可见性测试实践
大数据·人工智能·新媒体运营·内容运营
骑着蜗牛撵大象32713 分钟前
AI Agent 工程化开发实战(十四):拆开 Deep Agents 看家底——计划、子代理与文件系统内置了什么
人工智能·架构·langgraph·mcp·deepagents·子代理
lingyubb15 分钟前
适合短视频配音 操作简单的AI配音软件推荐怎么落地?从文案到成片的四步实操拆解
人工智能·音视频·状态模式
上弦外贸新观察1 小时前
ChatGPT广告服务商哪家好?分享OpenAI广告业务服务评估思路,从平台政策核实、账户支持、广告策略到运营流程逐项了解
人工智能·chatgpt
镜象科技1 小时前
大学AI心理健康解决方案服务商的危机联动设计:从宿舍楼到精神科的多方接力
人工智能
硅谷秋水1 小时前
Gemini Robotics 1.5 将 AI 智能体引入物理世界
人工智能·机器学习·语言模型·机器人
具身AGI1 小时前
物理AI 人类学习路线:三大判断怎么兑现
大数据·人工智能·学习
智购科技无人售货机工厂店1 小时前
自动售货机振动与运输可靠性测试实战:跌落、振动与包装~YH
java·大数据·服务器·人工智能·科技
米软科技1 小时前
从数据到决策:公立医院DRG成本精细化管理实践
大数据·人工智能
Simon_lca2 小时前
亚马逊CPF绿标与碳数据约束趋严:工厂端数据链正在成为新的准入条件
大数据·人工智能·经验分享·科技·制造