Unsupervised text sentiment conversion method and device based on generation prompt
By adopting a two-stage training method based on generated prompts, the problem of unstable model training in unsupervised text sentiment transformation is solved, and the text retention and sentiment transformation are improved, resulting in more accurate and fluent sentences.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- KUNMING UNIV OF SCI & TECH
- Filing Date
- 2023-09-20
- Publication Date
- 2026-07-21
AI Technical Summary
Existing unsupervised text sentiment transformation methods suffer from unstable model training and performance degradation due to the lack of supervision signals, making it difficult to generate accurate, fluent sentences with opposite sentiments.
A two-stage training method based on generative prompts is adopted. First, content prompt vectors are constructed through self-supervised training. Then, recurrent reconstruction training is carried out through text reconstruction loss and sentiment classification loss. Combined with the generative prompter and the pre-trained GPT-2 model, accurate and fluent sentences are generated.
It improves metrics such as text retention, sentiment conversion accuracy, and BLEu, resulting in generated sentences that better match the target sentiment. The model's convergence and generation quality are significantly improved.
Smart Images

Figure CN117273010B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to an unsupervised text sentiment transformation method based on generated prompts, belonging to the field of natural language processing technology. Background Technology
[0002] Text sentiment transformation is an important task in the field of natural language processing. It aims to alter the sentiment attributes of text through editing or generation methods while preserving the text's content. Figure 1 The diagram shows an example of an unsupervised text sentiment transformation method based on cyclic reconstruction. The model takes a positive sentiment sentence (this sandwich is delicious) as input and automatically generates a negative sentiment sentence (this sandwich is terrible). It plays an important role in sentiment analysis, social media analysis, and sentiment text generation.
[0003] Currently, deep learning-based text sentiment transformation tasks require constructing large-scale training data with similar content but opposite sentiments. However, manually labeling large-scale text sentiment transformation data is difficult. Current research mainly focuses on unsupervised text sentiment transformation methods, which fall into two categories: edit-based methods and generative methods. Edit-based methods primarily achieve sentiment transformation by replacing sentiment words. They first identify sentiment words based on word frequency statistics or attention mechanisms, and then use a retrieval-replacement method to complete the sentiment transformation. However, directly replacing sentiment words does not consider contextual information, easily leading to errors such as inappropriate collocation. For example, replacing the sentiment word "disgusting" in the negative sentiment sentence "the place was disgusting and hot in there" to generate the positive sentence "the place was so delicious and hot in there" is problematic because using "delicious" to modify the location is clearly inappropriate. Generative methods, on the other hand, mainly utilize an encoder-decoder framework to generate text with opposite sentiments. The encoder first represents the sentence in a high-dimensional space, and by inserting sentiment vectors into this high-dimensional space, separates the sentence content and sentiment attributes. Finally, text sentiment transformation is achieved through sentence self-encoding reconstruction and sentiment classification loss. However, because unsupervised methods do not represent sentences ideally, it is difficult to separate content from emotion, and the fluency and emotion conversion of generated sentences are not yet ideal.
[0004] In recent years, generative pre-trained language models, such as GPT (Generative Pre-trained Transformer) and GPT-2, have achieved excellent performance in tasks such as text summarization and machine translation, significantly improving the fluency and accuracy of generated content. However, unlike generative tasks such as machine translation, unsupervised sentiment transformation tasks lack labeled corpora and cannot fine-tune pre-trained models through supervised training. The recurrent reconstruction frameworks commonly used in unsupervised generation tasks, due to the lack of explicit supervision signals, result in uneven training gradients and are prone to model degradation. Figure 1 As shown, during the cyclic reconstruction process, the input positive sentiment sentence "this sandwich is delicious" is used to generate an intermediate sentence with negative sentiment using the GPT-2 model. Based on this, the positive sentiment sentence is reconstructed again using the GPT-2 model with shared parameters. However, in the process of generating the intermediate content "this sandy soil is not good", although sentiment conversion can be achieved through sentiment classification loss constraints, due to the lack of intermediate supervision signals, GPT-2 incorrectly generates "sandwich" as "sandy soil". The cyclically generated "sandy hair is very beautiful" causes error accumulation, making it difficult for the weak supervision signal of the final cyclic reconstruction to converge. Summary of the Invention
[0005] The technical problem solved by this invention is: This invention proposes a two-stage unsupervised sentiment conversion method based on cue learning, which is used to solve the problems of unstable training and severe performance degradation of supervised autoencoder models due to lack of supervision signals. The conversion method of this invention improves text retention, sentiment conversion and BLEu metrics.
[0006] The technical solution of the present invention is as follows: In a first aspect, the present invention provides an unsupervised text sentiment transformation method based on generated prompts, the method comprising:
[0007] Step 1: Construct an unsupervised text sentiment transformation model based on generated prompts: By constructing a generated prompter containing text content, the pre-trained model has better text and sentiment understanding capabilities. Then, based on the pre-training, it is trained again through text reconstruction loss and sentiment classification loss, so that the model can generate accurate, fluent sentences with opposite sentiments.
[0008] Step 2: Train an unsupervised text sentiment conversion model based on generated prompts. The training process is divided into two stages: The first stage uses self-supervised training to construct effective content prompt vectors, enabling the model to understand and process the sentiment content of the text; the second stage uses recurrent reconstruction training through reconstruction loss and sentiment classification loss, incorporating sentiment prompts to better understand and process the sentiment conversion task, thereby generating more accurate and natural sentences that conform to the target sentiment.
[0009] Step 3: Unsupervised text sentiment transformation based on generated prompts: Set the relevant parameters of the model input to achieve the purpose of batch processing of text, thereby improving the speed of the text sentiment transformation model. Then, save the trained neural network model and deploy it on the server to realize the transformation of sentiment attributes in the text.
[0010] As a preferred embodiment of the present invention, the specific steps of Step 1 are as follows:
[0011] Step 1.1: Construct the cue generator, which is a sequence-to-sequence model containing an LSTM layer and an MLP. This model accepts input data x and first performs word embeddings through an embedding layer. Then, through multiple iterations, the LSTM layer processes the embedded data, obtaining the last LSTM state of each sequence in the first iteration. These LSTM states are then processed by the MLP to generate the final cue vector. The final cue vector is a three-dimensional tensor containing the cue vector representations of a batch of sequences at specified time steps.
[0012] Step 1.2: Construct the generator's input format by concatenating the two 3D embedding vectors—the content cue vector and the sentiment cue vector—in the second dimension to generate the final cue vector. The purpose of this is to combine the input content cue and sentiment cue so that they can be provided as joint input to the model for subsequent processing and training.
[0013] Step 1.3: Constructing the decoding form of the pre-trained model. In the pre-trained GPT model, to decode text, we first need to extract the model's embedding layer and change the input from the original discrete text (such as integer-represented input IDs) to a cue vector form. During decoding, past contextual information (past key-value pairs) is used to maintain the previous computational state, thus generating coherent and grammatically correct text.
[0014] Step 1.4: Based on the pre-trained model, further unsupervised training will be conducted. The goal of the training is to enable the model to generate accurate, fluent sentences with opposite sentiments. To achieve this goal, this invention introduces two loss functions: text reconstruction loss and sentiment classification loss.
[0015] As a preferred embodiment of the present invention, the specific steps of step 2 are as follows:
[0016] Step 2.1, Self-Supervised Training: The first stage is self-supervised training, where the text is re-represented using a prompt generator to provide continuous vectors with rich semantic information for the text content prompts in the second stage. The prompt generator is a bidirectional LSTM (Long Short-Term Memory) model, and its output layer is connected to a Multilayer Perceptron (MLP).
[0017] Step 2.2, the second stage is the iterative reconstruction training, which concatenates the sentiment cue vector with the content vector generated by the cue generator to form the final cue. The cue pre-trained language model GPT-2 generates the sequence, thereby specifying the model to generate sentences with specific sentiments.
[0018] As a preferred embodiment of the present invention, in step 2.1:
[0019] Step 2.1.1: Assume the input sequence is (x1, x2, ..., x...). t ), where x t This represents the t-th word in the input sequence, which is also the last word. The hidden state h is obtained by encoding the sentence using BiLSTM. t and memory cells C t :
[0020] h t C t =BiLSTM(x t ,h t-1 C t-1 )
[0021] Step 2.1.2: Pass the hidden state through an MLP layer to obtain the first vector Z1 of the text latent representation.
[0022] Z1=MLP(h t )
[0023] Step 2.1.3: Repeat the above process to obtain the hidden state h at each time step. t+i and memory cells C t+i This yields the text latent representation vector Z corresponding to each time step. i+1 :
[0024] h t+i C t+i =BiLSTM(Z) i ,ht+i-1 C t+i-1 )
[0025] Z i+1 =MLP(h t+i )
[0026] Step 2.1.4: Concatenate the obtained text latent representation vectors of length n to obtain the final text latent representation e. c :
[0027]
[0028] Where [] denotes vector concatenation, which is then concatenated with the emotional vector <|positive|><|bosoftext|> that generates emotional information according to the specified instruction to form the final GPT-2 cue e:
[0029] e=[e c ;e s ]
[0030] Among them, e s This indicates the sentiment vector (e) that indicates the generation of sentiment information. s It is a randomly initialized cue vector used to specify the positive and negative emotional polarities, with a dimension of 2*768;
[0031] Step 2.1.5: Use the final cue vector e as input to the GPT-2 model for training, with a training loss J. rec as follows:
[0032]
[0033] Where e represents the emotional attribute ν src The sentence is generated by prompting the prompt generator. PLM is the sequence generation probability of the model GPTST of this invention, and t is the sequence length of the cyclically reconstructed sentence x′.
[0034] Intuitively, <|positive|> is used to prompt the model to generate sentences with a specific sentiment, and <|bosoftext|> prompts the model to start generating.
[0035] As a preferred embodiment of the present invention, in step 2.2:
[0036] Step 2.2.1: The first step is to construct the emotional reward, assigning the emotional attribute ν. src Taking sentence x as input, the input prompt generator generates a text latent representation vector e containing only text content and no emotional information. c .
[0037] Step 2.2.2: Unlike the input composition in the first stage, <|negative|><|bosoftext|> are used to concatenate the prompts generated by the generator to form the final prompts in the second stage. The sentiment vector <|negative|> with the opposite sentiment polarity is used to prompt the model to generate the sentiment polarity of the sentence.
[0038] Step 2.2.3: Employ reinforcement learning to construct a reward for sentence generation, guiding the model to generate sentences that align with the target emotion ν. tgt The sentence y′ also contains text content. Specifically, in reward construction, GPTST acts as the agent, interacting with a pre-trained classifier as the environment. The pre-trained classifier provides character-level feedback rewards based on the sequences generated by GPTST. Formally, the sentiment reward R and the sentiment classification loss J... s It is represented as:
[0039] R(y′)=-(v tgt log(P(v tgt ∣y′))+(1-v tgt )log(1-P(ν tgt ∣y′)))
[0040] J s =E y′~p(y′) [R(y′)]
[0041] Where P is the probability that the generated sequence matches the target sentiment, as determined by the pre-trained classifier. The parameters of the classifier are fixed during training. p(y′) is the sequence generation probability of y′. The final loss J is obtained by maximizing the expected sentiment reward R. s .
[0042] Step 2.2.4: For the loss in text information preservation, a cyclic reconstruction method is used. In the sentiment transformation task, the two sentences with the different sentiments have the same content information, except for the difference in sentiment information. In other words, the sentence with the target sentiment can be reconstructed from the sentence with the original sentiment. Based on the above, a cyclic reconstruction method is constructed to ensure the construction of the content preservation loss in text sentiment transformation.
[0043] Specifically, the emotional attribute v src Using the sentence x′ as input, generate the target sentiment attribute ν. tgt The sentence y′ is then used as input to generate a loop with the original emotional attribute v. src Given sentence x′, a cyclic reconstruction loss between x and x′ is constructed to ensure the preservation of text content. Formally, the text reconstruction loss J... c It is represented as:
[0044]
[0045] Where x is the emotional attribute ν src The sentence, P LM t represents the sequence generation probability of the unsupervised text sentiment transfer model GPTST of this invention, and t represents the sequence length of the reconstructed sentence x′.
[0046] Step 2.2.5: To encourage the model to improve both text retention and sentiment conversion accuracy simultaneously, the harmonic mean of the two losses is used as the final loss:
[0047]
[0048] Here, α is the harmonic weight, used to adjust the relative importance of text content retention and balance the two losses.
[0049] As a preferred embodiment of the present invention, the specific steps of step 3 are as follows:
[0050] Step 3.1: To achieve batch processing of the text sentiment conversion model, the batch size of the input text is first defined as 64, the length of the generated content prompt vector is set to 5, and the batch size of the model is the batch size of the input text. This enables the model to perform parallel computation on the input text and improves the conversion speed.
[0051] Step 3.2: Save the model as a ".pth" file and load the model into memory using the Flask framework to avoid frequent model loading processes caused by requesting results and improve the running speed of the recognition model;
[0052] Step 3.3: Deploy the model as an API interface using the Flask framework to enable multiple concurrent requests on the web port;
[0053] Step 3.4: On the web, the text sentiment conversion model deployed on the server is called to convert the sentiment attribute of the input text, thereby obtaining the result with the opposite sentiment polarity and displaying it on the front-end interface.
[0054] In a second aspect, the present invention also provides an unsupervised text sentiment conversion apparatus based on generated prompts, the apparatus including a module for performing the method described in the first aspect.
[0055] The beneficial effects of this invention are:
[0056] 1. In the first stage, this invention addresses the difficulty in constructing sentiment conversion cue vectors by designing an effective content cue vector in conjunction with an unsupervised sentiment conversion task. The text content is encoded to obtain the content cue vector, and the cue generator is trained through self-supervised training.
[0057] 2. The prompt generator of this invention transforms prompts from discrete vectors to continuous vectors, enabling the content prompt vectors to contain semantic information and sentiment features of the text. This allows the model to better understand and process the sentiment content of the text, making model convergence easier. Building upon this, a second-stage training for cyclic reconstruction is constructed using reconstruction loss and sentiment classification loss. Due to the pre-training in the first stage, the content prompt vectors possess the semantic information and key features of the text, making convergence of the cyclic reconstruction loss easier. By incorporating sentiment prompts, the model can better understand the sentiment direction of the generated text, thereby handling the sentiment conversion task. Thanks to the pre-training in the first stage, the model can generate more accurate and natural sentences that conform to the target sentiment in the second stage.
[0058] 3. This invention has significant improvements in indicators such as text retention, sentiment conversion, and BLEu. Attached Figure Description
[0059] Figure 1 Example diagram of unsupervised text sentiment transformation method based on loop reconstruction;
[0060] Figure 2 This is a diagram of the two-stage training and model architecture based on generated prompts in this invention;
[0061] Figure 3 This is a line graph showing the results of the harmonic weighting experiment in this invention;
[0062] Figure 4 Provide a vector visualization of the content;
[0063] Figure 5 This is a flowchart of the method in this invention; Detailed Implementation
[0064] Example 1: As Figures 1-5 As shown, an unsupervised text sentiment transformation method based on generated prompts includes:
[0065] Step 1: Construct an unsupervised text sentiment transformation model based on generated prompts: By constructing a generated prompter containing text content, the pre-trained model has better text and sentiment understanding capabilities. Then, based on the pre-training, it is trained again through text reconstruction loss and sentiment classification loss, so that the model can generate accurate, fluent sentences with opposite sentiments.
[0066] Step 2: Train an unsupervised text sentiment conversion model based on generated prompts. The training process is divided into two stages: The first stage uses self-supervised training to construct effective content prompt vectors, enabling the model to understand and process the sentiment content of the text; the second stage uses recurrent reconstruction training through reconstruction loss and sentiment classification loss, incorporating sentiment prompts to better understand and process the sentiment conversion task, thereby generating more accurate and natural sentences that conform to the target sentiment.
[0067] Step 3: Unsupervised text sentiment transformation based on generated prompts: Set the relevant parameters of the model input to achieve the purpose of batch processing of text, thereby improving the speed of the text sentiment transformation model. Then, save the trained neural network model and deploy it on the server to realize the transformation of sentiment attributes in the text.
[0068] As a preferred embodiment of the present invention, the specific steps of Step 1 are as follows:
[0069] Step 1.1: Construct the cue generator, which is a sequence-to-sequence model containing an LSTM layer and an MLP. This model accepts input data x and first performs word embeddings through an embedding layer. Then, through multiple iterations, the LSTM layer processes the embedded data, obtaining the last LSTM state of each sequence in the first iteration. These LSTM states are then processed by the MLP to generate the final cue vector. The final cue vector is a three-dimensional tensor containing the cue vector representations of a batch of sequences at specified time steps.
[0070] Step 1.2: Construct the generator's input format by concatenating the two 3D embedding vectors—the content cue vector and the sentiment cue vector—in the second dimension to generate the final cue vector. The purpose of this is to combine the input content cue and sentiment cue so that they can be provided as joint input to the model for subsequent processing and training.
[0071] Step 1.3: Constructing the decoding form of the pre-trained model. In the pre-trained GPT model, to decode text, we first need to extract the model's embedding layer and change the input from the original discrete text (such as integer-represented input IDs) to a cue vector form. During decoding, past contextual information (past key-value pairs) is used to maintain the previous computational state, thus generating coherent and grammatically correct text.
[0072] Step 1.4: Based on the pre-trained model, further unsupervised training will be conducted. The goal of the training is to enable the model to generate accurate, fluent sentences with opposite sentiments. To achieve this goal, this invention introduces two loss functions: text reconstruction loss and sentiment classification loss.
[0073] As a preferred embodiment of the present invention, the specific steps of step 2 are as follows:
[0074] Step 2.1, Self-Supervised Training: The first stage is self-supervised training, where the text is re-represented using a prompt generator to provide continuous vectors with rich semantic information for the text content prompts in the second stage. The prompt generator is a bidirectional LSTM (Long Short-Term Memory) model, and its output layer is connected to a Multilayer Perceptron (MLP).
[0075] Step 2.2, the second stage is the iterative reconstruction training, which concatenates the sentiment cue vector with the content vector generated by the cue generator to form the final cue. The cue pre-trained language model GPT-2 generates the sequence, thereby specifying the model to generate sentences with specific sentiments.
[0076] As a preferred embodiment of the present invention, in step 2.1:
[0077] Step 2.1.1: Assume the input sequence is (x1, x2, ..., x...). t ), where x t This represents the t-th word in the input sequence, which is also the last word. The hidden state h is obtained by encoding the sentence using BiLSTM. t and memory cells C t :
[0078] h t C t =BiLSTM(x t ,h t-1 C t-1 )
[0079] Step 2.1.2: Pass the hidden state through an MLP layer to obtain the first vector Z1 of the text latent representation.
[0080] Z1=MLP(h t )
[0081] Step 2.1.3: Repeat the above process to obtain the hidden state h at each time step. t+i and memory cells C t+i This yields the text latent representation vector Z corresponding to each time step. i+1 :
[0082] h t+i C t+i =BiLSTM(Z) i ,ht+i-1 C t+i-1 )
[0083] Z i+1 =MLP(h t+i )
[0084] Step 2.1.4: Concatenate the obtained text latent representation vectors of length n to obtain the final text latent representation e. c :
[0085]
[0086] Where [] denotes vector concatenation, which is then concatenated with the emotional vector <|positive|><|bosoftext|> that generates emotional information according to the specified instruction to form the final GPT-2 cue e:
[0087] e = [e c ;e s ]
[0088] Among them, e s This indicates the sentiment vector (e) that indicates the generation of sentiment information. s It is a randomly initialized cue vector used to specify the positive and negative emotional polarities, with a dimension of 2*768;
[0089] Step 2.1.5: Use the final cue vector e as input to the GPT-2 model for training, with a training loss J. rec as follows:
[0090]
[0091] Where e represents the emotional attribute ν src The sentence is generated with hints from the prompt generator, P LM t represents the sequence generation probability of the model GPTST (Generate Prompt Text Sentiment Transfer) of this invention, and t represents the sequence length of the cyclically reconstructed sentence x′.
[0092] Intuitively, <|positive|> is used to prompt the model to generate sentences with a specific sentiment, and <|bosoftext|> prompts the model to start generating.
[0093] As a preferred embodiment of the present invention, in step 2.2:
[0094] Step 2.2.1: The first step is to construct the emotional reward, assigning the emotional attribute ν. src Taking sentence x as input, the input prompt generator generates a text latent representation vector e containing only text content and no emotional information.c .
[0095] Step 2.2.2: Unlike the input composition in the first stage, <|negative|><|bosoftext|> are used to concatenate the prompts generated by the generator to form the final prompts in the second stage. The sentiment vector <|negative|> with the opposite sentiment polarity is used to prompt the model to generate the sentiment polarity of the sentence.
[0096] Step 2.2.3: Employ reinforcement learning to construct a reward for sentence generation, guiding the model to generate sentences that align with the target emotion ν. tgt The sentence y′ also contains text content. Specifically, in reward construction, GPTST acts as the agent, interacting with a pre-trained classifier as the environment. The pre-trained classifier provides character-level feedback rewards based on the sequences generated by GPTST. Formally, the sentiment reward R and the sentiment classification loss J... s It is represented as:
[0097] R(y′)=-(v tgt log(P(v tgt ∣y′))+(1-v tgt )log(1-P(v tgt ∣y′)))
[0098] J s =E y′~p(y′) [R(y′)]
[0099] Where P is the probability that the generated sequence matches the target sentiment, as determined by the pre-trained classifier. The parameters of the classifier are fixed during training. p(y′) is the sequence generation probability of y′. The final loss J is obtained by maximizing the expected sentiment reward R. s .
[0100] Step 2.2.4: For the loss in text information preservation, a cyclic reconstruction method is used. In the sentiment transformation task, the two sentences with the different sentiments have the same content information, except for the difference in sentiment information. In other words, the sentence with the target sentiment can be reconstructed from the sentence with the original sentiment. Based on the above, a cyclic reconstruction method is constructed to ensure the construction of the content preservation loss in text sentiment transformation.
[0101] Specifically, the emotional attribute ν src Using the sentence x′ as input, generate the target sentiment attribute ν. tgt The sentence y′ is then used as input to generate a loop with the original emotional attribute ν. srcGiven sentence x′, a cyclic reconstruction loss between x and x′ is constructed to ensure the preservation of text content. Formally, the text reconstruction loss J... c It is represented as:
[0102]
[0103] Where x is the emotional attribute ν src The sentence, P LM Let t be the sequence generation probability of the model GPTST of this invention, and t be the sequence length of the cyclically reconstructed sentence x′.
[0104] Step 2.2.5: To encourage the model to improve both text retention and sentiment conversion accuracy simultaneously, the harmonic mean of the two losses is used as the final loss:
[0105]
[0106] Here, α is the harmonic weight, used to adjust the relative importance of text content retention and balance the two losses.
[0107] As a preferred embodiment of the present invention, the specific steps of step 3 are as follows:
[0108] Step 3.1: To achieve batch processing of the text sentiment conversion model, the batch size of the input text is first defined as 64, the length of the generated content prompt vector is set to 5, and the batch size of the model is the batch size of the input text. This enables the model to perform parallel computation on the input text and improves the conversion speed.
[0109] Step 3.2: Save the model as a ".pth" file and load the model into memory using the Flask framework to avoid frequent model loading processes caused by requesting results and improve the running speed of the recognition model;
[0110] Step 3.3: Deploy the model as an API interface using the Flask framework to enable multiple concurrent requests on the web port;
[0111] Step 3.4: On the web, the text sentiment conversion model deployed on the server is called to convert the sentiment attribute of the input text, thereby obtaining the result with the opposite sentiment polarity and displaying it on the front-end interface.
[0112] According to the concept of the present invention, the present invention also provides an unsupervised text sentiment transformation device based on generated prompts, such as... Figure 3 As shown, the device includes the following integrated modules:
[0113] A module for constructing an unsupervised text sentiment transformation model based on generated prompts is used to improve the text and sentiment understanding capabilities of the pre-trained model by constructing a generated prompter containing text content. Then, based on the pre-trained model, it is trained again using text reconstruction loss and sentiment classification loss, so that the model can be used to generate accurate, fluent sentences with opposite sentiments.
[0114] The training module for an unsupervised text sentiment transformation model based on generated prompts consists of two stages: the first stage uses self-supervised training to construct effective content prompt vectors, enabling the model to understand and process the sentiment content of the text; the second stage uses recurrent reconstruction training through reconstruction loss and sentiment classification loss, incorporating sentiment prompts to better understand and process the sentiment transformation task, thereby generating more accurate and natural sentences that conform to the target sentiment.
[0115] The unsupervised text sentiment conversion module based on generated prompts is used to set relevant parameters of the model input to achieve the purpose of batch processing of text, thereby improving the speed of the text sentiment conversion model. Then, the trained neural network model is saved and deployed on the server to realize the conversion of sentiment attributes in the text.
[0116] To illustrate the effectiveness of this invention, the following experiments were conducted, using the Yelp public dataset selected for this invention, as shown below.
[0117] Training dataset: 266,041 texts with positive sentiment attributes and 177,218 texts with negative sentiment attributes.
[0118] Test dataset: 2000 texts with positive sentiment attributes and 2000 texts with negative sentiment attributes.
[0119] Evaluation dataset: 500 texts with positive sentiment attributes and 500 texts with negative sentiment attributes.
[0120] The neural network architecture of this invention is developed and implemented based on the PyTorch framework. The experimental server is configured as an 11th Gen Intel(R) Core(TM) i9-11900K@3.50GHz and an NVIDIA Corporation GA102 [GeForce RTX 3090].
[0121] This invention automatically evaluates the content retention (Content), style (Style) accuracy, and fluency (Fluency) of the model output. A language model pre-trained using an information alignment strategy is employed to evaluate Content retention, a fine-tuned BERT is used as a classifier to measure Style, and a grammatical classifier is used to measure Fluency. Text sentiment transformation tasks typically require a comprehensive evaluation of these three scores; this invention uses a joint score for evaluation, which captures the importance of each indicator and their interrelationships. The joint score for sentence x from the test set is defined as follows:
[0122]
[0123] This invention also reports the geometric mean (GM) of these three evaluation scores:
[0124] GM(Content,Style,Fluency)=e(log(Content(x))+log(Style(x))+log(Fluency(x))) / 3
[0125] The geometric mean averages the scores after taking the logarithm during calculation, which better handles the range and relative relationships of scores. The logarithmic operation maps a large range of scores to a smaller range, helping to avoid the excessive influence of extreme scores on the average. The geometric mean better reflects the relative relationships between various indicators, especially when the score ranges of indicators differ significantly, as it balances the contributions of each indicator. This invention also evaluates commonly used evaluation metrics for generation tasks, BLUE and BERTScore, to assess content retention.
[0126] To verify the effectiveness of the unsupervised text sentiment transformation method based on generated prompts, this invention conducts experimental analysis on the Yelp public dataset. The prompt generator is a bidirectional LSTM with a hidden layer size of 256 dimensions and a word embedding dimension of 768 dimensions. An MLP layer with dimensions of 256*768 is connected to its output layer. The prompt decoder uses a GPT-2 model with 124M parameters. The classifier uses an attention-based bidirectional LSTM model with a hidden layer size of 256 dimensions. The sentiment classifier is trained for 1 epoch with a batch size of 8. The optimizer uses Adam with a learning rate of 1e-4, and the learning rate scheduler uses LinearLR with a termination factor of 1e-6. In the training of GPTST, the optimizer is Adam, the learning rate is adjusted to 1e-3, the learning rate scheduler is LinearLR, the scheduler termination factor is 1e-9, the first stage is self-supervised training for 7 epochs, the generated content cue vector length is 5*768, and the second stage is cyclic reconstruction training for 13 epochs.
[0127] Experiment 1: Main Experimental Results
[0128] Table 1 presents the comparative experimental results between GPTST and the baseline model. The baseline model and GPTST were evaluated on the public dataset Yelp, with assessments and analyses conducted based on factors such as content retention, style accuracy, fluency, and joint score.
[0129] Table 1 Results of the Unsupervised Text Sentiment Transfer Experiment
[0130]
[0131] As shown in Table 1, RetrieveOnly achieved the highest sentiment conversion accuracy of 98.5 (Style). However, editing methods often suffer from low relevance of retrieved content, leading to low text retention in the generated text. Therefore, despite RetrieveOnly's high sentiment conversion accuracy, its Content retention score is only 14.2, meaning the generated sentence is completely different from the original. This results in a combined score of only 13 and 49.9 for Joint_score and GM, respectively. The baseline model TemplateBase is a template-based method that directly replaces the original sentiment words with the retrieved target sentiment words. Therefore, this method has high text retention and sentiment conversion accuracy. However, the direct replacement-based template method severely impacts sentence fluency, with an experimental result of only 34.8. Compared to GPTST, it achieved a content retention score of 56.6. This is because the self-supervised training in the first stage enabled the content cue vectors to contain textual semantic information and key sentiment features, thereby generating semantically correct and fluent sentences. As a result, it achieved the highest scores of 23.4 and 57.8 in the joint score and geometric mean score, respectively.
[0132] The baseline model BackTranslation posits that the sentiment attributes of text are eliminated during the back-translation training process in machine translation, and then decoded using a specific sentiment decoder. However, the latent representation separation strategy based on back-translation heavily relies on the performance of the translation model, which typically fails to achieve accurate information preservation. Therefore, this method only scores 16.7 on the content retention score, generating text content irrelevant to the input. The baseline models Multidecoder and UnpairedRL both employ a dual-decoder strategy to generate text with specific sentiments. Since the different sentiment decoders are trained solely on text reconstruction strategies, the models lack strong sentiment supervision, resulting in poor generalization ability on the test set. The experimental results in Table 1 show that the sentiment conversion accuracy is only 45.9 and 49.1 for Style, respectively, leading to a combined score of only 4 and 6.6 for Joint_score. Unlike generative methods based on encoders and decoders, GPTST uses a cue-based learning approach to construct effective cues to guide the pre-trained model in generation. Since there is no separation between text content and potential representations of sentiment attributes, there is no information loss. By constructing cues to effectively guide the pre-trained model in generation, high information retention and accurate sentiment conversion are achieved.
[0133] Experiment 2: Experiment and Analysis of Three Prompt Methods
[0134] This invention employs different prompt construction methods for experimentation, using discrete prompts (Hard Prompt), continuous prompts (Soft Prompt), and generated prompts based on GPTST to construct prompts, and reports the impact and analysis of different prompt construction methods on model performance.
[0135] Table 2 Performance Comparison of Different Prompt Methods
[0136]
[0137] As shown in Table 2, this invention uses "Here is some text:"{sentence}".Here is a write of the text, which is more{positive}:" as the discrete prompt template, where "sentence" represents the original sentence to be replaced, and "positive" represents the emotional polarity of the target sentiment. The input text is filled into the discrete prompt template as the final input to the model. Experimental results show that a high text fluency is achieved, but the text retention and sentiment conversion accuracy are only 7.1 and 10.9, respectively. This is because the GPT-2 model does not correctly recognize and process the discrete prompt instructions, causing the model to arbitrarily generate semantically irrelevant fluent text. The continuous prompts employ prefix tuning for two-stage self-supervised and iterative reconstruction training of the model. Results show improvements in text retention and sentiment conversion accuracy, increasing by 10.2 and 45.8 respectively. This indicates that prefix tuning is effective in improving the performance of the text sentiment conversion task. However, the overall performance metrics remain poor. Low sentence fluency negatively impacts the model's overall performance, resulting in scores of only 0.8 and 17.6 on the Joint score and Generalized Meaning (GM). This invention attributes this to the fact that although prefix tuning uses continuous prefix vectors as task prefixes, the text input remains discrete, making it prone to generating errors during the iterative reconstruction process, leading to error accumulation. Therefore, this invention employs a prompt generation method to construct prompts. Experimental results show a balanced improvement in text retention (Content), sentiment conversion accuracy (Style), and overall performance metrics.
[0138] Experiment 3: Length Prompt Experiment and Analysis
[0139] This invention experimentally analyzed the correlation between cue length (GPTST(Length)) and model generation performance. Cue length refers to the length of the content cue vector generated by the cue generator, as shown in Table 3. GPTST(1): a content cue vector with 1*768 dimensions.
[0140] Table 3 shows the experimental results for the indicated length.
[0141]
[0142] As shown in Table 3, in the experiment on cue length, this invention found a direct correlation between cue length and the text retention of the final generated sentence. The content retention increased from 34.3 to 73.9 as the cue length increased from 1 to 9. According to this invention, it can be intuitively assumed that within a certain length range, the vector is directly proportional to the amount of information it contains. Therefore, the cue length is positively correlated with the content retention of the generated text. However, longer cue vectors may lead to excessive retention of emotion, causing the generated text to overly retain the emotional content of the original text. Thus, the experimental results show that excessively high text retention affected the accuracy of the emotional conversion, decreasing it from 87.1 to 67.7. Although the content retention reached as high as 73.9, this invention considers the emotional conversion accuracy in emotional conversion tasks to be an important indicator, and an accuracy of 66.1% is unacceptable.
[0143] Experiment 4: Harmonic Weight Experiment and Analysis
[0144] As shown in Table 4, this invention conducted experiments and analyses on the correlation between the important parameter loss harmonic weight α and the model generation performance during the training process. It reports various evaluation indicators of the sentence generation performance of the training model under different harmonic weights α and analyzes the impact of α on the model generation performance.
[0145] Table 4 Experimental results for harmonic weighting parameter α
[0146]
[0147] In the experiment of loss harmonic weighting, this invention conducted experiments with α ranging from 0.1 to 0.8. As shown in Table 4, the experimental results indicate that as α increases, the content retention gradually decreases; when α is 0.8, the content decreases to 56.6, but the corresponding sentiment retention also gradually increases from 67.4 to 79.7. This is because in text sentiment transformation tasks, text retention loss and sentiment transformation loss are often mutually exclusive learning objectives, reflecting a trade-off between content retention and sentiment transformation in these tasks. Figure 3 As shown, higher loss harmonic weights encourage the model to focus more on sentiment conversion, resulting in higher sentiment conversion accuracy (Style), but may lead to missing text content. Conversely, lower loss harmonic weights may better preserve text content, resulting in higher content retention, but may reduce performance in sentiment conversion. Figure 3The paper presents text fluency and the Bertscore metric, commonly used to measure content retention. It reveals that both decrease as content retention decreases. This is because diverse content is more likely to generate disjointed sentences. When models are required to achieve excessively high sentiment conversion accuracy, some sentence coherence may be sacrificed, resulting in disjointed sentences and thus affecting the fluency metric. Experimental results demonstrate that α effectively modulates the two losses J during model training. c and J s Balanced learning is used to achieve a balance between text content retention and conversion accuracy.
[0148] Experiment 5: Visual Analysis of Content Hint Vectors
[0149] To improve the interpretability of the model, the text visualizes the content suggestion vectors generated by the suggestion generator. It performs dimensionality reduction visualization on the content suggestion vectors generated by the suggestion generator after the first stage of self-supervised training, such as... Figure 4 On the left, NoTrain, after completing the iterative reconstruction training, visualizes the content suggestion vectors generated by the suggestion generator through dimensionality reduction, such as... Figure 4 As shown on the right, "Trained".
[0150] like Figure 4 As shown, red represents the content cue vector representation of positive sentiment polarity, and blue represents the content cue vector representation of negative sentiment polarity. It can be observed that the representation morphology remains unchanged before and after cyclic reconstruction training, but the representation changes from compact and dense to looser. Therefore, this invention argues that the model does not separate content representation from sentiment representation throughout the entire sentiment conversion process, thus avoiding content loss during the separation process. The loose representation facilitates finer-grained and in-depth semantic analysis, providing greater flexibility and enabling the model to better capture semantic and implicit sentiment information in the text, better handle complex sentiment expressions, and thus provide a smooth training gradient for model training. This effectively alleviates the difficulty of model training in unsupervised text sentiment conversion tasks and avoids model degradation.
[0151] The specific embodiments of the present invention have been described in detail above with reference to the accompanying drawings. However, the present invention is not limited to the above embodiments. Within the scope of knowledge possessed by those skilled in the art, various changes can be made without departing from the spirit of the present invention.
Claims
1. An unsupervised text sentiment transfer method based on generated prompts, characterized by: The specific steps of the method are as follows: Step 1: Construct an unsupervised text sentiment transformation model based on generated prompts: By constructing a generated prompter containing text content, the pre-trained model has better text and sentiment understanding capabilities. Then, based on the pre-trained model, it is trained again through text reconstruction loss and sentiment classification loss, so that the model can be used to generate accurate, fluent sentences with opposite sentiments. Step 2: Train an unsupervised text sentiment transformation model based on generated prompts. The training process is divided into two stages: The first stage uses self-supervised training to construct effective content prompt vectors, enabling the model to understand and process the sentiment content of the text. The second stage involves iterative reconstruction training using reconstruction loss and sentiment classification loss, incorporating sentiment cues to better understand and process sentiment transformation tasks, thereby generating more accurate and natural sentences that conform to the target sentiment. Step 3: Unsupervised text sentiment transformation based on generated prompts: Set the relevant parameters of the model input to achieve the purpose of batch processing of text, thereby improving the speed of the text sentiment transformation model. Then, save the trained neural network model and deploy it on the server to realize the transformation of sentiment attributes in the text. The specific steps of Step 2 are as follows: Step 2.1 Self-supervised training The first stage is self-supervised training, which uses a prompt generator to re-represent the text and provide continuous vectors with rich semantic information for the text content prompts in the second stage; the prompt generator is a bidirectional LSTM model, and the output layer of the model is connected to a multilayer perceptron. Step 2.2, the second stage is the iterative reconstruction training, which concatenates the sentiment cue vector with the content vector generated by the cue generator to form the final cue. The cue pre-trained language model GPT-2 generates the sequence, thereby specifying the model to generate sentences with specific sentiments. The specific steps of Step 2.2 are as follows: Step 2.2.1: The first step is to construct emotional rewards, assigning emotional attributes as... Sentences As input, the input prompt generator generates a text latent representation vector that contains only text content and no emotional information. ; Step 2.2.2: Unlike the input composition in the first stage, <|negative|> <|bosoftext|> are used to concatenate the prompts generated by the generator to form the final prompts in the second stage. The sentiment vector <|negative|> with the opposite sentiment polarity is used to prompt the model to generate the sentiment polarity of the sentence. Step 2.2.3: Employ reinforcement learning to construct a reward for generating sentences, thereby guiding the model to generate sentences that align with the target emotion. Sentences that also contain text content Specifically, in the reward construction, GPTST acts as an agent, interacting with a pre-trained classifier as an environment. The pre-trained classifier provides character-level feedback rewards based on the sequences generated by GPTST. In form, this involves emotional rewards. and sentiment classification loss It is represented as: ; ; in It is the probability of a generated sequence matching the target sentiment determined by a pre-trained classifier. The parameters of the classifier are fixed during the training process. yes The sequence generation probability is maximized by maximizing the expected emotional reward. To obtain the final loss ; Step 2.2.4: For the loss of text information preservation, a cyclic reconstruction method is used to construct it. In the sentiment conversion task, the two sentences that are converting sentiments are identical in content information except for the sentiment information. In other words, the sentence with the target sentiment can be reconstructed into the sentence with the original sentiment. Based on the above, a cyclic reconstruction method is constructed to ensure the construction of the content preservation loss in text sentiment conversion. Specifically, emotional attributes Sentences As input, generate target sentiment attributes Sentences Then As input, the loop generates elements with original emotional attributes. Sentences , build and The text reconstruction loss is used to ensure the preservation of text content; formally, the text reconstruction loss... It is represented as: ; Where x represents the emotional attribute. The sentence, For the probability of sequence generation, Sentences reconstructed for loops The sequence length; Step 2.2.5: Use the harmonic mean of the two losses as the final loss: ; in, It is a harmonic weighting, used to adjust the relative importance of text content retention and balance the two losses.
2. The unsupervised text sentiment transformation method based on generated prompts according to claim 1, characterized in that: The specific steps of Step 1 are as follows: Step 1.1: Construct a cue generator. The cue generator is a sequence-to-sequence model that includes an LSTM layer and an MLP. The model accepts input data x and first performs word embedding processing through an embedding layer. Then, through multiple iterations, the LSTM layer processes the embedded data and obtains the last LSTM state of each sequence in the first iteration. Then, these LSTM states are processed by an MLP to generate the final cue vector; the final cue vector is a three-dimensional tensor that contains the cue vector representations of a sequence at a specified number of time steps. Step 1.2: Construct the input form of the generator by concatenating the two three-dimensional embedding vectors, the content cue vector and the sentiment cue vector, in the second dimension to generate the final cue vector. The purpose of this is to combine the input content cue and sentiment cue so that they can be provided as joint input to the model for subsequent processing and training. Step 1.3: Construct the decoding form of the pre-trained model. In order to decode the text in the pre-trained GPT model, the embedding layer of the model is first extracted, and the input is changed from the original discrete text input to the model to the form of prompt vectors. During the decoding process, past context information is used to maintain the previous computational state, generating coherent and grammatically correct text; Step 1.4: Based on the pre-trained model, further unsupervised training will be conducted. The goal of the training is to enable the model to generate accurate, fluent sentences with opposite sentiments. To achieve this goal, two loss functions are introduced: text reconstruction loss and sentiment classification loss.
3. The unsupervised text sentiment transformation method based on generated prompts according to claim 1, characterized in that: The specific steps of Step 2.1 are as follows: Step 2.1.1, Assume the input sequence is... ,in This represents the t-th word in the input sequence, which is also the last word. The hidden state is obtained by encoding the sentence using BiLSTM. and memory cells : ; Step 2.1.2: Pass the hidden state through an MLP layer to obtain the first vector of the text latent representation. : ; Step 2.1.3: Re-execute the above process Step 2.1.1-Step 2.1.2 to obtain the hidden state at each time step. and memory cells This yields the text latent representation vector for each time step. : ; ; Step 2.1.4: Concatenate the obtained text latent representation vectors of length n to obtain the final text latent representation. : ; in, This indicates vector concatenation, which is then combined with a specified instruction to generate an emotion vector <|positive|> <|bosoftext|> as the final cue for GPT-2. : ; in, This indicates the sentiment vector that instructs the generation of sentiment information. It is a randomly initialized cue vector used to specify the positive and negative emotional polarities, with a dimension of 2*768; Step 2.1.5: Develop the final hint vector. As input to the GPT-2 model, it is used for training, with the training loss... as follows: ; in For emotional attributes The sentence is prompted by the prompt generator. This represents the sequence generation probability of the GPTST model in this invention. Sentences reconstructed for loops The sequence length; Intuitively, <|positive|> is used to prompt the model to generate sentences with a specific sentiment, and <|bosoftext|> prompts the model to start generating.
4. The unsupervised text sentiment transformation method based on generated prompts according to claim 1, characterized in that: The specific steps of Step 3 are as follows: Step 3.1: First, define the batch size of the input text to the model as 64, set the length of the generated content prompt vector to 5, and set the batch processing size of the model to be the batch size of the input text. This is used to realize the parallel computation of the input text by the model and improve the conversion speed. Step 3.2: Save the model as a ".pth" file and load it into memory using the Flask framework. This avoids frequent model loading processes caused by requesting results and improves the running speed of the recognition model. Step 3.3: Deploy the model as an API interface using the Flask framework to enable multiple concurrent requests on the web port; Step 3.4: On the web, the text sentiment conversion model deployed on the server is called to convert the sentiment attribute of the input text, thereby obtaining the result with the opposite sentiment polarity and displaying it on the front-end interface.
5. An unsupervised text sentiment transfer device based on generated prompts, characterized in that, Includes a module for performing the method as claimed in any one of claims 1-4.