昇思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)
相关推荐
mahtengdbb1几秒前
三阶段压缩瓶颈改进YOLOv26特征提取效率与通道自适应能力提升
人工智能·yolo·目标跟踪
你数过天上的星星吗2 分钟前
Python学习笔记一(标识符、关键字、变量、数据类型、关系运算)
笔记·python·学习
嵌入式小企鹅2 分钟前
CPU需求变化、RISC-V安全方案、DeepSeek V4适配、太空算力动态
人工智能·驱动开发·华为·开源·算力·risc-v
HyperAI超神经4 分钟前
利用堆叠集成学习,英国研究团队实现251颗盾牌座δ型星星震学指数高精度预测
人工智能·机器学习·集成学习
AI刀刀5 分钟前
手机deepseek怎么导出pdf
人工智能·ai·pdf·豆包·deepseek·ds随心转
专注&突破6 分钟前
用AI学习graphify
人工智能·学习
wayz116 分钟前
Day 16 编程实战:PCA主成分分析与技术指标降维
人工智能·算法·机器学习
超梦dasgg12 分钟前
SpringAi学习
人工智能·学习·ai编程
05大叔13 分钟前
贝叶斯,支持向量机,深度学习
人工智能·分类·数据挖掘
小羽网安2 小时前
从零开始学习 sql 注入,常见的 sql 注入解析
数据库·sql·学习