昇思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)
相关推荐
HuggingFace4 分钟前
经同意的语音克隆
人工智能
崇山峻岭之间4 分钟前
Matlab学习记录05
开发语言·学习·matlab
nnsix8 分钟前
Unity 新InputSystem 学习笔记
笔记·学习
智算菩萨9 分钟前
实战:用 Python + 传统NLP 自动总结长文章
开发语言·人工智能·python
whitelbwwww15 分钟前
Pytorch--张量表示实际数据
人工智能·pytorch·python
roman_日积跬步-终至千里17 分钟前
【人工智能导论】05-学习-机器学习基础:从数据到智能决策
人工智能·学习·机器学习
nice_lcj52017 分钟前
【AI进阶】拒绝“人工智障”:掌握提示词工程(Prompt Engineering)的6大核心技巧
人工智能·prompt
万俟淋曦19 分钟前
【论文速递】2025年第40周(Sep-28-Oct-04)(Robotics/Embodied AI/LLM)
人工智能·深度学习·ai·机器人·大模型·论文·具身智能
jiayong2320 分钟前
Spring AI Alibaba 深度解析(一):框架概述与核心功能
java·人工智能·spring
盐焗西兰花34 分钟前
鸿蒙学习实战之路-数据持久化键值型数据库KV-Store全攻略
数据库·学习·harmonyos