昇思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)
相关推荐
Eward-an6 分钟前
华为ModelEngine全流程评测:从智能体开发到应用编排,解锁企业级AI开发新范式
大数据·人工智能
liliwoliliwo13 分钟前
深度学习--CNN
人工智能·深度学习
70asunflower17 分钟前
LLM几种主要的开源方式及优劣
人工智能·llm
科技林总35 分钟前
【系统分析师】11.5 软件需求文档化
学习
fids38 分钟前
大模型面试题
人工智能·llm
九硕智慧建筑一体化厂家39 分钟前
DDC:看似普通的存在,在楼宇自控系统中却主宰智能建筑高效运行?
大数据·运维·人工智能·网络协议·制造·设计规范
y芋泥啵啵gfe40 分钟前
AI考研深造VS直接工作:选对赛道,认证为竞争力加码
人工智能·职场和发展
python零基础入门小白43 分钟前
多智能体强化学习:从基础到实战,掌握协作与竞争的艺术!
人工智能·学习·chatgpt·大模型·产品经理·ai大模型·大模型学习
志栋智能1 小时前
安全超自动化的终极目标:实现自适应安全防护
运维·人工智能·安全·自动化