昇思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)
相关推荐
说私域几秒前
基于AI智能名片链动2+1模式S2B2C商城小程序的流量运营策略研究
人工智能·微信·小程序·产品运营·流量运营
小六花s几秒前
渗透测试前四天PHP文件包含笔记
android·学习·渗透测试
山后太阳1 分钟前
如何评估TensorRT加速效果?
人工智能
秋深枫叶红3 分钟前
嵌入式第四十七篇——ARM汇编
汇编·arm开发·学习
rannn_1115 分钟前
【Javaweb学习|Day7】事务管理、文件上传
后端·学习·javaweb
2501_941333105 分钟前
YOLO11-BiFPN实现:小麦杂质检测与分类系统详解_1
人工智能·分类·数据挖掘
Mixtral6 分钟前
2026年面试记录转写工具深度测评:3款工具准确率与效率对比
人工智能·面试·职场和发展·语音识别·语音转文字
STLearner6 分钟前
AAAI 2026 | 时间序列(Time Series) 论文总结[下] (分类,异常检测,基础模型,表示学习,生成)
大数据·论文阅读·人工智能·python·深度学习·机器学习·数据挖掘
陈天伟教授7 分钟前
人工智能应用-机器视觉:绘画大师 02.深度神经网络中的内容与风格
人工智能·神经网络·dnn
l1t10 分钟前
DeepSeek总结的SQLite 数据库的版本更新历史摘要
数据库·人工智能·sqlite