昇思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 分钟前
水晶光电发布 2026 半年报:归母净利润 5.24 亿元同比增长 4.73%
人工智能·能源
浩腾数字多媒体8 分钟前
如何判断专业电子留言厂家适配条件?
大数据·人工智能·python
阿里云大数据AI技术13 分钟前
从 Common Crawl 到可训练语料:用 EMR Serverless Daft 构建清洗、模型标注与向量化管线
人工智能·spark
jay神18 分钟前
27届计算机专业深度学习选题推荐
人工智能·深度学习·yolo·机器学习·计算机视觉
stuartevil20 分钟前
AI漫剧日更制作怎么做才不累
人工智能
大模型任我行22 分钟前
NUS:全模态感知驱动AI科学家
人工智能·语言模型·自然语言处理·论文笔记
会编程的吕洞宾22 分钟前
DeepAgents In Action学习(Third)
python·学习·django
weixin_4462608524 分钟前
G‑MARK:基于知识图谱的协同自动驾驶接地多智能体推理框架
人工智能·自动驾驶·知识图谱
天天进步201526 分钟前
Pixelle-Video 源码解析 #6:AI 文案生成流程:如何根据主题自动写解说词?
人工智能
章老师说31 分钟前
壬远 AI 网关 v0.4.0 正式发布:模型定价、RMB 配额与多 Key 路由,让企业级 AI 流量治理再进一步
人工智能·ai·开源·负载均衡·ai-native