昇思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)
相关推荐
ZGi.ai6 小时前
ZGI工作流引擎:把AI应用的执行过程从黑盒变成流程图
人工智能·低代码·流程图·ai应用·ai工作流·zgi
物联网软硬件开发-轨物科技6 小时前
【轨物方案】从传感器到AI诊断:箱变智能化技术栈四层架构精讲
人工智能·架构
星期一研究室6 小时前
创作分发这件事,来看看Codex是怎么做的
人工智能·黑客·微信
爱学习的章鱼哥6 小时前
一些AI实操的经验与感想
人工智能·ai·ai作画·ai编程·ai写作·ai经验
言己lxz6 小时前
阿里云官方技术文档已全面上线 llms.txt —— AI Agent 一站式消费 300+ 款云产品文档
人工智能·阿里云·llmstxt
happyness446 小时前
如何让AI重构旧代码
人工智能·重构
dream_home84076 小时前
图像算法模型NPU适配与算法服务实战指南
人工智能·python·算法·npu 图像服务
AI服务老曹6 小时前
Docker部署AI视频分析平台参数配置说明与调优指南
人工智能·docker·音视频
AI大模型-小雄7 小时前
用 ChatGPT 整理工作资料,正确的提问顺序是什么?附完整操作模板
人工智能·chatgpt·提示词·效率工具·codex·办公技巧·ai办公
AIGS0017 小时前
跨越语义鸿沟:企业本体语义平台的构建与落地
java·人工智能·python·机器学习·人工智能ai大模型应用