昇思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)
相关推荐
Sopaco4 分钟前
2026年大火的AI工程化中多智能体协调的艺术
人工智能
超自然祈祷18 分钟前
战术战法计策计谋博弈随笔
人工智能
runningshark24 分钟前
【软件端(3)】CNN
人工智能
minhuan42 分钟前
大模型应用:多卡集群跑满14B模型:大模型推理算力应用实践.66
人工智能
golang学习记1 小时前
VS Code 发布新终端
人工智能
LaoZhangGong1231 小时前
学习TCP/IP的第4步:重点掌握TCP序列号和确认号
网络·学习·tcp/ip·以太网
DuHz1 小时前
UWB 雷达综述精读:应用、标准、信号处理、数据集、芯片与未来方向——论文阅读
论文阅读·学习·算法·信息与通信·信号处理
calvinpaean1 小时前
Video Depth Anything: Consistent Depth Estimation for Super-Long Videos论文学习
学习
wubba lubba dub dub7501 小时前
第三十四周 学习周报
学习
songyuc1 小时前
【SAR】旋转框定义法学习笔记
笔记·学习