昇思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)
相关推荐
程序员小白条2 分钟前
AI 编程辅助,从入门到真香
java·开发语言·数据库·人工智能·面试·职场和发展
SKY -dada4 分钟前
【无标题】
人工智能·chatgpt·能源·双向赋能
code_pgf5 分钟前
SAM在机器人融合感知的泛化应用
人工智能·计算机视觉·机器人
MATLAB代码顾问6 分钟前
【深度学习】Vision Transformer (ViT) 详解:从原理到实践
人工智能·深度学习·transformer
fuquxiaoguang6 分钟前
0.8W跑10B模型:端侧AI的“寒武纪爆发“与中间件的轻量进化
人工智能·中间件·端侧ai
嵌入式小企鹅7 分钟前
国产算力突破、RISC-V车规生态成型、AI编程工具免费化浪潮
学习·开源·ai编程·risc-v·昇腾·deepseek v4
XMAIPC_Robot8 分钟前
基于RK3588 高算力,小尺寸,轻重量6T算力无人机AI模块,可接两路同步相机模组
运维·人工智能·深度学习·fpga开发·无人机·边缘计算
SuperHeroWu79 分钟前
【AI大模型】Self-Attention:为什么它能取代 RNN 解决长距离依赖?
人工智能·rnn·深度学习·循环神经网络·自注意力机制·self-attention
数信云 DCloud10 分钟前
人工智能安全观察:漫谈与AI新物种相处之道
人工智能·安全·ai·智能体
朝新_11 分钟前
【LangChain】少样本提示(few-shorting) 掌握 Few-Shot 提示,让大模型按你的规则输出
java·人工智能·langchain