A poetry generation method and system based on conditional variational autoencoder

By using a conditional variational autoencoder-based method and a bidirectional GRU recurrent neural network to generate classical poetry, the problems of over-translation and repeated translation are solved, high-quality poetry is generated, and the rhythmicity and emotional expression of the poetry are maintained.

CN114510947BActive Publication Date: 2025-09-23ZHEJIANG UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202210085516.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-01-25
Publication Date
2025-09-23
Estimated Expiration
2042-01-25

AI Technical Summary

Technical Problem

Existing technologies have problems with over-translation or repeated translation when generating classical poetry, resulting in a mismatch in the number of characters in the generated poetry, a large gap in semantic information, and difficulty in maintaining the rhythm and beauty of the poetry.

Method used

Through a method based on conditional variational autoencoder, the semantic information of keywords is expanded, and the semantic vector of the poetry library is generated using a pre-trained bidirectional GRU recurrent neural network. The similarity is calculated and candidate poems are sampled. The hidden layer variables are calculated by combining the full probability formula and the heavy parameter technique to generate high-quality poems.

Benefits of technology

It improves the fluency and beauty of generated poetry, and can quickly generate high-quality poetry that meets rhythmic and style requirements based on keywords.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114510947B_ABST
    Figure CN114510947B_ABST
Patent Text Reader

Abstract

The present invention discloses a poetry generation method based on a conditional variational autoencoder, comprising: S1 obtaining keywords and generating corresponding keyword semantic vectors, wherein the keywords include the theme information of the poem to be generated; S2 sampling the keyword semantic vectors obtained in S1 and the semantic vectors stored in a poetry library to generate a candidate set including multiple poems; S3 calculating and obtaining hidden variables based on the poem semantic vectors and the keyword semantic vectors in the candidate set; S4 generating poetry based on the keyword semantic vectors, context semantic vectors, and the hidden variables obtained in S3. The present invention also discloses a poetry generation system based on the above method. The method of the present invention can avoid the problem of over-translation, and obtain high-quality poetry with smooth sentences and a certain style by expanding the semantic information of keywords.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of deep learning and natural language processing, and specifically to a poetry generation method and system based on a conditional variational autoencoder. Background Art

[0002] Classical Chinese poetry is an integral part of China's cultural heritage. It is used in all aspects of daily life, for example, as a means of expressing personal feelings, political views, and conveying messages at festivals and funerals. The creation of classical Chinese poetry must meet a series of structural, tonal, rhyme, and semantic requirements. These stringent restrictions make writing poetry in classical China a daunting task. For example, the quatrain consists of four lines, each containing five to seven characters. Characters follow specific tonal and rhyme patterns within and between lines. For example, the last character of the second, fourth, and optional first lines must rhyme, while the third line is not subject to rhyme restrictions. Furthermore, poetry must adhere to a prescribed tonal pattern. In Chinese, each character is assigned a tone, either ping (horizontal tone) or ze (downward tone). For example, a common tonal pattern in the five-character quatrain is "*ZPPZ, PPZZP, *PPZZ, *ZZPP," where * indicates that any tone is acceptable. In addition to adhering to these formal standards, poetry must demonstrate concise and accurate use of language and express emotion.

[0003] However, due to a lack of professional training, most people lack the ability to express rich emotions within texts of limited length and restricted format. Therefore, research on how to automatically generate poetry using computers has attracted increasing research attention. Current poetry generation methods generate poetry based on user-provided keywords. Keywords consist of a limited number of characters or words, typically 2-6. However, since the number of characters in the generated poem is more than twice that of the keyword, this significant disparity in character count leads to a significant gap in semantic information between the model input and output, which in turn creates an over-translation problem for poetry generation models: certain characters or words appear meaninglessly consecutively, typically twice. Reduplication, a common technique in poetry creation, manifests in the same way as over-translation problems and cannot be simply distinguished based on grammatical rules. Reduplication can enhance the rhythm and sense of rhythm in poetry. Simply limiting the number of character or word repetitions during the model generation process, while alleviating the over-translation problem, can also lead to the loss of reduplication, reducing the aesthetic quality of the poem.

[0004] Patent document CN108563622B discloses a method and device for generating quatrains with diverse styles. The method includes obtaining the first sentence of a quatrain to be generated, converting the text sequence of the first sentence into a first sentence vector, and inputting the first vector obtained by concatenating the first sentence vector with a one-hot vector representing the quatrain's style into a pre-established quatrain generation model to generate multiple quatrains with different styles. This invention requires the provision of specific verse content, which increases the operator's workload and reduces the efficiency of poem generation.

[0005] Patent document CN109582952A discloses a poetry generation method, apparatus, computer equipment, and medium, including a poetry generation model based on a conditional variational autoencoder (CVAE). The poetry generation model parses the poem description text to obtain a text feature vector, and obtains at least a first verse based on the text feature vector of the poem description text; determines a context vector based on the text feature vector of the poem description text and the verse feature vector of the first verse; obtains a prior distribution and a verse latent variable vector based on the context vector; and generates a second verse based on the verse latent variable vector and the context vector, thereby obtaining poetry. This method uses a traditional conditional variational autoencoder algorithm, which is only applicable to multi-character text for poetry generation, and the generated poetry still has the problem of repeated words. Summary of the Invention

[0006] In order to solve the above problems, the present invention provides a poetry generation method based on conditional variational autoencoder, which avoids the problems of over-translation or repeated translation by expanding the semantic information scale of keywords, thereby generating high-quality poetry with smooth sentences and a certain style.

[0007] A poetry generation method based on conditional variational autoencoder, comprising:

[0008] S1 obtains keywords and generates corresponding keyword semantic vectors, wherein the keywords include the theme information of the poem to be generated;

[0009] S2 samples the keyword semantic vectors obtained in S1 and the semantic vectors of poems in the poetry library to generate a candidate set including multiple poems;

[0010] S3 calculates and obtains hidden layer variables based on the poem semantic vector and the keyword semantic vector in the candidate set;

[0011] S4 generates poetry based on the keyword semantic vector, context semantic vector and the latent variables obtained in S3.

[0012] Preferably, the poetry library: a certain amount of poems and corresponding keywords collected in advance are encoded using a bidirectional GRU recurrent neural network to obtain semantic vectors corresponding to the poems, which are stored in the poetry library.

[0013] Preferably, the S2 is specifically:

[0014] S2.1 calculates the similarity between the input keyword semantic vector and the semantic vector stored in the poetry library: use the pre-trained GRU recurrent neural network to extract the semantic vectors of the keywords corresponding to all poems in the poetry library, and extract the vector of the user-provided keyword, and calculate the similarity between the two using the cosine similarity indicator;

[0015] S2.2 Based on similarity, a certain amount of poems are selected as candidate sets: based on the similarity, the sampling probability distribution is calculated through the softmax function, and a random number generator is used to generate a random number K between (0,1), and K poems that meet the sampling probability distribution are collected as candidate poetry sets.

[0016] Preferably, the specific process of S3 is:

[0017] S3.1 calculates the posterior probability distribution of the poems in the candidate set using the full probability formula;

[0018] S3.2 Calculate the expectation and variance of the posterior probability distribution of the poems in the candidate set;

[0019] S3.3 Calculate the prior probability distribution of the poems in the candidate set based on the expectation and variance of the posterior probability distribution of the poems;

[0020] S3.4 calculates the prior probability distribution according to S3.3 and uses the reparameterization technique to calculate the hidden layer variables.

[0021] Preferably, the specific process of generating poetry in S4 is:

[0022] S4.1 Initially set the context semantic vector to 0;

[0023] S4.2 generates the current line of poetry by mapping the context semantic vector, keyword semantic vector and latent variables into a high-dimensional abstract semantic space;

[0024] S4.3 through the one-dimensional convolution kernel conv 1*d Convolve with the current generated poem line to obtain a new contextual semantic vector;

[0025] S4.4 repeats S4.2 to obtain the final poem based on the contextual semantic vector obtained in S4.3.

[0026] The present invention also discloses a system based on the above-mentioned poetry generation method. The system is simple to operate and only requires the input of a small number of keywords to quickly generate poems with smooth sentences and a certain style.

[0027] A system for a poetry generation method, comprising:

[0028] Input module, used to vectorize the acquired keywords;

[0029] The poetry library module samples a certain amount of relevant poems from the poetry library based on the keyword semantic vector;

[0030] The prior probability calculation module calculates the prior probability distribution of poems based on related poems in the poetry library to supplement semantic information for keywords;

[0031] The posterior probability calculation module is used to correct the prior probability distribution of the relevant poems in the prior probability calculation module;

[0032] The poetry generation module generates poetry based on the semantic information supplemented by the prior probability calculation module.

[0033] Preferably, the system is built based on a conditional variational autoencoder and trained through a bidirectional GRU recurrent neural network.

[0034] Preferably, the correction is to evaluate the similarity between the prior probability distribution and the posterior probability distribution through the KL divergence function, and to update the probability distribution of the prior probability calculation module and the posterior probability calculation module using the back propagation algorithm.

[0035] Compared with the prior art, the present invention has the following beneficial effects:

[0036] (1) Based on sampling and the prior probability distribution of poems related to keywords, the semantic information scale of keywords is expanded, thereby overcoming the over-translation problem and improving the fluency and beauty of generated poems.

[0037] (2) The resources in the poetry library can be adjusted and replaced to control the generated poetry to present different styles and express different emotional tendencies. BRIEF DESCRIPTION OF THE DRAWINGS

[0038] Figure 1 A schematic diagram of the flow chart of the poetry generation method provided by the present invention;

[0039] Figure 2 This is a schematic structural diagram of the system provided by the present invention. DETAILED DESCRIPTION

[0040] Pre-construct a poetry library: collect 1,000 poems from the Song Dynasty to the Ming Dynasty, use a bidirectional GRU recurrent neural network to encode them, obtain semantic vectors corresponding to the poems, and store them in the poetry library.

[0041] like Figure 1 As shown in Figure 1, a poetry generation method based on conditional variational autoencoder:

[0042] S1 obtains keywords, including the theme information of the poem to be generated, and generates corresponding keyword semantic vectors;

[0043] S2 first calculates the similarity between the input keyword semantic vector and the semantic vector stored in the poetry library: it uses the pre-trained GRU recurrent neural network to extract the semantic vectors of the keywords corresponding to all poems in the poetry library, and extracts the vectors of the user-provided keywords, and calculates the similarity between the two using the cosine similarity indicator:

[0044] s=vU T

[0045] Where s is the similarity, v is the keyword semantic vector, and U is the semantic vector stored in the poetry library.

[0046] Based on the similarity, the sampling probability distribution is calculated by the softmax function:

[0047] P = softmax(s)

[0048] Where p is the sampling probability distribution, and a random number generator is used to generate a random number k between (0,1), and k poems that meet the sampling probability distribution are collected as the candidate poetry set.

[0049] S3 calculates the prior probability distribution of poems based on the candidate poem set obtained by sampling using the full probability formula:

[0050]

[0051] q y (z|c) is the prior probability distribution, p(z|c,r) is the posterior probability distribution of the candidate poetry set, r is the candidate poetry set, k is the number of poems in the candidate poetry set, and the method of obtaining the posterior probability distribution belongs to the existing technology and will not be repeated here.

[0052] q y (z|c) is regarded as a Gaussian mixture model, and an independent Gaussian distribution density function q(z|c) is found so that the difference between the two can be sufficiently small, that is, the KL divergence of the two can be guaranteed to be small enough:

[0053] KL(q y (z|c)|q(z|c))≤K(c,r)

[0054] Since KL(q y(z|c)|q(z|c)) is difficult to calculate, that is, KL(q y (z|c)|q(z|c)), that is, the calculation process:

[0055]

[0056] Assume that the expectation and standard deviation of q(z|c,x) are μ and σ respectively, and the expectation and variance of p(z|c,r) are μ r and The dimension of the hidden layer variable is J, which can be derived as follows:

[0057]

[0058] μ j and σ j Find the extreme point:

[0059]

[0060]

[0061] Assuming μ r,j The domain of is (a,b), The domain of is (c, d), and substituting the above extreme points into K(c, r), we can obtain the following inequality:

[0062]

[0063] Finally, by setting appropriate hyperparameters j, a, b, c, and d, this method can obtain a Gaussian probability density function q(z|c), which is consistent with the target probability density function q y The KL divergence of (z|c) is less than a small constant upper bound.

[0064] According to the above process, the expectation and standard deviation corresponding to each candidate poem are calculated.

[0065] Based on the expectation and standard deviation corresponding to each poem in the candidate poems, the prior probability distribution of the poems is calculated, and the hidden variables are obtained using the re-parameterization technique.

[0066] The parameter resetting technique is an existing conventional technical means, and its process will not be described in detail.

[0067] S4 generates poetry based on the keyword semantic vector, context semantic vector, and the latent variables obtained in S3:

[0068] S4.1 Initially set the context semantic vector to 0;

[0069] S4.2 generates the current line of poetry by mapping the initial context semantic vector, keyword semantic vector and latent variables into a high-dimensional abstract semantic space;

[0070] S4.3 through the one-dimensional convolution kernel conv 1*d Convolve with the current generated poem line to obtain a new contextual semantic vector;

[0071] S4.4 repeats S4.2 to obtain the final poem based on the contextual semantic vector obtained in S4.3.

[0072] like Figure 2 The system is based on a conditional variational autoencoder and is trained through a bidirectional GRU recurrent neural network to obtain a poetry generation method, including:

[0073] Input module, used to vectorize the acquired keywords;

[0074] The poetry library module extracts a certain amount of relevant poems from the poetry library according to sampling requirements;

[0075] The prior probability calculation module calculates the prior probability distribution of poems based on related poems in the poetry library and supplements the semantic information of keywords;

[0076] The posterior probability calculation module is used to correct the prior probability distribution of the relevant poems in the prior probability calculation module;

[0077] The poetry generation module generates poetry based on the semantic information supplemented by the prior probability calculation module.

[0078] The poetry generation module consists of a contextual semantic network and a line generation network. The contextual semantic network modeling has generated the semantic expression of all lines of poetry. The line generation network maps the contextual semantic vector, keyword semantic vector and hidden layer variables to a high-dimensional abstract semantic space to generate the current line of poetry.

[0079] The contextual semantic generation network uses a one-dimensional convolution kernel conv 1*d Convolve it with the current generated poem line to get a new contextual semantic expression.

[0080] The correction is to use the KL divergence function to evaluate the similarity between the prior probability distribution and the posterior probability distribution, and use the back propagation algorithm to update the parameters of the prior probability calculation module and the posterior probability calculation module.

[0081] This system also uses cyclic KL annealing technology to prevent the prior probability and posterior probability from being too similar: the weight of the KL term is set to 0 in the initial stage of training; the weight of the KL term is continuously increased as the training process progresses; when the weight of the KL term increases to 1, the weight of the KL term is gradually reduced; and the above steps are repeated until the model converges.

Claims

1. A poetry generation method based on conditional variational autoencoder, characterized in that: include: S1 obtains keywords and generates corresponding keyword semantic vectors. The keywords include the theme information of the poem to be generated. The poetry library is encoded by a certain amount of poems and corresponding keywords collected in advance using a bidirectional GRU recurrent neural network to obtain the semantic vectors corresponding to the poems, which are stored in the poetry library. S2 samples the keyword semantic vectors obtained in S1 and the semantic vectors of poems in the poetry library to obtain a candidate set including multiple poems. The specific process is as follows: S2.1 calculates the similarity between the input keyword semantic vector and the semantic vector of the poems in the poetry library. It uses the pre-trained GRU recurrent neural network to extract the semantic vectors of the keywords corresponding to all poems in the poetry library, and extracts the vector of the user-provided keyword. The similarity between the two is calculated using the cosine similarity indicator: s = vU T Where s is the similarity, v is the keyword semantic vector, and U is the semantic vector stored in the poetry library; S2.2 Based on similarity, a certain amount of poems are sampled as candidate sets, and the sampling probability distribution is calculated using the softmax function: P = softmax(s) Where p is the sampling probability distribution, and a random number generator is used to generate a random number k between (0, 1), and k poems that meet the sampling probability distribution are collected as the candidate poetry set; S3 calculates and obtains hidden layer variables based on the poem semantic vector and the keyword semantic vector in the candidate set. The specific process is as follows: S3.1 calculates the posterior probability distribution of the poems in the candidate set using the full probability formula; S3.2 Calculate the expectation and variance of the posterior probability distribution of the poems in the candidate set; S3.3 Calculate the prior probability distribution of the poems in the candidate set based on the expectation and variance of the posterior probability distribution of the poems; S3.4 Calculate the prior probability distribution according to S3.3 and use the reparameterization technique to calculate the hidden layer variables; S4 generates poetry based on the keyword semantic vector, context semantic vector, and the hidden variables obtained in S3. The specific process is as follows: S4.1 Initially set the context semantic vector to 0; S4.2 generates the current line of poetry by mapping the context semantic vector, keyword vector and hidden variables into a high-dimensional abstract semantic space; S4.3 through the one-dimensional convolution kernel conv 1*d Convolve with the current generated poem line to obtain a new contextual semantic vector; S4.4 repeats S4.2 to obtain the final poem based on the contextual semantic vector obtained in S4.

3.

2. A system for implementing the poetry generation method of claim 1, comprising: Input module, used to vectorize the acquired keywords; The poetry library module samples a certain amount of relevant poems from the poetry library based on the keyword semantic vector; The prior probability calculation module calculates the prior probability distribution of poems based on related poems in the poetry library to supplement semantic information for keywords; The posterior probability calculation module is used to correct the prior probability distribution of the relevant poems in the prior probability calculation module; The poetry generation module generates poetry based on the semantic information supplemented by the prior probability calculation module.

3. The system according to claim 2, characterized in that The system is built based on a conditional variational autoencoder and trained through a bidirectional GRU recurrent neural network.

4. The system according to claim 2, wherein: The correction is to evaluate the similarity between the prior probability distribution and the posterior probability distribution by using the KL divergence function, and to update the probability distribution of the prior probability calculation module and the posterior probability calculation module by using the back propagation algorithm.

Citation Information

Patent Citations

  • A method and apparatus for generating quatrains with diverse styles

    CN108563622B

  • Poem generation method and device, computer equipment and medium

    CN109582952A