昇思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)
相关推荐
夜斗小神社9 分钟前
【黑马RAG与Agent智能体项目】(二)提示词工程
人工智能
C++ 老炮儿的技术栈10 分钟前
不调用C++/C的字符串库函数,编写函数strcmp
c语言·开发语言·c++·人工智能·windows·git·visual studio
码农三叔15 分钟前
(6-1)手部、足部与末端执行器设计:仿生手设计
人工智能·架构·机器人·人形机器人
●VON16 分钟前
Flutter for OpenHarmony:基于可选描述字段与上下文感知渲染的 TodoList 任务详情子系统实现
学习·flutter·架构·交互·von
liliangcsdn16 分钟前
RL中GAE的计算过程详解
大数据·人工智能·算法
yhyvc18 分钟前
人形具身机器人国产/进口快速选型优先级清单
人工智能·机器人
wangmengxxw26 分钟前
SpringAI-mysql
java·数据库·人工智能·mysql·springai
考證寶題庫網30 分钟前
Designing and Implementing a Microsoft Azure AI Solution 微軟Azure AI-102 認證全攻略
人工智能·microsoft·azure
逄逄不是胖胖38 分钟前
《动手学深度学习》-52文本预处理实现
人工智能·pytorch·python·深度学习
Pyeako41 分钟前
opencv计算机视觉--图形透视(投影)变换&图形拼接
人工智能·python·opencv·计算机视觉·图片拼接·投影变换·图形透视变换