昇思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)
相关推荐
Jonathan Star2 分钟前
Ant Design (antd) Form 组件中必填项的星号(*)从标签左侧移到右侧
人工智能·python·tensorflow
挂科边缘19 分钟前
YOLOv12环境配置,手把手教你使用YOLOv12训练自己的数据集和推理(附YOLOv12网络结构图),全文最详细教程
人工智能·深度学习·yolo·目标检测·计算机视觉·yolov12
deep_drink22 分钟前
【论文精读(三)】PointMLP:大道至简,无需卷积与注意力的纯MLP点云网络 (ICLR 2022)
人工智能·pytorch·python·深度学习·3d·point cloud
风流倜傥唐伯虎39 分钟前
N卡深度学习环境配置
人工智能·深度学习·cuda
winfreedoms1 小时前
ROS2语音&ai与控制——黑马程序员ROS2课程上课笔记(6)
人工智能·笔记
呆萌很1 小时前
深入浅出FPN:目标检测中的特征金字塔网络
人工智能
OEC小胖胖1 小时前
DeepSeek导出文档
人工智能·效率工具·知识管理·ai工作流·deepseek
我命由我123452 小时前
Photoshop - Photoshop 工具栏(67)修补工具
学习·ui·职场和发展·求职招聘·职场发展·学习方法·photoshop
得一录2 小时前
蒸汽、钢铁与无限心智(Steam, Steel, and Infinite Minds)全文
人工智能·aigc
大模型任我行2 小时前
英伟达:物理感知的多模态评判模型
人工智能·语言模型·自然语言处理·论文笔记