欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python PaddleNLP实现自动生成虎年藏头诗

程序员文章站 2022-06-24 16:16:51
目录一、 数据处理1.paddlenlp升级2.提取诗头3.生成词表4.定义dataset二、定义模型并训练1.模型定义2.模型训练3.模型保存三、生成藏头诗总结一、 数据处理本项目中利用古诗数据集作...

一、 数据处理

本项目中利用古诗数据集作为训练集,编码器接收古诗的每个字的开头,解码器利用编码器的信息生成所有的诗句。为了诗句之间的连贯性,编码器同时也在诗头之前加上之前诗句的信息。举例:

“白日依山尽,黄河入海流,欲穷千里目,更上一层楼。” 可以生成两个样本:

样本一:编码器输入,“白”;解码器输入,“白日依山尽,黄河入海流”

样本二:编码器输入,“白日依山尽,黄河入海流。欲”;解码器输入,“欲穷千里目,更上一层楼。”

1.paddlenlp升级

!pip install -u paddlenlp
looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
collecting paddlenlp
[?25l  downloading https://pypi.tuna.tsinghua.edu.cn/packages/17/9b/4535ccf0e96c302a3066bd2e4d0f44b6b1a73487c6793024475b48466c32/paddlenlp-2.2.3-py3-none-any.whl (1.2mb)
[k     |████████████████████████████████| 1.2mb 11.2mb/s eta 0:00:01
[?25hrequirement already satisfied, skipping upgrade: h5py in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlenlp) (2.9.0)
requirement already satisfied, skipping upgrade: colorlog in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlenlp) (4.1.0)
requirement already satisfied, skipping upgrade: colorama in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlenlp) (0.4.4)
requirement already satisfied, skipping upgrade: seqeval in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlenlp) (1.2.2)
requirement already satisfied, skipping upgrade: jieba in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlenlp) (0.42.1)
requirement already satisfied, skipping upgrade: multiprocess in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlenlp) (0.70.11.1)
requirement already satisfied, skipping upgrade: six in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from h5py->paddlenlp) (1.16.0)
requirement already satisfied, skipping upgrade: numpy>=1.7 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from h5py->paddlenlp) (1.20.3)
requirement already satisfied, skipping upgrade: scikit-learn>=0.21.3 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from seqeval->paddlenlp) (0.24.2)
requirement already satisfied, skipping upgrade: dill>=0.3.3 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from multiprocess->paddlenlp) (0.3.3)
requirement already satisfied, skipping upgrade: scipy>=0.19.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from scikit-learn>=0.21.3->seqeval->paddlenlp) (1.6.3)
requirement already satisfied, skipping upgrade: threadpoolctl>=2.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from scikit-learn>=0.21.3->seqeval->paddlenlp) (2.1.0)
requirement already satisfied, skipping upgrade: joblib>=0.11 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from scikit-learn>=0.21.3->seqeval->paddlenlp) (0.14.1)
installing collected packages: paddlenlp
  found existing installation: paddlenlp 2.1.1
    uninstalling paddlenlp-2.1.1:
      successfully uninstalled paddlenlp-2.1.1
successfully installed paddlenlp-2.2.3

2.提取诗头

import re
poems_file = open("./data/data70759/poems_zh.txt", encoding="utf8")
# 对读取的每一行诗句,统计每一句的词头
poems_samples = []
poems_prefix = []
poems_heads = []
for line in poems_file.readlines():
    line_ = re.sub('。', ' ', line)
    line_ = line_.split()
    # 生成训练样本
    for i, p in enumerate(line_):
        poems_heads.append(p[0])
        poems_prefix.append('。'.join(line_[:i]))
        poems_samples.append(p + '。')


# 输出文件信息
for i in range(20):
    print("poems heads:{}, poems_prefix: {}, poems:{}".format(poems_heads[i], poems_prefix[i], poems_samples[i]))
poems heads:欲, poems_prefix: , poems:欲出未出光辣达,千山万山如火发。
poems heads:须, poems_prefix: 欲出未出光辣达,千山万山如火发, poems:须臾走向天上来,逐却残星赶却月。
poems heads:未, poems_prefix: , poems:未离海底千山黑,才到天中万国明。
poems heads:满, poems_prefix: , poems:满目*四望幽,白云高卷嶂烟收。
poems heads:日, poems_prefix: 满目*四望幽,白云高卷嶂烟收, poems:日回禽影穿疏木,风递猿声入小楼。
poems heads:远, poems_prefix: 满目*四望幽,白云高卷嶂烟收。日回禽影穿疏木,风递猿声入小楼, poems:远岫似屏横碧落,断帆如叶截中流。
poems heads:片, poems_prefix: , poems:片片飞来静又闲,楼头江上复山前。
poems heads:飘, poems_prefix: 片片飞来静又闲,楼头江上复山前, poems:飘零尽日不归去,帖破清光万里天。
poems heads:因, poems_prefix: , poems:因登巨石知来处,勃勃元生绿藓痕。
poems heads:静, poems_prefix: 因登巨石知来处,勃勃元生绿藓痕, poems:静即等闲藏草木,动时顷刻徧乾坤。
poems heads:横, poems_prefix: 因登巨石知来处,勃勃元生绿藓痕。静即等闲藏草木,动时顷刻徧乾坤, poems:横天未必朋元恶,捧日还曾瑞至尊。
poems heads:不, poems_prefix: 因登巨石知来处,勃勃元生绿藓痕。静即等闲藏草木,动时顷刻徧乾坤。横天未必朋元恶,捧日还曾瑞至尊, poems:不独朝朝在巫峡,楚王何事谩劳魂。
poems heads:若, poems_prefix: , poems:若教作镇居中国,争得泥金在泰山。
poems heads:才, poems_prefix: , poems:才闻暖律先偷眼,既待和风始展眉。
poems heads:嚼, poems_prefix: , poems:嚼处春冰敲齿冷,咽时雪液沃心寒。
poems heads:蒙, poems_prefix: , poems:蒙君知重惠琼实,薄起金刀钉玉深。
poems heads:深, poems_prefix: , poems:深妆玉瓦平无垅,乱拂芦花细有声。
poems heads:片, poems_prefix: , poems:片逐银蟾落醉觥。
poems heads:巧, poems_prefix: , poems:巧剪银花乱,轻飞玉叶狂。
poems heads:寒, poems_prefix: , poems:寒艳芳姿色尽明。

3.生成词表

# 用paddlenlp生成词表文件,由于诗文的句式较短,我们以单个字作为词单元生成词表
from paddlenlp.data import vocab

vocab = vocab.build_vocab(poems_samples, unk_token="<unk>", pad_token="<pad>", bos_token="<", eos_token=">")
vocab_size = len(vocab)

print("vocab size", vocab_size)
print("word to idx:", vocab.token_to_idx)

4.定义dataset

# 定义数据读取器
from paddle.io import dataset, batchsampler, dataloader
import numpy as np

class poemdataset(dataset):
    def __init__(self, poems_data, poems_heads, poems_prefix, vocab, encoder_max_len=128, decoder_max_len=32):
        super(poemdataset, self).__init__()
        self.poems_data = poems_data
        self.poems_heads = poems_heads
        self.poems_prefix = poems_prefix
        self.vocab = vocab
        self.tokenizer = lambda x: [vocab.token_to_idx[x_] for x_ in x]
        self.encoder_max_len = encoder_max_len
        self.decoder_max_len = decoder_max_len

    def __getitem__(self, idx):
        eos_id = vocab.token_to_idx[vocab.eos_token]
        bos_id = vocab.token_to_idx[vocab.bos_token]
        pad_id = vocab.token_to_idx[vocab.pad_token]
        # 确保encoder和decoder的输出都小于最大长度
        poet = self.poems_data[idx][:self.decoder_max_len - 2]  # -2 包含bos_id和eos_id
        prefix = self.poems_prefix[idx][- (self.encoder_max_len - 3):]  # -3 包含bos_id, eos_id, 和head的编码
        # 对输入输出编码

        sample = [bos_id] + self.tokenizer(poet) + [eos_id]
        prefix = self.tokenizer(prefix) if prefix else []
        heads = prefix + [bos_id] + self.tokenizer(self.poems_heads[idx]) + [eos_id] 
        sample_len = len(sample)
        heads_len = len(heads)
        sample = sample + [pad_id] * (self.decoder_max_len - sample_len)
        heads = heads + [pad_id] * (self.encoder_max_len - heads_len)
        mask = [1] * (sample_len - 1) + [0] * (self.decoder_max_len - sample_len) # -1 to make equal to out[2]
        out = [np.array(d, "int64") for d in [heads, heads_len, sample, sample, mask]]
        out[2] = out[2][:-1]
        out[3] = out[3][1:, np.newaxis]
        return out

    def shape(self):
        return [([none, self.encoder_max_len], 'int64', 'src'),
                ([none, 1], 'int64', 'src_length'),
                ([none, self.decoder_max_len - 1],'int64', 'trg')], \
               [([none, self.decoder_max_len - 1, 1], 'int64', 'label'),
                ([none, self.decoder_max_len - 1], 'int64', 'trg_mask')]


    def __len__(self):
        return len(self.poems_data)

dataset = poemdataset(poems_samples, poems_heads, poems_prefix, vocab)
batch_sampler = batchsampler(dataset, batch_size=2048)
data_loader = dataloader(dataset, batch_sampler=batch_sampler)

二、定义模型并训练

1.模型定义

from seq2seq.models import seq2seqmodel
from paddlenlp.metrics import perplexity
from seq2seq.loss import crossentropycriterion
import paddle
from paddle.static import inputspec

# 参数
lr = 1e-6
max_epoch = 20
models_save_path = "./checkpoints"

encoder_attrs = {"vocab_size": vocab_size, "embed_dim": 200, "hidden_size": 128, "num_layers": 4, "dropout": .2,
                    "direction": "bidirectional", "mode": "gru"}
decoder_attrs = {"vocab_size": vocab_size, "embed_dim": 200, "hidden_size": 128, "num_layers": 4, "direction": "forward",
                    "dropout": .2, "mode": "gru", "use_attention": true}

# inputs shape and label shape
inputs_shape, labels_shape = dataset.shape()
inputs_list = [inputspec(input_shape[0], input_shape[1], input_shape[2]) for input_shape in inputs_shape]
labels_list = [inputspec(label_shape[0], label_shape[1], label_shape[2]) for label_shape in labels_shape]

net = seq2seqmodel(encoder_attrs, decoder_attrs)
model = paddle.model(net, inputs_list, labels_list)

model.load("./final_models/model")

opt = paddle.optimizer.adam(learning_rate=lr, parameters=model.parameters())

model.prepare(opt, crossentropycriterion(), perplexity())
w0122 21:03:30.616776   166 device_context.cc:447] please note: device: 0, gpu compute capability: 7.0, driver api version: 10.1, runtime api version: 10.1
w0122 21:03:30.620450   166 device_context.cc:465] device: 0, cudnn version: 7.6.

2.模型训练

# 训练,训练时间较长,已提供了训练好的模型(./final_models/model)
model.fit(train_data=data_loader, epochs=max_epoch, eval_freq=1, save_freq=5, save_dir=models_save_path, shuffle=true)

3.模型保存

# 保存
model.save("./final_models/model")

三、生成藏头诗

import warnings

def post_process_seq(seq, bos_idx, eos_idx, output_bos=false, output_eos=false):
    """
    post-process the decoded sequence.
    """
    eos_pos = len(seq) - 1
    for i, idx in enumerate(seq):
        if idx == eos_idx:
            eos_pos = i
            break
    seq = [idx for idx in seq[:eos_pos + 1]
           if (output_bos or idx != bos_idx) and (output_eos or idx != eos_idx)]
    return seq

# 定义用于生成祝福语的类
from paddlenlp.data.tokenizer import jiebatokenizer

class genpoems():
    # content (str): the str to generate poems, like "恭喜发财"
    # vocab: the instance of paddlenlp.data.vocab.vocab
    # model: the inference model
    def __init__(self, vocab, model):
        self.bos_id = vocab.token_to_idx[vocab.bos_token]
        self.eos_id = vocab.token_to_idx[vocab.eos_token]
        self.pad_id = vocab.token_to_idx[vocab.pad_token]
        self.tokenizer = lambda x: [vocab.token_to_idx[x_] for x_ in x]
        self.model = model
        self.vocab = vocab

    def gen(self, content, max_len=128):
        # max_len is the encoder_max_len in seq2seq model.
        out = []
        vocab_list = list(vocab.token_to_idx.keys())
        for w in content:
            if w in vocab_list:
                content = re.sub("([。,])", '', content)
                heads = out[- (max_len - 3):] + [self.bos_id] + self.tokenizer(w) + [self.eos_id]
                len_heads = len(heads)
                heads = heads + [self.pad_id] * (max_len - len_heads)
                x = paddle.to_tensor([heads], dtype="int64")
                len_x = paddle.to_tensor([len_heads], dtype='int64')
                pred = self.model.predict_batch(inputs = [x, len_x])[0]
                out += self._get_results(pred)[0]
            else:
                warnings.warn("{} is not in vocab list, so it is skipped.".format(w))
                pass
        out = ''.join([self.vocab.idx_to_token[id] for id in out])
        return out
    
    def _get_results(self, pred):
        pred = pred[:, :, np.newaxis] if len(pred.shape) == 2 else pred
        pred = np.transpose(pred, [0, 2, 1])
        outs = []
        for beam in pred[0]:
            id_list = post_process_seq(beam, self.bos_id, self.eos_id)
            outs.append(id_list)
        return outs
# 载入预测模型
from seq2seq.models import seq2seqinfermodel
import paddle

encoder_attrs = {"vocab_size": vocab_size, "embed_dim": 200, "hidden_size": 128, "num_layers": 4, "dropout": .2,
                    "direction": "bidirectional", "mode": "gru"}
decoder_attrs = {"vocab_size": vocab_size, "embed_dim": 200, "hidden_size": 128, "num_layers": 4, "direction": "forward",
                    "dropout": .2, "mode": "gru", "use_attention": true}

infer_model = paddle.model(seq2seqinfermodel(encoder_attrs,
                                             decoder_attrs,
                                             bos_id=vocab.token_to_idx[vocab.bos_token],
                                             eos_id=vocab.token_to_idx[vocab.eos_token],
                                             beam_size=10,
                                             max_out_len=256))
infer_model.load("./final_models/model")
# 送新年祝福
# 当然,表白也可以
generator = genpoems(vocab, infer_model)

content = "生龙活虎"
poet = generator.gen(content)
for line in poet.strip().split('。'):
    try:
        print("{}\t{}。".format(line[0], line))
    except:
        pass

输出结果

生    生涯不可见,何处不相逢。
龙    龙虎不知何处,人间不见人间。
活    活人不是人间事,不觉人间不可识。
虎    虎豹相逢不可寻,不知何处不相识。

总结

这个项目介绍了如何训练一个生成藏头诗的模型,从结果可以看出,模型已经具有一定的生成诗句的能力。但是,限于训练集规模和训练时间,生成的诗句还有很大的改进空间,未来还将进一步优化这个模型,敬请期待。

以上就是python paddlenlp实现自动生成虎年藏头诗的详细内容,更多关于paddlenlp生成藏头诗的资料请关注其它相关文章!