昇思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)
相关推荐
Camellia03113 分钟前
嵌入式学习--江协stm32day5
stm32·嵌入式硬件·学习
HeteroCat3 分钟前
一周年工作总结:做了一年的AI工作我都干了什么?
人工智能
a_157153249867 分钟前
SpringCloud学习笔记-4
笔记·学习·spring cloud
YSGZJJ17 分钟前
股指期货技术分析与短线操作方法介绍
大数据·人工智能
Guheyunyi26 分钟前
监测预警系统重塑隧道安全新范式
大数据·运维·人工智能·科技·安全
码码哈哈爱分享27 分钟前
[特殊字符] Whisper 模型介绍(OpenAI 语音识别系统)
人工智能·whisper·语音识别
郄堃Deep Traffic33 分钟前
机器学习+城市规划第十三期:XGBoost的地理加权改进,利用树模型实现更精准的地理加权回归
人工智能·机器学习·回归·城市规划
Lucky-Niu33 分钟前
解决transformers.adapters import AdapterConfig 报错的问题
人工智能·深度学习
FserSuN38 分钟前
Prompt工程学习之思维树(TOT)
人工智能·学习·prompt