昇思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)
相关推荐
min181123456几秒前
AI辅助诊断:精准医疗的新范式
人工智能
阿里云云原生4 分钟前
阿里云全新发布的 UModel 是什么
人工智能·阿里云·云计算·可观测·umodel
HarmonLTS5 分钟前
Python人工智能深度开发:技术体系、核心实践与工程化落地
开发语言·人工智能·python·算法
Jason_zhao_MR10 分钟前
YOLO5目标检测方案-基于米尔RK3576开发板
linux·人工智能·嵌入式硬件·目标检测·计算机视觉·目标跟踪·嵌入式
FL1717131410 分钟前
Koopman 算子及其PyKoopman实现
人工智能
沉默-_-12 分钟前
微信小程序页面配置详解
学习·微信小程序·apache·微信开发者工具
北京云帆互联科技17 分钟前
云帆学习考试系统更新说明v8.8.0
学习·考试系统·高校考试系统
Quintus五等升25 分钟前
深度学习③|分类任务—AlexNet
人工智能·经验分享·深度学习·神经网络·学习·机器学习·cnn
zl_vslam33 分钟前
SLAM中的非线性优-3D图优化之绝对位姿SE3约束右扰动(十七)
人工智能·算法·计算机视觉·3d
光羽隹衡37 分钟前
计算机视觉——Opencv(基础操作一)
人工智能·opencv·计算机视觉