昇思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)
相关推荐
往事如yan1 分钟前
VS Code 单击 Markdown 默认打开预览
人工智能
jkyy20144 分钟前
移动出行健康空间崛起:人车家一体化,补齐智慧生活健康短板
人工智能·物联网·生活·健康医疗
Lee_Chen8611 分钟前
5天开发企业级 Agent(设计篇 01)|一条主链与八个模块
人工智能
Code额16 分钟前
Python asyncio 异步编程全套学习文档(零基础完整版)
python·学习·oracle·async·异步·asyncio
秦先生在广东17 分钟前
Agent Substrate:用 30× 超配比把 AI Agent 运行成本打下来的云原生沙箱调度系统
人工智能
qq_3144053521 分钟前
用知漫剧写修真漫剧剧本:如何一键生成门派升阶的标准化剧情?
人工智能
NutShell Wang25 分钟前
Rust 1.97 实战迁移:v0 符号重整、Cargo 警告治理与位运算新 API
人工智能·后端·性能优化·rust·vibe coding
小刘快学习26 分钟前
职业教育机构的题库答疑与学情分析,模型账怎么算清
大数据·人工智能
江瀚视野28 分钟前
AperData销量2天破万,五一视界未来何在?
大数据·人工智能
贵慜_Derek32 分钟前
DeepSeek Harness 记忆解读:场域分层、参考架构与实现思路
人工智能·agent·deepseek