当前位置: 首页 > news >正文

LLM模型与实践之基于MindSpore的GPT2文本摘要

前言

安装环境

!pip install tokenizers==0.15.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
# 该案例在 mindnlp 0.3.1 版本完成适配,如果发现案例跑不通,可以指定mindnlp版本,执行`!pip install mindnlp==0.3.1`
!pip install mindnlp

数据加载

from mindnlp.utils import http_get# download dataset
url = 'https://download.mindspore.cn/toolkits/mindnlp/dataset/text_generation/nlpcc2017/train_with_summ.txt'
path = http_get(url, './')

数据预处理

原始数据格式:

article: [CLS] article_context [SEP]
summary: [CLS] summary_context [SEP]

预处理后的数据格式:

[CLS] article_context [SEP] summary_context [SEP]
import json
import numpy as np# preprocess dataset
def process_dataset(dataset, tokenizer, batch_size=6, max_seq_len=1024, shuffle=False):def read_map(text):data = json.loads(text.tobytes())return np.array(data['article']), np.array(data['summarization'])def merge_and_pad(article, summary):# tokenization# pad to max_seq_length, only truncate the articletokenized = tokenizer(text=article, text_pair=summary,padding='max_length', truncation='only_first', max_length=max_seq_len)return tokenized['input_ids'], tokenized['input_ids']dataset = dataset.map(read_map, 'text', ['article', 'summary'])# change column names to input_ids and labels for the following trainingdataset = dataset.map(merge_and_pad, ['article', 'summary'], ['input_ids', 'labels'])dataset = dataset.batch(batch_size)if shuffle:dataset = dataset.shuffle(batch_size)return dataset

模型构建

from mindspore import ops
from mindnlp.transformers import GPT2LMHeadModelclass GPT2ForSummarization(GPT2LMHeadModel):def construct(self,input_ids = None,attention_mask = None,labels = None,):outputs = super().construct(input_ids=input_ids, attention_mask=attention_mask)shift_logits = outputs.logits[..., :-1, :]shift_labels = labels[..., 1:]# Flatten the tokensloss = ops.cross_entropy(shift_logits.view(-1, shift_logits.shape[-1]), shift_labels.view(-1), ignore_index=tokenizer.pad_token_id)return loss

模型训练

模型推理

def process_test_dataset(dataset, tokenizer, batch_size=1, max_seq_len=1024, max_summary_len=100):def read_map(text):data = json.loads(text.tobytes())return np.array(data['article']), np.array(data['summarization'])def pad(article):tokenized = tokenizer(text=article, truncation=True, max_length=max_seq_len-max_summary_len)return tokenized['input_ids']dataset = dataset.map(read_map, 'text', ['article', 'summary'])dataset = dataset.map(pad, 'article', ['input_ids'])dataset = dataset.batch(batch_size)return dataset

总结

使用mindnlp库实现GPT2模型进行文本摘要,采用BertTokenizer进行分词, 使用线性预热和衰减的学习率策略进行模型训练. 通过多种数据预处理和模型优化技术, 训练并部署模型进行文本摘要推理. 

http://www.lryc.cn/news/405984.html

相关文章:

  • 【Android】使用视图绑定ViewBinding来代替findViewById
  • 字符的统计——423、657、551、696、467、535
  • pytest+allure
  • 【数据结构】AVL树(平衡二叉搜索树)
  • ASP.NET Web Api 使用 EF 6,DateTime 字段如何取数据库服务器当前时间
  • 【HarmonyOS】应用设置屏幕常亮
  • Docker部署Elasticsearch8.6.0 Kibana8.6.0
  • 第四篇论文小记
  • python使用 tkinter 生成随机颜色
  • 【Linux学习 | 第1篇】Linux介绍+安装
  • 设计模式-抽象工厂
  • Ubunton-24.04 简单配置使用
  • 什么是STP环路保护
  • Python算法基础:解锁冒泡排序与选择排序的奥秘
  • QtCMake工程提升类后找不到头文件
  • Docker核心技术:Docker原理之Cgroups
  • union的特性和大小端
  • 个性化IT服务探索实践
  • UE4-打包游戏,游戏模式,默认关卡
  • Unity ShaderLab基础
  • 用代理IP会频繁掉线是什么原因?HTTP和SOCKS5协议优劣势是什么?
  • MongoDB教程(十三):MongoDB覆盖索引
  • 快速认识EA(Enterprise Architecture)
  • 词云图制作
  • AndroidStudio与手机进行无线调试
  • 脉冲编码调制(PCM,Pulse Code Modulation)简介
  • Pytorch transforms 的研究
  • 一个C++模板工厂的编译问题的解决。针对第三方库的构造函数以及追加了的对象构造函数。牵扯到重载、特化等
  • 《昇思 25 天学习打卡营第 20 天 | Pix2Pix实现图像转换 》
  • 关于c#的简单应用三题