昇思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)
相关推荐
大布布将军1 分钟前
⚡后端安全基石:JWT 原理与身份验证实战
前端·javascript·学习·程序人生·安全·node.js·aigc
ASD123asfadxv3 分钟前
基于改进Faster R-CNN的鸭蛋质量检测与分类系统_x101-32x8d_fpn_ms-3x_coco模型详解
人工智能·分类·cnn
纪佰伦6 分钟前
类人脑的另一种计算 ——大语言模型large-lauguage-model ——模型怎么找出这种规律的
人工智能·语言模型·自然语言处理
天竺鼠不该去劝架8 分钟前
2026 金融智能体选型逻辑:懂金融、重合规、能落地
大数据·数据库·人工智能
让学习成为一种生活方式8 分钟前
如何根据过滤的pep序列进一步过滤gff3文件--python015
开发语言·人工智能·python
阿正的梦工坊11 分钟前
WebArena:一个真实的网页环境,用于构建更强大的自主智能体
人工智能·深度学习·机器学习·大模型·llm
im_AMBER11 分钟前
Leetcode 90 最佳观光组合
数据结构·c++·笔记·学习·算法·leetcode
大模型实验室Lab4AI12 分钟前
小模型驱动大智能:NVIDIA 新研究实现 2.5 倍效率提升,成本直降 70%
人工智能
Hcoco_me14 分钟前
Word2Vec 核心知识点速记版
人工智能·自然语言处理·word2vec
qijiabao411317 分钟前
深度学习|可变形卷积DCNv3编译安装
人工智能·python·深度学习·机器学习·cuda