昇思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)
相关推荐
Token炼金师1 分钟前
模型的防线:Prompt 注入防御、越狱攻击与对齐、红队测试、价值观对齐、对抗样本鲁棒性、安全评测与边界 —— 模型安全六防
人工智能·红队测试·prompt 注入防御·越狱攻击与对齐·价值观对齐·对抗样本鲁棒性·安全评测与边界
minglie14 分钟前
高等代数试题
学习
嘘神秘用15 分钟前
布:AI 驱动的 Redis 客户端,更快、更直观
数据库·人工智能·redis
黒亱中旳16 分钟前
Java AI 框架三国杀:Solon AI vs Spring AI vs LangChain4j 深度对比
java·人工智能·spring
小和尚同志33 分钟前
前端 AI 单元测试思考与落地
前端·人工智能·aigc
alxraves1 小时前
医用超声远程会诊系统:会诊平台的核心架构与功能解析
java·人工智能·架构
Xiaofeng36931 小时前
GPT-5.6 发布后,我用了一个周末重新规划学习路线图
gpt·学习
jinggongszh1 小时前
智能硬件对接与系统落地:开发岗在制造现场的经验沉淀
大数据·人工智能
ZeekerLin1 小时前
AI 原生团队协作机制:角色、分工与工程文化变化
大数据·人工智能
城中南小2 小时前
零基础认识大语言模型(LLM)工作原理(5.Transformer的FFN模块到底是什么?)
人工智能·语言模型·transformer