昇思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 分钟前
引领新一代人机交互,科大讯飞全新发布星火语音基座大模型 SparkAudio 1.0
人工智能·人机交互·语音识别
神奇的代码在哪里3 分钟前
MOSS-Image-SFT 开源解读:JSON 布局控制文字与排版
人工智能·开源·文生图·diffusers·moss-image
猪腰子正7 分钟前
深度学习环境安装教程-适用Windows系统
人工智能·windows·深度学习
随性而行3608 分钟前
企业微信 Webhook 接收不到消息?网络连通性、URL 签名验签与 API 返回错误码排查指南
java·网络·人工智能·后端·微信·企业微信
IT大白鼠13 分钟前
MCP 生态半年报从 Anthropic 的一份协议,到 AI 世界的「USB-C」
人工智能
TMT星球13 分钟前
觅蜂联合发起具身评测平台RoboColiseum,GPT-6、Opus 5.5、Gemini 4同台决战!
人工智能·gpt
lucas_AI14 分钟前
Kimi K3.1 Pro 发布:104B 激活参数追到 Opus 5.5 差 0.7 个点,还主动「删掉」了自己的网络攻击能力
人工智能
AIGC大时代14 分钟前
AI 辅助题录筛选怎么选型终裁:金标准召回·漏检·批处理边界的能用/限场景/不能用验收
人工智能
ebok.15 分钟前
低代码平台如何集成 AI:从开发侧到运行侧的技术实践与京微智枢方案
人工智能·低代码·ai·rxjava
larance16 分钟前
[机器学习40讲]28-最简单的概率图:朴素贝叶斯
人工智能·机器学习