昇思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)
相关推荐
q***T5833 分钟前
机器学习基础
人工智能·机器学习
大明者省10 分钟前
BERT/ViT 模型核心参数 + 实际编码案例表
人工智能·深度学习·bert
isNotNullX30 分钟前
数据中台有什么用?数据仓库和数据中台怎么选?
大数据·数据仓库·人工智能·数据中台
roman_日积跬步-终至千里1 小时前
【AI Engineering】Should I build this AI application?—AI应用决策框架与实践指南
大数据·人工智能
新智元1 小时前
谷歌 Nano Banana Pro 炸了!硅谷 AI 半壁江山同框,网友:PS 已死
人工智能·openai
m***D2861 小时前
机器学习总结
人工智能·机器学习
新智元1 小时前
51 岁周志华、53 岁刘云浩,当选中国科学院院士!
人工智能·openai
('-')1 小时前
《从根上理解MySQL是怎样运行的》第五章学习笔记
笔记·学习·mysql
魁首2 小时前
初识 ACP (Agent Client Protocol)
人工智能·ai编程·mcp
周末程序猿2 小时前
开源项目|不一样的思维导图
人工智能·后端