昇思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 分钟前
C语言学习笔记202606008- 三角形判断(3种方法)
c语言·笔记·学习
XGeFei9 分钟前
【Fastapi学习笔记(6)】—— Fastapi文件上传、请求头自动转换
笔记·学习·fastapi
AIGS00112 分钟前
探索向量空间JBoltAI:工业企业数智化升级的基础设施
java·人工智能·人工智能ai大模型应用
qq_5278878713 分钟前
机器学习训练中Epoch、Batch、Bath_size、Data_size的区别
人工智能·机器学习·batch
林间码客14 分钟前
《人工智能概论》实验6 知识点复习提纲
人工智能
林间码客18 分钟前
《人工智能概论》实验3 知识点复习提纲
人工智能
科技圈快迅19 分钟前
商业旅拍后期修图软件实测:像素蛋糕功能与应用分析
人工智能
一口吃俩胖子22 分钟前
【脉宽调制DCDC功率变换学习笔记024】频域性能
笔记·学习
吃着火锅x唱着歌24 分钟前
深度探索C++对象模型 学习笔记 第五章 构造、解构、拷贝语意学(2)
c++·笔记·学习
无忧智库31 分钟前
某矿山井下人员精准定位与AI行为安全识别管控系统建设方案(WORD)
人工智能·安全