Chinese NewsTitle Generation Project by GPT2.带有超级详细注释的中文GPT2新闻标题生成项目。

Overview

GPT2-NewsTitle

带有超详细注释的GPT2新闻标题生成项目

UpDate 01.02.2021

  • 从网上收集数据,将清华新闻数据、搜狗新闻数据等新闻数据集,以及开源的一些摘要数据进行整理清洗,构建一个较完善的中文摘要数据集。
  • 数据集清洗时,仅进行了简单地规则清洗。例如:清洗htlm标记、去除多余空字符、去除图片标记等。
  • 处理后数据集详细信息,见数据集描述
数据 原始数据/项目地址 处理后文件下载地址
清华新闻数据 地址 百度云盘 提取码: vhol
搜狗新闻数据 地址 百度云盘 提取码:ode6
nlpcc2017摘要数据 地址 百度云盘 提取码:e0zq
csl摘要数据 地址 百度云盘 提取码:0qot
教育培训行业摘要数据 地址 百度云盘 提取码:kjz3
lcsts摘要数据 地址 百度云盘 提取码:bzov
神策杯2018摘要数据 地址 百度云盘 提取码:6f4f
万方摘要数据 地址 百度云盘 提取码: p69g
微信公众号摘要数据 地址 百度云盘 提取码: 5has
微博数据 地址 百度云盘 提取码: 85t5
news2016zh新闻数据 地址 百度云盘 提取码: qsj1

数据集集合:百度云盘 提取码: 7am8

项目描述

  • 本项目是一个带有超级详细中文注释的基于GPT2模型的新闻标题生成项目。
  • 本项目参考了GPT2-ChineseGPT2-chitchatCDial-GPTGPT2等多个GPT2开源项目,并根据自己的理解,将代码进行重构,添加详细注释,希望可以帮助到有需要的人。
  • 本项目使用HuggingFace的transformers实现GPT2模型代码编写、训练及测试。
  • 本项目通过Flask框架搭建了一个Web服务,将新闻摘要生成模型进行工程化,可以通过页面可视化地体验新闻标题生成效果。
  • 本项目的代码详细讲解,可以自行阅读代码,也可查看代码注释介绍
  • 本项目提供的新闻标题模型是一个6层的小模型(其实是穷人没人卡,只能训练小模型),并且在训练该模型过程中,没有加载预训练的GPT2模型而是随机初始化的参数,并且训练轮数较少(5轮,还没收敛完),因此效果一般。如果想要更好效果的模型,可以按照个人需求训练一个模型。
  • 本项目的目的是带领大家走一遍GPT2生成模型的训练、测试及部署全部流程。

文件结构

  • config
    • config.json 模型的配置信息,包含n_ctx、n_embd、n_head、n_layer等。
  • vocab
    • vocab.txt 字典文件,该字典为大小为13317,删除了将原始字典中的“##中文”,并且增加了“[Content]”、“[Title]”、“[Space]”等标记。
  • data_dir 存放数据的文件夹
  • templates 存放html页面的文件夹
  • data_helper.py 数据预处理文件,将数据进行简单的清洗
  • data_set.py 数据类文件,定义模型所需的数据类,方便模型训练使用
  • model.py GPT2模型文件,主要对transformers包中GPT2LMHeadModel的重写,修改计算loss部分,只计算预测title部分的loss
  • train.py 通过新闻正文生成新闻标题的GPT2模型的训练文件
  • generate_title.py 根据训练好的模型,进行新闻标题生成,预测文件
  • http_server.py 构建web服务文件

运行环境

  • gevent == 1.3a1
  • flask == 0.12.2
  • transformers == 3.0.2

详细见requirements.txt文件

数据集

数据来源于新浪微博,数据链接:https://www.jianshu.com/p/8f52352f0748?tdsourcetag=s_pcqq_aiomsg

数据描述 下载地址
原始数据 百度网盘,提取码: nqzi
处理后数据 百度网盘,提取码: duba

原始数据为直接从网上下载的新闻数据,处理后数据为使用data_helper.py处理过的数据,可直接用于训练。

模型参数

详细见config/config.json文件

参数
initializer_range 0.02
layer_norm_epsilon 1e-05
n_ctx 512
n_embd 768
n_head 12
n_layer 6
n_positions 512
vocab_size 13317

注意:模型输入除了各个词的向量表示外,还包括文字段落向量表示和位置向量表示。

模型文件分享

模型 下载地址
GPT2模型 百度网盘,提取码: 165b

模型训练

python3 train.py
或
python3 train.py --output_dir output_dir/(自定义保存模型路径) 

训练参数可自行添加,包含参数具体如下:

参数 类型 默认值 描述
device str "0" 设置训练或测试时使用的显卡
config_path str "config/config.json" 模型参数配置信息
vocab_path str "vocab/vocab.txt" 词表,该词表为小词表,并增加了一些新的标记
train_file_path str "data_dir/train_data.json" 新闻标题生成的训练数据
test_file_path str "data_dir/test_data.json" 新闻标题生成的测试数据
pretrained_model_path str None 预训练的GPT2模型的路径
data_dir str "data_dir" 生成缓存数据的存放路径
num_train_epochs int 5 模型训练的轮数
train_batch_size int 16 训练时每个batch的大小
test_batch_size int 8 测试时每个batch的大小
learning_rate float 1e-4 模型训练时的学习率
warmup_proportion float 0.1 warm up概率,即训练总步长的百分之多少,进行warm up操作
adam_epsilon float 1e-8 Adam优化器的epsilon值
logging_steps int 20 保存训练日志的步数
eval_steps int 4000 训练时,多少步进行一次测试
gradient_accumulation_steps int 1 梯度积累
max_grad_norm float 1.0
output_dir str "output_dir/" 模型输出路径
seed int 2020 随机种子
max_len int 512 输入模型的最大长度,要比config中n_ctx小

或者修改train.py文件中的set_args函数内容,可修改默认值。

本项目提供的模型,共训练了5个epoch,模型训练损失和测试集损失分别如下:

模型其实还没有训练完全,按照loss走势,还可以继续训练。

模型测试

python3 generate_title.py
或
python3 generate_title.py --top_k 3 --top_p 0.9999 --generate_max_len 32

参数可自行添加,包含参数具体如下:

参数 类型 默认值 描述
device str "0" 设置训练或测试时使用的显卡
model_path str "output_dir/checkpoint-139805" 模型文件路径
vocab_path str "vocab/vocab.txt" 词表,该词表为小词表,并增加了一些新的标记
batch_size int 3 生成标题的个数
generate_max_len int 32 生成标题的最大长度
repetition_penalty float 1.2 重复处罚率
top_k int 5 解码时保留概率最高的多少个标记
top_p float 0.95 解码时保留概率累加大于多少的标记
max_len int 512 输入模型的最大长度,要比config中n_ctx小

测试结果如下:

从测试集中抽一篇
content:
今日,中国三条重要高铁干线——兰新高铁、贵广铁路和南广铁路将开通运营。其中兰新高铁是中国首条高原高铁,全长1776公里,最高票价658元。贵广铁路最贵车票320元,南广铁路最贵车票206.5元,这两条线路大大缩短西南与各地的时空距离。出行更方便了!中国“高铁版图”再扩容 三条重要高铁今日开通
title:
生成的第1个标题为:中国“高铁版图”再扩容 三条重要高铁今日开通
生成的第2个标题为:贵广铁路最高铁版图
生成的第3个标题为:出行更方便了!中国“高铁版图”再扩容三条重要高铁今日开通

从网上随便找一篇新闻
content:
值岁末,一年一度的中央经济工作会议牵动全球目光。今年的会议,背景特殊、节点关键、意义重大。12月16日至18日。北京,京西宾馆。站在“两个一百年”奋斗目标的历史交汇点上,2020年中央经济工作会议谋划着中国经济发展大计。习近平总书记在会上发表了重要讲话,深刻分析国内外经济形势,提出2021年经济工作总体要求和政策取向,部署重点任务,为开局“十四五”、开启全面建设社会主义现代化国家新征程定向领航。
title:
生成的第1个标题为:习近平总书记在京会上发表重大计划 提出2025年经济工作总体要求和政策
生成的第2个标题为:习近平总书记在会上发表重要讲话
生成的第3个标题为:习近平总书记在会上发表重要讲话,深刻分析国内外经济形势

解码采用top_k和top_p解码策略,有一定的随机性,可重复生成。

启动Flask服务

python3 http_server.py
或
python3 http_server.py --http_id "0.0.0.0" --port 5555

本地测试直接使用"127.0.0.1:5555/news-title-generate",如果给他人访问,只需将"127.0.0.1"替换成的电脑的IP地址即可。

具体如下图所示:

未来工作

  • 后期可能会将清华新闻数据、搜狗新闻数据等新闻数据集进行整理清洗,构建一个较完善的新闻标题数据集。
  • 后期可能会使用新闻数据训练一个小的GPT2预训练模型。
  • 后期可能会对已上传的新闻标题模型进行更新,训练一个效果较好的模型。

致谢

参考

Citing

@misc{GPT2-NewsTitle,
  author = {Cong Liu},
  title = {Chinese NewsTitle Generation Project by GPT2},
  year = {2020},
  publisher = {GitHub},
  journal = {GitHub repository},
  url="https://github.com/liucongg/GPT2-NewsTitle",
}

联系作者

e-mail:[email protected]

知乎:刘聪NLP

公众号:NLP工作站

Owner
logCong
知乎:刘聪NLP 公众号:NLP工作站
logCong
Code for ACL 2020 paper "Rigid Formats Controlled Text Generation"

SongNet SongNet: SongCi + Song (Lyrics) + Sonnet + etc. @inproceedings{li-etal-2020-rigid, title = "Rigid Formats Controlled Text Generation",

Piji Li 212 Dec 17, 2022
Silero Models: pre-trained speech-to-text, text-to-speech models and benchmarks made embarrassingly simple

Silero Models: pre-trained speech-to-text, text-to-speech models and benchmarks made embarrassingly simple

Alexander Veysov 3.2k Dec 31, 2022
ChatterBot is a machine learning, conversational dialog engine for creating chat bots

ChatterBot ChatterBot is a machine-learning based conversational dialog engine build in Python which makes it possible to generate responses based on

Gunther Cox 12.8k Jan 03, 2023
Conditional Transformer Language Model for Controllable Generation

CTRL - A Conditional Transformer Language Model for Controllable Generation Authors: Nitish Shirish Keskar, Bryan McCann, Lav Varshney, Caiming Xiong,

Salesforce 1.7k Dec 28, 2022
Syntax-aware Multi-spans Generation for Reading Comprehension (TASLP 2022)

SyntaxGen Syntax-aware Multi-spans Generation for Reading Comprehension (TASLP 2022) In this repo, we upload all the scripts for this work. Due to siz

Zhuosheng Zhang 3 Jun 13, 2022
This project aims to conduct a text information retrieval and text mining on medical research publication regarding Covid19 - treatments and vaccinations.

Project: Text Analysis - This project aims to conduct a text information retrieval and text mining on medical research publication regarding Covid19 -

1 Mar 14, 2022
基于pytorch+bert的中文事件抽取

pytorch_bert_event_extraction 基于pytorch+bert的中文事件抽取,主要思想是QA(问答)。 要预先下载好chinese-roberta-wwm-ext模型,并在运行时指定模型的位置。

西西嘛呦 31 Nov 30, 2022
Mapping a variable-length sentence to a fixed-length vector using BERT model

Are you looking for X-as-service? Try the Cloud-Native Neural Search Framework for Any Kind of Data bert-as-service Using BERT model as a sentence enc

Han Xiao 11.1k Jan 01, 2023
Implementation of COCO-LM, Correcting and Contrasting Text Sequences for Language Model Pretraining, in Pytorch

COCO LM Pretraining (wip) Implementation of COCO-LM, Correcting and Contrasting Text Sequences for Language Model Pretraining, in Pytorch. They were a

Phil Wang 44 Jul 28, 2022
An ultra fast tiny model for lane detection, using onnx_parser, TensorRTAPI, torch2trt to accelerate. our model support for int8, dynamic input and profiling. (Nvidia-Alibaba-TensoRT-hackathon2021)

Ultra_Fast_Lane_Detection_TensorRT An ultra fast tiny model for lane detection, using onnx_parser, TensorRTAPI to accelerate. our model support for in

steven.yan 121 Dec 27, 2022
Compute distance between sequences. 30+ algorithms, pure python implementation, common interface, optional external libs usage.

TextDistance TextDistance -- python library for comparing distance between two or more sequences by many algorithms. Features: 30+ algorithms Pure pyt

Life4 3k Jan 06, 2023
Code for the paper "Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer"

T5: Text-To-Text Transfer Transformer The t5 library serves primarily as code for reproducing the experiments in Exploring the Limits of Transfer Lear

Google Research 4.6k Jan 01, 2023
A model library for exploring state-of-the-art deep learning topologies and techniques for optimizing Natural Language Processing neural networks

A Deep Learning NLP/NLU library by Intel® AI Lab Overview | Models | Installation | Examples | Documentation | Tutorials | Contributing NLP Architect

Intel Labs 2.9k Jan 02, 2023
Free and Open Source Machine Translation API. 100% self-hosted, offline capable and easy to setup.

LibreTranslate Try it online! | API Docs | Community Forum Free and Open Source Machine Translation API, entirely self-hosted. Unlike other APIs, it d

3.4k Dec 27, 2022
Collection of useful (to me) python scripts for interacting with napari

Napari scripts A collection of napari related tools in various state of disrepair/functionality. Browse_LIF_widget.py This module can be imported, for

5 Aug 15, 2022
This is a project built for FALLABOUT2021 event under SRMMIC, This project deals with NLP poetry generation.

FALLABOUT-SRMMIC 21 POETRY-GENERATION HINGLISH DESCRIPTION We have developed a NLP(natural language processing) model which automatically generates a

7 Sep 28, 2021
A fast hierarchical dimensionality reduction algorithm.

h-NNE: Hierarchical Nearest Neighbor Embedding A fast hierarchical dimensionality reduction algorithm. h-NNE is a general purpose dimensionality reduc

Marios Koulakis 35 Dec 12, 2022
Binary LSTM model for text classification

Text Classification The purpose of this repository is to create a neural network model of NLP with deep learning for binary classification of texts re

Nikita Elenberger 1 Mar 11, 2022
Facilitating the design, comparison and sharing of deep text matching models.

MatchZoo Facilitating the design, comparison and sharing of deep text matching models. MatchZoo 是一个通用的文本匹配工具包,它旨在方便大家快速的实现、比较、以及分享最新的深度文本匹配模型。 🔥 News

Neural Text Matching Community 3.7k Jan 02, 2023
Code for our ACL 2021 (Findings) Paper - Fingerprinting Fine-tuned Language Models in the wild .

🌳 Fingerprinting Fine-tuned Language Models in the wild This is the code and dataset for our ACL 2021 (Findings) Paper - Fingerprinting Fine-tuned La

LCS2-IIITDelhi 5 Sep 13, 2022