昇思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)
相关推荐
yzx9910131 分钟前
重构价值:2026年AI就业形势的深度剖析
人工智能·重构
I Promise3411 分钟前
BEV视角智驾方案业务需求分类与主流技术全解
人工智能·深度学习·计算机视觉
多恩Stone12 分钟前
【3D-AICG 系列-12】Trellis 2 的 Shape VAE 的设计细节 Sparse Residual Autoencoding Layer
人工智能·python·算法·3d·aigc
qq_242188633216 分钟前
金融AI反欺诈系统构建指南
人工智能·笔记·金融·课程设计
新加坡内哥谈技术20 分钟前
Claude C 编译器:它揭示了软件未来的什么
人工智能
予枫的编程笔记24 分钟前
【Kafka进阶篇】Kafka消息重复消费?Exactly-Once语义落地指南,PID+事务消息吃透
人工智能·kafka·消息队列·exactly-once·分布式消息·kafka幂等性·kafka事务消息
四谎真好看29 分钟前
SSM学习笔记(SpringBoot篇)
spring boot·笔记·学习·学习笔记·ssm
Loo国昌40 分钟前
【AI应用开发实战】09_Prompt工程与模板管理:构建可演进的LLM交互层
大数据·人工智能·后端·python·自然语言处理·prompt
学编程的闹钟42 分钟前
E语言子程序调用详解
学习
新缸中之脑1 小时前
Wellows:生成式AI搜索优化平台
人工智能·chatgpt