昇思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)
相关推荐
Juchecar10 分钟前
解析视觉:视觉识别的七层模型
人工智能
Juchecar1 小时前
解析视觉:大脑如何“辨别”美丑?
人工智能
老蒋新思维1 小时前
紧跟郑滢轩,以 “学习力 +” 驱动 AI 与 IP 商业变革
网络·人工智能·学习·tcp/ip·企业管理·创始人ip·创客匠人
Guheyunyi1 小时前
安防监控系统,如何为你的生活构筑智慧安全屏障?
大数据·人工智能·安全·信息可视化·生活
计算衎1 小时前
python的AI大模型之facebook/nllb-200-distilled-600M的介绍和使用
人工智能·python·facebook·huggingface_hub
小狗照亮每一天1 小时前
【菜狗看背景】自动驾驶发展背景——20251117
人工智能·机器学习·自动驾驶
运筹说1 小时前
运筹说145期:从快递到自动驾驶:启发式算法的智慧幕后
人工智能·自动驾驶·启发式算法
大白IT1 小时前
智能驾驶:从感知到规控的自动驾驶系统全解析
人工智能·机器学习·自动驾驶
FL16238631291 小时前
智慧交通自动驾驶场景道路异常检测数据集VOC+YOLO格式8302张6类别
人工智能·yolo·自动驾驶