A sentiment controllable poem generation method based on an improved loss function transformer model
By improving the loss function of the Transformer model and combining the rhyme matrix and penalty mechanism, the problem of unclear rhyme control in existing technologies is solved, and high-quality rhyming and emotionally controllable poetry generation is achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- NANJING UNIV OF SCI & TECH
- Filing Date
- 2023-12-27
- Publication Date
- 2026-07-24
AI Technical Summary
Existing poetry generation models have shortcomings in format control and emotional expression, especially in rhyme control, which is not clear enough and makes it difficult to improve the quality of poetry generation at the same time.
We employ a Transformer model with an improved loss function, transforming the decoded vector into a rhyme vector using a rhyme matrix. This is combined with loss functions related to poetry generation and rhyme, and beam search and a penalty mechanism are used during model training to ensure that the generated poems achieve higher standards in rhyme rate and emotional accuracy.
The generated poems have a rhyme rate of over 90%, while the emotional expression is more accurate, improving the literary quality and format control of the poems, and achieving high-quality poetry generation.
Smart Images

Figure CN117807991B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of text generation technology, and in particular relates to an emotion-controlled poetry generation method based on an improved loss function Transformer model. Background Technology
[0002] Introduction to Existing Poetry Generation Models: Poetry generation is a challenging text generation task, the difficulty of which lies in generating a text that is both formally structured and rich in meaning. There is already considerable research on existing poetry generation models. Traditional methods include word salads and genetic algorithms; starting with machine learning, RNN models are used for sequence generation, while also incorporating word association similar to human outline writing. The most representative example is the Nine Songs series model researched by Tsinghua University; and with the introduction of Transformers, poetry generation has been extended beyond fixed sequence lengths, for example, the SongNet model can generate high-quality Song Dynasty poems.
[0003] Existing poetry generation model solutions: In RNN model-related research, the focus is on how to mimic human associations with a theme, and then formulate a poem outline to create a poem; while in the research on poetry generation based on the Transformer model, the controllability of the format is further solved, and a larger model such as GPT-2 is used, plus implicit format control in the embedding layer, so that the generated poems are better in terms of format control.
[0004] Existing poetry generation models suffer from several problems: Because formatting controls are embedded as part of the label within the embedding layer, and the learning process involves the embedding layer incorporating too much information, the learning objective is not clearly defined. Therefore, there is room for improvement in aspects such as rhyme control. Furthermore, the aforementioned poetry generation models focus on improving only one aspect, often neglecting the potential for a comprehensive approach to improving the overall quality of generated poetry. Summary of the Invention
[0005] The purpose of this invention is to solve the problems existing in the above-mentioned background technology and to provide a method for generating poetry with controllable emotion based on an improved loss function Transformer model.
[0006] To achieve the objectives of this invention, a method for generating emotion-controlled poetry based on an improved Transformer loss function model is disclosed, comprising the following steps:
[0007] Step 1: Collect publicly available datasets, filter and refine the datasets, and process them to obtain the required labels;
[0008] Step 2: Input the sentiment tags and poem data into the Transformer encoder, and decode using a Linear layer neural network to obtain the decoded vector;
[0009] Step 3: During the model training process, transform the decoded vector into a prosody vector through a designed rhyme matrix. When performing gradient descent, not only calculate the loss function related to poem generation for the decoded vector, but also calculate the loss function related to rhyming for the prosody vector;
[0010] Step 4: During the model inference process, use the beam search method combined with heuristic methods such as repetition penalty and endorsement penalty to calculate the poem with the highest possibility of generation to improve the quality of poem generation;
[0011] Step 5: Input the prompt sentiment word into the deployed model and perform inference to obtain a newly generated poem corresponding to the input sentiment.
[0012] Further, the rhyme matrix in Step 3 is A n,m+1 , where n represents the dictionary size and m represents the number of prosody types; for the i-th row and j-th column of the matrix, if the word numbered i contains the prosody numbered j, then the value here in the matrix is set to 1; if the word numbered i does not have any corresponding prosody, then a value of 1 will be set at the position of the i-th row and the (m + 1)-th column of the matrix; except for the above two cases, other positions are set to 0.
[0013] Further, the dimension of the prosody vector in Step 3 is m + 1, and it is obtained by multiplying the rhyme matrix and the decoded vector.
[0014] Further, the model training process in Step 3 involves the loss function L1 related to poem generation and the loss function L2 related to rhyming; the total loss value required for gradient descent is calculated based on these two, and the calculation formula is: L = (1 - α)L1 + αL2.
[0015] Further, through a large number of experiments, the best value for α is between 0.3 and 0.4.
[0016] Further, the repetition penalty in Step 4 is to reduce the probability that the model generates the same word more than 2 times continuously; if the word numbered x appears at both position i and position j in the generated poem, and i + 1 < j, then the probability of this poem will be multiplied by a penalty factor to reduce its output probability.
[0017] Further, the endorsement penalty in Step 4 uses a verse matching algorithm to reduce the probability that the model outputs the verses that appear in the training set.
[0018] Compared with the prior art, the significant advancements of this invention are: 1) proposing a rhyme matrix to transform the decoding vector into a rhyming vector to obtain a rhyme-related loss function; 2) by merging the gradient descent of the two loss functions, the poems generated by the model have a high rhyme rate while ensuring quality; 3) by combining sentiment tags, the literary quality of the poems is further improved while ensuring the control of the poem format.
[0019] To more clearly illustrate the functional characteristics and structural parameters of the present invention, further explanation is provided below in conjunction with the accompanying drawings and specific embodiments. Attached Figure Description
[0020] The accompanying drawings, which are included to provide a further understanding of the invention and form part of this application, illustrate exemplary embodiments of the invention and, together with their description, serve to explain the invention and do not constitute an undue limitation thereof. In the drawings:
[0021] Figure 1 This is a schematic diagram of the overall architecture of the model.
[0022] Figure 2 This is a basic analytical diagram of poetry quality. Detailed Implementation
[0023] The technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0024] A sentiment-controlled poetry generation method based on an improved loss function Transformer model includes the following steps:
[0025] Step 1: Collect publicly available datasets, filter and refine the datasets, and process them to obtain the required labels;
[0026] Specifically, in step 1, a semi-supervised learning method was used to obtain the sentiment labels. Since there are relatively few sentiment labels labeled by humans, it is necessary to first train a sentiment classifier based on this, and then further label data with high confidence, iterate the training, and finally complete the labeling of the poems in the training set.
[0027] Step 2: Input the sentiment tags and poetry data into the Transformer encoder, and decode them using a Linear layer neural network to obtain the decoded vector;
[0028] Specifically, step 2 involves a specific model as follows: Figure 1As shown in the figure, first, three types of Embedding encodings are performed on the original poem, namely: Positional Embedding associated with positions, Word Embedding that compresses text information, and Tone Embedding related to rhythm after converting each character into prosody information. After adding the three types of Embedding, it is input into the Transformer encoder and then decoded using a Linear layer.
[0029] Step 3: During the model training process, the decoded vector is transformed into a rhythm vector through a designed character-rhyme matrix. When performing gradient descent, not only the loss function related to poem generation is calculated for the decoded vector, but also the loss function related to rhyming is calculated for the rhythm vector.
[0030] Specifically, the dimension of the rhythm vector in Step 3 is m + 1, which is obtained by multiplying the decoded vector by the character-rhyme matrix. The model training process involves the loss function L1 related to poem generation, which uses the cross-entropy loss function and is Figure 1 shown as Cross Entropy Loss, and the loss function L2 related to rhyming, which uses the multi-class cross-entropy loss function and is Figure 1 shown as Rhythm Error Loss. The total loss value required for gradient descent is calculated based on these two, and the calculation formula is: L = (1 - α)L1 + αL2. Through a large number of experiments, taking the percentage value of α as the horizontal axis and various experimental indicators as the vertical axis, a graph is plotted Figure 2 and it is observed that the best value range of α is between 0.3 and 0.4.
[0031] For the experiment on the hyperparameter α value, we used the proportion of poems that meet the conditions among all the generated poems to judge the selection of the α value. Among them, the rhyming rate and the pair adhesion rate describe the strictness of the format of a poem, the emotional accuracy is obtained by using the above-mentioned emotion classifier to classify and judge the generated poem, and the perplexity describes the degree to which the generated poem is considered unlikely to appear in the eyes of the observer. Here, the selected observer is the Chinese open-source large model ChatGLM.
[0032] Step 4: During the model inference process, the beam search method is used, combined with heuristic methods such as repetition penalty and endorsement penalty, to calculate the most likely generated poem to improve the quality of poem generation.
[0033] Specifically, the repetition penalty described in Step 4 is to reduce the probability that the model generates the same character more than twice consecutively. If the character numbered x appears at positions i and j in the generated poem, and i + 1 < j, then the probability of this poem will be multiplied by a penalty factor to reduce its output probability. The endorsement penalty uses a poem matching algorithm to reduce the probability that the model outputs the poems that appear in the training set.
[0034] Step 5: Input the emotional prompts into the deployment model and perform inference to obtain a newly generated poem corresponding to the input emotional prompts.
[0035] Specifically, in terms of model parameter settings, the Embedding layer of the Transformer decoder has a dimension of 200, the output hidden layer has a dimension of 200, the number of multi-head attention layers is 2, and the number of layers is 2. Overall, it is a lightweight model.
[0036] To determine the effectiveness of the method, we conducted ablation experiments. The rhyme loss function was set using the optimal α value range obtained from the above experiments. We also invited volunteers to conduct a human-based poetry quality assessment. The following table shows the evaluation dimensions:
[0037] Note: 5 points is the highest, 1 point is the lowest fluent: The poetry reads fluently and is formatted correctly. coherent: The poem's theme and meaning remain consistent throughout. Meaning: Poetry conveys a clear message poetry: It has the characteristics of poetry when read.
[0038] The experiment randomly selected 100 poems, dividing them into 25 poems based on whether they rhymed and 25 poems based on whether they conveyed emotion. The table below shows the human evaluation of the poem quality:
[0039] category fluent coherent Meaning poetry No rhyme, no emotion 3.27 3.40 3.32 3.33 Unrhymed but emotional 3.63 3.80 3.70 3.85 It rhymes but lacks emotion. 3.35 3.45 3.52 3.63 It has rhyme and emotion 3.68 3.77 3.73 3.87
[0040] Experiments show that the rhyme loss function can achieve a rhyme rate of over 90% in the generated poems, while the addition of sentiment cue words can significantly improve the quality of the generated poems, proving the effectiveness of the above methods. Furthermore, this model is lightweight, possessing greater potential in the current era of burgeoning large-scale models.
[0041] It should be noted that, in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article, or apparatus.
[0042] Although embodiments of the invention have been shown and described, it will be understood by those skilled in the art that various changes, modifications, substitutions and alterations can be made to these embodiments without departing from the principles and spirit of the invention, the scope of which is defined by the appended claims and their equivalents.
Claims
1. A method for generating emotion-controlled poetry based on an improved Transformer model with an improved loss function, characterized in that, It includes the following steps: Step 1: Collect a public dataset, screen and refine the dataset, and process it to obtain the required labels; Step 2: Input the sentiment labels and poem data into a Transformer encoder, and decode them using a Linear layer neural network to obtain a decoded vector; Step 3: During model training, the decoded vectors are transformed into prosody vectors using a pre-designed rhyme matrix. During gradient descent, not only are loss functions related to poetry generation calculated for the decoded vectors, but also loss functions related to rhyme are calculated for the prosody vectors. The model training process involves loss functions related to poetry generation. The cross-entropy loss function is used; the rhyme-related loss function is used. The multi-class cross-entropy loss function is used. Step 4: During the model inference process, use the beam search method combined with the repetition penalty or endorsement penalty heuristic method to calculate the most likely generated poem to improve the poem generation quality; Step 5: Input a prompt sentiment word into the deployed model and perform inference to obtain a newly generated poem corresponding to the input sentiment; The rhyme matrix described in Step 3 is An,m+1, where n represents the dictionary size and m represents the number of rhyme types; for the i-th row and j-th column of the matrix, if the word numbered i contains the rhyme numbered j, then the value at this position in the matrix is set to 1; if the word numbered i does not have any corresponding rhymes, then 1 will be set at the position of the i-th row and the m+1-th column of the matrix; except for the above two cases, other positions are set to 0; The dimension of the rhyme vector described in Step 3 is m+1, which is obtained by multiplying the rhyme matrix by the decoded vector.
2. The emotion-controllable poetry generation method based on an improved loss function Transformer model according to claim 1, characterized in that, The model training process described in step 3 involves loss functions related to poetry generation. loss function related to rhyme The total loss required for gradient descent is calculated from both, and the formula is as follows: .
3. The emotion-controllable poetry generation method based on an improved loss function Transformer model according to claim 2, characterized in that, The value is between 0.3 and 0.
4.
4. The emotion-controllable poetry generation method based on an improved loss function Transformer model according to claim 1, characterized in that, The repetition penalty described in Step 4 is to reduce the probability that the model generates the same word more than 2 times consecutively; if the word numbered x appears at both position i and position j in the generated poem, and i+1<j, then the probability of this poem will be multiplied by a penalty factor to reduce its output probability.
5. The emotion-controllable poetry generation method based on an improved loss function Transformer model according to claim 1, characterized in that, The endorsement penalty described in Step 4 uses a verse matching algorithm to reduce the probability that the model outputs verses that appear in the training set.