昇思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)
相关推荐
乌鲁木齐德润恒圣信息科技有限公司8 分钟前
GEO生成式引擎优化:技术原理与43引擎自动测试系统
大数据·前端·人工智能
SelectDB1 小时前
SelectDB 和阿里云 SelectDB 有什么区别?公司关系、产品版本与部署方式详解
人工智能
SelectDB1 小时前
什么是 AI Agent 数据库?SelectDB 如何构建 Agent Native 实时分析基座
人工智能
严肃小白兔1 小时前
第 14/24 期:正则化方法及其对神经网络的影响
人工智能·神经网络·机器学习
江润舟2 小时前
万物|炼器:从零手搓工业级旋转目标检测网络·卷7 —— OBB检测头与DFL解码(三)
人工智能·python·深度学习
峯5802 小时前
清华博士90分钟AI与神经网络-内容梳理
人工智能
AI创界者2 小时前
Python 进阶实战(五十六):KV Cache 驱逐闭环反馈与 Prefill / Decode 分离协同调度
人工智能·python·aigc
乌鲁木齐德润恒圣信息科技有限公司2 小时前
43引擎自动测试系统:GEO工程化怎么按月迭代
人工智能
Yeats_Liao2 小时前
SoduWork 实战:1,210 条物联网报警的标准化与分诊
网络·人工智能·物联网·ai
Dawson Zhu2 小时前
【模型量化⑨】量化选型决策树:如何为你的模型选择最优量化方案
人工智能·语言模型·架构·aigc·agi