昇思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)
相关推荐
楼田莉子8 分钟前
C++项目:日志&&线程池
linux·c++·学习·visual studio code
沐曦股份MetaX15 分钟前
基于内生复杂性的类脑脉冲大模型“瞬悉1.0”问世
人工智能·开源
weixin_4215850121 分钟前
表示学习发展历程
学习
power 雀儿35 分钟前
张量基本运算
人工智能
陈天伟教授1 小时前
人工智能应用- 人工智能交叉:01. 破解蛋白质结构之谜
人工智能·神经网络·算法·机器学习·推荐算法
EmbedLinX1 小时前
嵌入式Linux之U-Boot
linux·服务器·笔记·学习
政安晨1 小时前
政安晨【人工智能项目随笔】使用OpenClaw的主节点协同子节点撰写大型技术前沿论文的实战指南
人工智能·ai agent·openclaw论文写作·openclaw论文写作经验·ai代理写论文·ai分布式协作·oepnclaw应用
大成京牌2 小时前
2026年京牌政策深度对比,三款优质车型选购推荐榜单探索
人工智能
xuxianliang3 小时前
第154章 “神谕”的低语(AI)
人工智能·程序员创富
geneculture3 小时前
人机互助新时代超级个体(OPC)的学术述评——基于人文学科与数理学科的双重视域
大数据·人工智能·哲学与科学统一性·信息融智学·融智时代(杂志)