昇思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)
相关推荐
QXWZ_IA1 小时前
道路巡检用边缘计算好还是云端处理好:跟着一帧图像走一遍
人工智能·边缘计算
硅谷秋水2 小时前
RoboKit:加速机器人学习中的并行可微分优化
人工智能·机器学习·语言模型·机器人
AvocadoZeroOneOI2 小时前
Kadane 算法(最大子段和)
c++·学习·算法·动态规划
西瓜籽大魔王2 小时前
Midjourney新手教程:V8.2参数别背,一张头像从选图到改图走一遍
人工智能
EatFan3 小时前
GitHub 热榜 14 个项目 11 个与 AI Agent 相关:Agent Skills 正在成为智能体的新分发层
人工智能·github
秦先生在广东3 小时前
百万字Markdown不卡顿的秘密
人工智能
秦先生在广东3 小时前
别用AI生图,改代码直接出电影级视频
人工智能
秦先生在广东3 小时前
DS-Bot让AI机器人有了永久记忆
人工智能
深海鱼肝油ya4 小时前
LangChain(二)提示词 & 格式化输出
人工智能·大模型·llm·agent·提示词·格式化输出·智能体
云上工程笔记4 小时前
优刻得UCloud技术实践:中立云定位、自建智算中心与AI工厂方案怎么落地
人工智能