昇思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)
相关推荐
非著名架构师11 分钟前
智慧气象护航:构建陆海空立体交通气象安全保障体系
大数据·人工智能·安全·疾风气象大模型4.0·疾风气象大模型·风光功率预测
tech-share25 分钟前
基于pytorch 自建AI大模型
人工智能·深度学习·机器学习·gpu算力
j***121526 分钟前
网络爬虫学习:应用selenium获取Edge浏览器版本号,自动下载对应版本msedgedriver,确保Edge浏览器顺利打开。
爬虫·学习·selenium
夏洛克信徒1 小时前
从 “工具” 到 “代理”:Gemini 3.0 重构 AI 能力边界,开启智能协作新纪元
大数据·人工智能·神经网络
AI浩1 小时前
回归基础:让去噪生成模型真正去噪
人工智能·数据挖掘·回归
ekprada1 小时前
DAY 16 数组的常见操作和形状
人工智能·python·机器学习
用户5191495848451 小时前
C#扩展成员全面解析:从方法到属性的演进
人工智能·aigc
柳鲲鹏1 小时前
OpenCV: 光流法python代码
人工智能·python·opencv
✎ ﹏梦醒͜ღ҉繁华落℘2 小时前
freeRTOS学习笔记(十四)--内存
笔记·学习
又是忙碌的一天2 小时前
mysql 学习第二天 SQL语句
sql·学习·mysql