昇思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)
相关推荐
大闲在人3 分钟前
传统软件工程在 AI 时代急需改进的四个核心维度
人工智能·软件工程
CappuccinoRose14 分钟前
CSS 语法学习文档(十五)
前端·学习·重构·渲染·浏览器
qyresearch_19 分钟前
机动休闲艇产业:技术革新与消费升级驱动下的全球市场新格局
人工智能
湘-枫叶情缘27 分钟前
从数据库写作到情绪工程:网络文学工程化转向的理论综述
数据库·人工智能
heimeiyingwang41 分钟前
企业非结构化数据的 AI 处理与价值挖掘
大数据·数据库·人工智能·机器学习·架构
开开心心就好1 小时前
轻松鼠标连, 自定义区域模仿人手点击
人工智能·windows·物联网·计算机视觉·计算机外设·ocr·excel
HuDie3401 小时前
AI产品经理课程笔记
人工智能·笔记·产品经理
香芋Yu1 小时前
【大模型面试突击】04_Embedding与表示学习
学习·面试·embedding
枕石 入梦2 小时前
华为云服务器本地部署大模型实战(Ollama + Tesla T4 踩坑记)
服务器·人工智能·大模型·华为云
智慧化智能化数字化方案2 小时前
财务数字化——解读农化集团业财一体化数字化转型解决方案【附全文阅读】
大数据·人工智能