昇思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)
相关推荐
Zero_to_zero123411 分钟前
Claude code系列(一):claude安装、入门及基础操作指令
人工智能·python
szcsun511 分钟前
机器学习(二)-线性回归实战
人工智能·机器学习·线性回归
普通网友14 分钟前
Android16 adb投屏工具Scrcpy介绍。
人工智能
搬砖者(视觉算法工程师)16 分钟前
语义分割:基于 TensorFlow 对 FCN 与迁移学习的探究
人工智能
cookqq25 分钟前
基于Spring AI+本地大模型+MongoDB实现私密化与记忆能力-企业级免费大模型应用
人工智能·mongodb·spring
了一梨27 分钟前
SQLite3学习笔记6:UPDATE(改)+ DELETE(删)数据(C API)
笔记·学习·sqlite
云卓SKYDROID31 分钟前
无人机飞行模式详解
人工智能·无人机·高科技·云卓科技·技术解析、
数字游民95271 小时前
小程序上新,猜对了么更新110组素材
人工智能·ai·小程序·ai绘画·自媒体·数字游民9527
飘若随风1 小时前
JS学习系列-01-什么是JS
开发语言·javascript·学习
泰迪智能科技1 小时前
分享|联合编写教材入选第二批“十四五”职业教育国家规划教材名单
大数据·人工智能