Contextual Fine-Tuning of Language Models with Classifier-Driven Content Moderation for Text Generation
<p>Evolution of LLMs.</p> "> Figure 2
<p>Types of fine-tuning.</p> "> Figure 3
<p>Traditional story generation using LLM.</p> "> Figure 4
<p>Pipeline of the proposed system.</p> "> Figure 5
<p>Prompt generation module.</p> "> Figure 6
<p>Supervised fine-tuning.</p> "> Figure 7
<p>Gradient normalization of LLaMA, Mistral, and Zephyr models. (<b>a</b>) LLaMA gradient normalization; (<b>b</b>) Mistral gradient normalization; (<b>c</b>) Zephyr gradient normalization.</p> "> Figure 8
<p>Learning rate of LLaMA, Mistral, and Zephyr models. (<b>a</b>) LLaMA learning rate; (<b>b</b>) Mistral learning rate; (<b>c</b>) Zephyr learning rate.</p> "> Figure 9
<p>Training loss of LLaMA, Mistral, and Zephyr models. (<b>a</b>) LLaMA training loss; (<b>b</b>) Mistral training loss; (<b>c</b>) Zephyr training loss.</p> "> Figure 10
<p>BERT Classifier.</p> "> Figure 11
<p>Types of classifiers.</p> "> Figure 12
<p>Confusion matrix BERT Classifier.</p> ">
Abstract
:1. Introduction
1.1. Child-Friendly Story Generation with Classifier Integration
1.2. Fine-Tuning Large Language Models for Child-Friendly Story Generation
1.3. Contributions of the Proposed Research
- Custom Dataset Creation: We have developed a dataset comprising 515 stories explicitly designed for children, including titles and prompts to guide story generation. The dataset ensures the model is trained to produce age-appropriate content that meets children’s cognitive and emotional needs.
- Supervised Fine-Tuning of Multiple Models: We have applied Supervised Fine-Tuning (SFT) to three state-of-the-art language models—Zephyr-7B, Mistral-7B, and Llama2 7B—enabling them to generate child-friendly stories. The fine-tuning process allows these models to adapt to the unique requirements of children’s literature, ensuring simplicity, thematic appropriateness, and creativity.
- Classifier Model for Content Evaluation: A novel classifier model is introduced, which evaluates the generated stories by flagging parts of the content that may be inappropriate or unsuitable for children. Once flagged, these portions are passed through the fine-tuned LLM for automatic reframing, ensuring the entire story is coherent and aligned with child-friendly standards. This addition provides a second layer of control in maintaining the quality and appropriateness of the generated text.
- Comprehensive Comparative Analysis: We conducted a comparative analysis of three models by evaluating their performance across critical aspects, such as simplicity, and content appropriateness. This evaluation aimed to assess how well each model handles child-friendly story generation. To further ensure the quality and safety of the content, we incorporated human evaluation. A group of PhD scholars from a state university manually assessed the stories generated by each model based on five key criteria: Language Simplicity and Clarity, Suitability for Children, Deceptiveness, Story Structure and Pacing, and Creativity and Originality. To ensure consistency, we calculated the Inter-Annotator Agreement (IAA) using the Kappa score.
- Evaluation of Generated Stories: In addition to standard NLP metrics such as ROUGE, METEOR, and BERT Score, we incorporate human evaluations to assess the suitability of the generated stories for children. This includes factors such as creativity, thematic appropriateness, readability, and emotional tone, ensuring the final content is engaging and suitable for young readers.
2. Literature Survey
2.1. Thematic Categorization
2.1.1. Early Language Models and Content Generation
2.1.2. Transformer-Based Models for Language Generation
2.1.3. Fine-Tuning Large Language Models (LLMs)
2.1.4. Classifier Models in Content Moderation
2.1.5. Evaluation Metrics in Content Generation
2.2. Comparative Analysis
3. Methodology
3.1. Mistral-7B
- Architecture: Mistral-7B is also based on the transformer model but incorporates enhancements in its attention mechanisms and memory management. These optimizations are designed to make the model more efficient during both training and inference, reducing the computational resources required. The use of improved layer normalization and attention mechanisms allows Mistral-7B to achieve faster convergence during training and faster response times during text generation tasks.
- Unique Features: One of the key strengths of Mistral-7B is its speed. Mistral-7B is optimized for faster text generation while maintaining a similar level of accuracy and coherence to other models in its parameter range. This is particularly important for real-time applications like chatbot-based storytelling or interactive children’s story platforms, where fast response times are crucial.
- Use Case: Mistral-7B is well suited for real-time and low-latency applications. Its fast inference times make it ideal for interactive storytelling, where children may interact with a story-generation system in real-time. The model’s training process is designed to minimize latency without sacrificing the creative aspects of story generation.
3.2. LLaMA2 7B
- Architecture: Like the other models, Llama2 7B follows the transformer architecture with 7 billion parameters. It includes several layers of self-attention and feed-forward networks, and it is optimized for large-scale text generation and understanding tasks. The model is pre-trained on a massive dataset covering a wide range of domains, allowing it to generalize well across different text-generation tasks.
- Performance: Llama2 7B is known for its high performance in generating fluent, coherent, and contextually accurate text. Its robust training process enables it to adapt to specific tasks, making it particularly effective in creative writing tasks. The model performs well in generating long-form text that requires maintaining thematic and narrative consistency, which is essential for storytelling.
- Why it is Widely Used: The popularity of Llama2 7B stems from its open-source nature and strong performance across different tasks. It has been fine-tuned and evaluated on numerous benchmarks, making it a trusted choice for various natural language generation applications. In this study, its adaptability to child-friendly story generation is tested, given its ability to produce high-quality and engaging narratives.
3.3. Zephyr-7B-Beta
- Architecture: Like most Large Language Models, Zephyr-7B-Beta employs the transformer architecture, consisting of multiple layers of self-attention and feed-forward networks. Each layer contains several attention heads, allowing the model to capture long-range dependencies in the text. This architecture is well suited for tasks that require coherent and contextually appropriate text generation over extended passages.
- Size: With 7 billion parameters, Zephyr-7B-Beta strikes a balance between model complexity and computational efficiency. It is large enough to perform exceptionally well on text generation tasks while being small enough to be fine-tuned and deployed on consumer-grade hardware or cloud services.
- Intended Use Case: Zephyr-7B-Beta is designed for general-purpose text generation tasks, such as dialogue generation, summarization, and creative writing. Pre-trained on diverse datasets, it is capable of generating high-quality text across various domains. For this study, Zephyr-7B-Beta is fine-tuned to generate child-friendly stories, focusing on simplicity, engagement, and appropriate language for young audiences.
3.4. Dataset Preparation
- Story and Title Compilation: Initially, the stories and their titles were gathered and structured in a JSON file format. Each entry in this file includes the full text of a story along with its title, forming the foundational dataset for the subsequent processing steps.
- Preparing Prompts for Child-Friendly Stories: In the process of adapting generative models for producing child-friendly stories, a key step involved crafting detailed prompts (Algorithm 1) that would guide the models in generating coherent and engaging narratives (Figure 5). This was achieved through a systematic approach involving text extraction and prompt generation based on the characteristics of each story. The following sections detail how this was accomplished using Python code and the Natural Language Toolkit (NLTK).
Algorithm 1 Prompt generation process. |
|
3.5. Key Components of the Prompt Generation Process
- Text Extraction: The process begins by extracting essential elements from each story using a custom function designed to capture the key details. This involves several important steps:
- (a)
- Tokenization and Stopword Removal: The text is split into individual words, and common stopwords (e.g., “the”, “and”, and “is”) are filtered out to retain only meaningful terms.
- (b)
- Frequency Distribution Analysis: A frequency analysis is conducted to identify the most common words, revealing key themes within the story.
- (c)
- Character Name Extraction: Character names are identified using regular expressions that capture capitalized words, with a limit set on the number of names extracted.
- (d)
- Sentence Extraction: The first and last sentences of the story are extracted to serve as reference points for prompt generation.
- Detailed Prompt Generation: The next step involves using the extracted elements to create rich, context-aware prompts. This is achieved by a function that generates prompts based on predefined templates to encourage narrative depth. The process includes the following:
- (a)
- Random Sampling: Key themes and character names are randomly selected to diversify the prompts, choosing up to five themes and three characters for each prompt.
- (b)
- Template Usage: A variety of storytelling templates are employed, each designed to generate unique narrative styles.
- Implementation Process: The entire prompt generation is managed by a central function that processes the stories and generates the prompts, with the following stages:
- (a)
- File Handling: The stories are read from an input file, ensuring compatibility with various text formats.
- (b)
- Prompt Assignment: A unique prompt is generated for each story and appended to the story object.
- (c)
- Output Generation: The updated stories, now containing their prompts, are saved to an output file in JSON format.
- Final Dataset Creation: The processed stories, complete with titles and generated prompts, are saved into a new JSON file. This organized dataset forms the basis for fine-tuning the language models used in this study.
3.6. Dataset Description: Classifer Model
3.7. Fine-Tuning Process: LLaMA 2, Mistral-7B, and Zephyr-7B-Beta
3.7.1. Training Setup
- Model: Each model, LLaMA 2, Mistral-7B, and Zephyr-7B-Beta, is a large-scale generative language model capable of handling various natural language generation tasks. For this research, LoRA was applied to the attention layers of the models, allowing efficient fine-tuning with minimal memory requirements.
- Training Dataset: The same custom dataset, consisting of 515 child-friendly stories, was used for all models. Each story in the dataset had an associated prompt and title. The dataset was tokenized using each model’s pre-trained tokenizer, resized to accommodate the unique vocabulary of the child-friendly dataset, ensuring proper tokenization and interpretation during the training process.
- Gradient Checkpointing: To further manage memory usage, gradient checkpointing was enabled. This technique reduces memory consumption by recomputing intermediate activations during backpropagation, allowing the models to handle longer sequences without exceeding memory limitations.
3.7.2. Training Arguments
- Batch Size: Due to memory constraints, the batch size was set to 1 per device for both training and evaluation.
- Learning Rate: A fixed learning rate of was employed for stable convergence.
- Gradient Accumulation Steps: Set to 4 to allow gradient accumulation over multiple steps, effectively addressing the small batch size limitation.
- Evaluation Strategy: Each model was evaluated after each epoch on a validation set to monitor performance and avoid overfitting.
- Number of Epochs: All models were fine-tuned for 6 epochs, with early stopping based on validation loss.
3.7.3. Loss Function
- The first term contributes to the loss when the true label is 1, which means that the model should predict a high probability for the positive class.
- The second term contributes to the loss when the true label is 0, indicating that the model should predict a low probability for the positive class.
- The overall loss is averaged over all samples to provide a measure of how well the model is performing across the entire dataset.
3.7.4. Optimizer and Learning Rate Scheduler
3.7.5. Training Configuration
- Batch Size: Set to 1 per device for both training and evaluation due to memory constraints.
- Learning Rate: Fixed at to ensure stable convergence.
- Max Token Length: For the models—Mistral-7B and Zephyr-7B-Beta, the maximum sequence length was set to 2048 and for LLaMA 2 the maximum sequence length was set to 1024.
- Epochs: The models were trained for 6 epochs.
- Gradient Accumulation Steps: Set to 4 for all models, allowing for gradient accumulation across multiple steps.
- Evaluation Frequency: The models were evaluated after each epoch.
- Optimizer: AdamW with a cosine learning rate schedule.
3.7.6. Training Process
Algorithm 2 Fine Tune Language Model. |
|
3.8. Design of the LLM Agent
- Initial Content Generation: The LLM agent generates a draft story based on an input prompt. This draft is produced using the pre-trained language model, which incorporates various themes and narratives designed for children’s literature.
- Classification Step: Once the initial draft is generated, it is passed to the classifier model. The classifier’s role is to evaluate the suitability of the content (Algorithm 3). It analyzes the title and story text to determine whether it meets the criteria for appropriateness for children.
- Feedback Loop: After the classifier processes the generated content, it returns a suitability assessment back to the LLM agent:
- If the content is marked as suitable: The process concludes, and the finalized story is ready for use.
- If the content is marked as unsuitable: The classifier provides feedback regarding the specific elements that contributed to the classification. This feedback may include suggestions for adjustments or the highlighting of inappropriate themes.
- Content Revision: The LLM agent takes the feedback from the classifier and revises the story accordingly (Algorithm 4). This may involve the following:
- Altering phrases or themes that were flagged as unsuitable.
- Enhancing character dialogues or descriptions to align better with child-friendly standards.
- Iterative Process: Steps 2 through 4 are repeated iteratively until the classifier model determines that the story is suitable for children. This iterative refinement allows for an effective tightening of the narrative to ensure it meets the intended standards.
- Final Approval: Once the classifier marks the story as suitable, the final version is saved, ready for presentation or further processing.
- Dynamic Adjustment: The iterative nature of the process allows for real-time adjustments to be made to the content, ensuring that each draft can be fine-tuned based on the classifier’s insights.
- Quality Assurance: By embedding the classifier model within the LLM framework, the overall quality and appropriateness of the generated content can be systematically enhanced.
- Child-Centric Focus: This approach prioritizes the cognitive and emotional needs of young audiences, reinforcing the commitment to producing literature that is both engaging and safe for children.
Algorithm 3 Classify Generated Story By Sentence. |
|
Algorithm 4 Reframe Unsuitable Sentences With LLM. |
|
4. Experimental Procedure
4.1. Environment Setup
4.2. Data Preprocessing
4.2.1. Dataset for LLM Fine-Tuning
- Data Cleaning: The dataset used for fine-tuning the LLM, comprising 500 children’s stories, was meticulously examined for inconsistencies, duplicates, and irrelevant entries. Any incomplete or malformed stories were removed to maintain the integrity of the dataset.
- Text Normalization: All text entries were converted to lowercase, and special characters, punctuation, and extraneous whitespace were eliminated. This standardization facilitates better tokenization and embedding representation.
- Tokenization: Each story was tokenized into individual words or subword units using the Hugging Face Transformers library’s tokenizer. This process converts the textual data into a format suitable for model input.
- Prompt Generation: Prompts were created for each story to guide the LLM during the generation phase. These prompts included essential elements, such as character names and key themes, extracted from the stories.
- Splitting the Dataset: The dataset was divided into training and validation sets using an 80:20 split ratio. This division allows for the evaluation of the LLM performance on unseen data.
4.2.2. Dataset for Classifier Model
- Label Encoding: The stories designated for the classifier model were labeled as “suitable” or “unsuitable” based on their content. This binary classification scheme was converted into numerical labels (0 for unsuitable and 1 for suitable) to facilitate training.
- Data Cleaning: Similar to the LLM dataset, this dataset underwent thorough cleaning to remove duplicates and ensure that all stories met the necessary quality standards.
- Feature Extraction: Relevant features were extracted from the stories, such as story length, word count, and lexical diversity. These features were combined with BERT embeddings to create a comprehensive feature set for the classifier model.
- Scaling Features: The custom features for the classifier were standardized using the StandardScaler from the scikit-learn library. This scaling process ensures that all features contribute equally to the training, promoting faster convergence.
4.3. Model Configuration
- LLM Configuration
- Model Selection: We utilized several advanced generative models, specifically LLaMA 2, Mistral-7B, and Zephyr-7B-Beta, to enhance the capability of generating contextually relevant and child-friendly stories. These models were chosen for their strong performance in natural language generation tasks.
- Fine-Tuning Process: The fine-tuning of these models was conducted using a Supervised Fine-Tuning (SFT) approach, utilizing a custom dataset comprising 515 child-friendly stories. The primary goal was to minimize the discrepancy between the model-generated text and the reference stories, focusing on fluency, coherence, and appropriateness for young audiences.
- Training Setup: The fine-tuning process was carried out using the SFT Trainer. Given the substantial number of parameters in these models, computational resources were a concern. To address this, Low-Rank Adaptation (LoRA) was employed, which permitted updates to specific parts of the models’ attention layers without retraining the entire network, thereby reducing memory usage and computational costs while maintaining high performance.
- Training Dataset: Each story in the custom dataset was associated with a prompt and a title. The dataset was tokenized using each model’s pre-trained tokenizer and resized to accommodate the unique vocabulary of the child-friendly dataset, ensuring proper tokenization and interpretation during training.
- Gradient Checkpointing: To further manage memory consumption, gradient checkpointing was implemented, allowing the recomputation of intermediate activations during backpropagation. This approach enabled the models to handle longer sequences without exceeding memory limits.
- Training Arguments:
- –
- Batch Size: Set to 1 per device due to memory constraints.
- –
- Learning Rate: A fixed learning rate of was utilized to ensure stable convergence.
- –
- Gradient Accumulation Steps: Set to 4 to accumulate gradients over multiple steps, effectively addressing the small batch size limitation.
- –
- Evaluation Strategy: Each model was evaluated after each epoch on a validation set to monitor performance and avoid overfitting.
- –
- Number of Epochs: All models underwent fine-tuning for 6 epochs, with early stopping based on validation loss.
- Loss Function: The Cross-Entropy Loss function was employed during fine-tuning for all models, measuring the divergence between the predicted and actual token distributions. By minimizing this loss, the models were guided to generate text closely resembling the reference child-friendly stories.
- Optimizer and Learning Rate Scheduler: The AdamW optimizer was chosen for fine-tuning, decoupling weight decay from the gradient update process for improved effectiveness with large models. A cosine learning rate schedule with warm-up steps was implemented, gradually increasing the learning rate during the initial training phases followed by a slow decay, which helped prevent large fluctuations in optimization.
- Classifier Model Configuration
- Model Selection: The classifier model (Figure 11) is based on the BERT architecture, specifically utilizing the BERT Base variant. The BERT bidirectional attention mechanism effectively captures contextual relationships within the text, making it well suited for classification tasks, especially in understanding the language for generating child-friendly content.
- Pre-trained Weights: The model is initialized with pre-trained weights from the Hugging Face model hub. This leverages the knowledge acquired during pre-training, allowing the classifier to benefit from vast amounts of language data, thus enhancing its performance on the specific task of classifying stories as suitable or unsuitable for children.
- Classifier Layer: A custom classification layer is added on top of the BERT architecture. This layer consists of a fully connected neural network with two output units representing the classes “suitable” and “unsuitable”. A dropout layer is incorporated to mitigate overfitting, followed by a linear layer that produces logits, which are then passed through a softmax activation function to produce class probabilities.
- Training Parameters: The classifier is trained using the Cross-Entropy Loss function to measure the difference between the predicted probabilities and the actual class labels. The AdamW optimizer is employed with a learning rate of , while a batch size of 8 is utilized due to memory constraints. The model undergoes training for 3 epochs, with gradient accumulation to optimize performance effectively while managing memory limitations. Each epoch involves monitoring performance on a validation set to prevent overfitting.
- Iterative Feedback Loop: An iterative feedback mechanism was implemented, allowing the classifier model to interact with the LLM agent. After generating a story, the classifier assessed its suitability, providing feedback that guides further generation. This loop continued until the classifier marked the story as suitable, ensuring that the final output aligned with the child-friendly criteria. Through careful model configuration and fine-tuning processes, we established a robust framework for both the LLM and classifier, enhancing their ability to generate and evaluate narratives appropriate for young audiences. This configuration served as the backbone for the experimental processes that follow.
- Training Process The training process for LLaMA 2, Mistral-7B, and Zephyr-7B-Beta models was meticulously designed to adapt these large-scale generative models for the specific task of generating child-friendly stories. This process involved several key steps to ensure that the models would produce high-quality, age-appropriate content.
- (a)
- Loading Pre-trained Models: The training began by loading the pre-trained versions of LLaMA 2, Mistral-7B, and Zephyr-7B-Beta. This step was crucial, as it allowed the models to utilize the extensive knowledge they acquired during their initial training on diverse text corpora, providing a strong foundation for further fine-tuning.
- (b)
- Tokenization and Data Preparation: The custom dataset of 515 child-friendly stories was tokenized using the respective pre-trained tokenizers for each model. This involved resizing the tokenizers to accommodate the unique vocabulary of the dataset, ensuring that the stories were properly tokenized and interpreted during the training process. Each story was paired with a corresponding prompt and title to enhance contextual understanding.
- (c)
- Implementing Low-Rank Adaptation (LoRA): To facilitate efficient fine-tuning while minimizing memory consumption, Low-Rank Adaptation (LoRA) was employed. This technique specifically targeted the attention layers of the models, allowing certain parameters to be updated without the need to retrain the entire network. This approach significantly reduced both computational costs and memory requirements, enabling effective model adaptation.
- (d)
- Gradient Checkpointing: To further manage memory usage, gradient checkpointing was enabled. This technique reduced memory consumption by storing only a subset of intermediate activations during forward propagation and recomputing the necessary activations during backpropagation. This allowed the models to handle longer sequences while staying within memory limits.
- (e)
- Training Configuration: The training was set up with a batch size of 1 per device, given the large model sizes and memory constraints. The learning rate was fixed at , and the models were trained for a total of 6 epochs, with early stopping based on validation loss to prevent overfitting. Gradient accumulation was utilized, allowing gradients to be accumulated over 4 steps, effectively addressing the challenges posed by small batch sizes.
- (f)
- Loss Function and Optimization: Cross-Entropy Loss was used as the loss function during training, guiding the models to minimize the divergence between the generated text and the reference child-friendly stories. The AdamW optimizer, coupled with a cosine learning rate schedule, facilitated stable convergence and effective optimization of the models.
- (g)
- Model Evaluation: Throughout the training process, the models were periodically evaluated on a validation dataset to monitor performance. This evaluation included assessing fluency, coherence, and suitability of the generated text. By tracking the performance metrics and employing early stopping, the training process ensured that the models remained focused on generating high-quality child-friendly stories. By leveraging techniques such as LoRA and gradient checkpointing, the training process for LLaMA 2, Mistral-7B, and Zephyr-7B-Beta was conducted efficiently, resulting in models capable of generating appropriate and engaging content for children.
- Classifier Integration: The integration of a classifier model is a crucial step in ensuring that the generated stories align with child-friendly content standards. This integration involves leveraging a pre-trained BERT-based classifier to evaluate and filter the outputs from the generative models—LLaMA 2, Mistral-7B, and Zephyr-7B-Beta. The classifier’s role is to determine whether a generated story is suitable or unsuitable for children, allowing for an added layer of content validation.
- (a)
- Classifier Architecture: The classifier is built upon the BERT architecture, specifically utilizing the BERT Base variant, known for its bidirectional attention mechanism. This design enables the model to effectively capture the contextual relationships within the generated text, making it well suited for classification tasks. A custom classification layer is added on top of the BERT model, consisting of a fully connected neural network with two output units corresponding to the classes “suitable” and “unsuitable” for children.
- (b)
- Pre-training and Fine-tuning: To enhance the classifier’s performance, it is initialized with pre-trained weights from the Hugging Face model hub. This initialization allows the model to benefit from knowledge acquired during extensive pre-training on diverse text datasets. Subsequently, the classifier is fine-tuned using a curated dataset of labeled stories, which includes examples of both suitable and unsuitable content. This fine-tuning process involves training the model on this labeled data, allowing it to learn the context that differentiate child-friendly stories from those that may contain inappropriate themes.
- (c)
- Training Process: The training of the classifier involves utilizing the Cross-Entropy Loss function, which quantifies the difference between the predicted classifications and the actual labels. The Adam optimizer is employed with a learning rate of , and the model is trained for a predefined number of epochs. A batch size of 8 is used to ensure effective learning while managing computational resources. The classifier’s training is closely monitored through validation metrics, ensuring that it generalizes well to unseen data.
- (d)
- Integration with Generative Models: Once trained, the classifier is integrated into the workflow of the generative models. After a story is generated by LLaMA 2, Mistral-7B, or Zephyr-7B-Beta, the output text is passed through the classifier. The model evaluates the story, producing probabilities for each class—suitable or unsuitable. Based on a defined threshold, the classifier determines whether the generated story meets the necessary standards for child-friendly content.
- (e)
- Feedback Loop and Continuous Improvement: The integration of the classifier creates a feedback loop in the content generation process. Stories that are flagged as unsuitable can be analyzed to understand the reasons for the classification, allowing for iterative improvements in both the generative models and the classification criteria. This continuous improvement process is essential for refining the quality of generated content, ensuring it remains appropriate for children.
- (f)
- Deployment and Scalability: The classifier model, once integrated and tested, is deployed alongside the generative models in a production environment. Its scalability ensures that as the volume of generated stories increases, the classifier can efficiently evaluate them in real-time, providing immediate feedback and validation. In summary, the integration of a BERT-based classifier into the story generation pipeline not only enhances the safety and appropriateness of the content but also ensures that the generated stories are aligned with the intended audience’s needs. This approach establishes a robust framework for generating engaging and suitable narratives for children.
5. Results and Evaluation
5.1. ROUGE-1 (Unigram Overlap)
5.2. ROUGE-2 (Bigram Overlap)
5.3. ROUGE-L (Longest Common Subsequence)
5.4. METEOR (Harmonic Mean of Precision and Recall)
5.5. BERT Score (Semantic Similarity of Embeddings)
5.6. Precision-Classifier Model
5.7. Recall-Classifier Model
5.8. F1-Score-Classifier Model
5.9. Accuracy-Classifier Model
6. Results and Discussion
6.1. ROUGE, METEOR, and BERT Score Analysis
6.1.1. ROUGE (Recall-Oriented Understudy for Gisting Evaluation)
6.1.2. METEOR (Metric for Evaluation of Translation with Explicit ORdering)
6.1.3. BERT Score
6.2. BERT Classifier Performance
6.3. Comparison with Hybrid and Random Forest Models
6.4. Insights from Macro and Weighted Averages
6.5. Impact of Fine-Tuning and Classifier Reframing
6.6. Comparison with Existing Models
6.7. Manual Evaluation
- Language Simplicity and Clarity: This criterion assesses how easily the language can be understood by children. A rating of 1 reflects a high level of complexity, while a rating of 5 signifies clear and accessible language.
- Suitability for Children: This evaluates the appropriateness of the story for children, ensuring it avoids any inappropriate themes. A rating of 5 indicates complete appropriateness, while a 1 would reflect significant issues with suitability.
- Deceptiveness: This measures the accuracy and truthfulness of the content but with an inverted scale. A rating of 1 indicates that the story is less deceptive (accurate and truthful), while a 5 indicates that the story is more deceptive (misleading or false).
- Story Structure and Pacing: Evaluators assess the organization and flow of the story. A rating of 1 reflects poor structure and pacing, while a 5 indicates a well-organized and engaging narrative.
- Creativity and Originality: This criterion assesses the originality and creativity of the story. A rating of 1 indicates a lack of creativity, while a 5 reflects a highly original and inventive story.
6.8. Case Study
- Input PromptWrite a short children’s story about two siblings who embark on a journey to find a magical flower that spreads love and joy. Describe how their actions transform their town and teach a lesson about kindness and generosity.
- Model-Generated StoryA long time ago, there lived an old poet, a thoroughly kind old poet. He had two children: a little boy and a little girl. The little boy and the little girl were twins, and they were very good children, and they loved their old poet father very much. One day, the old poet said to them, “My dearest children, I have a special task for you. I need you to go on a journey to find a very rare and special flower that grows only in a far-off land. This flower is called the Cupid’s Blossom, and it is said to have the power to bring true love to those who possess it. But be warned, it is said that whoever finds the flower will also attract the attention of vengeful spirits, who will stop at nothing to take it from them. I know that you are both brave and kind, and I believe that you can complete this task and bring back the flower to me. Are you willing to undertake this journey?” The little boy and the little girl looked at each other and nodded eagerly. “We will do our best, father,” they said. The old poet gave them a map and a small bag of gold coins to help them on their journey. He also gave them a special amulet that would protect them from harm. The next day, the little boy and the little girl set out on their journey. They followed the map and traveled through beautiful landscapes and dangerous territories. But along the way, they encountered dark, ominous creatures lurking in the shadows, whose glowing eyes seemed to follow their every move. After a long and difficult journey, they finally reached the land where the Cupid’s Blossom grew. The flower was hidden in a secret glade surrounded by tall, ancient trees. The little boy and the little girl searched carefully and finally found the flower. They carefully picked the flower and placed it in their bag. As they were about to leave, they heard a soft, melodious voice calling to them. They turned around and saw a beautiful, winged figure with a bow and arrow. It was Cupid, the god of love. Cupid thanked them for finding the flower and said that he had been watching over them on their journey. He warned them that love could be both a blessing and a curse, and those who misuse the flower would be doomed to suffer the consequences. The little boy and the little girl thanked Cupid and promised to use the flower wisely. They said goodbye and began their journey home. As they traveled back, they thought about the importance of true love and how it could bring joy and happiness to others. They decided that when they returned home, they would use the Cupid’s Blossom to help others find love. When they finally arrived home, the old poet was overjoyed to see them. He thanked them for their bravery and kindness and praised them for their successful journey. The little boy and the little girl gave the Cupid’s Blossom to their father, and he placed it in a special vase where it could be seen and admired by all. From that day on, the old poet and his children lived in peace and happiness, knowing that they had helped bring true love to others. The Cupid’s Blossom became a symbol of love and kindness in their town, and people came from far and wide to see it. The old poet continued to write poetry about love and kindness, and his children grew up to be kind and thoughtful individuals. They remembered the lessons they had learned on their journey and carried them with them throughout their lives. And so, the story of the old poet and his children’s journey to find the Cupid’s Blossom became a beloved tale of bravery, kindness, and true love. It reminded everyone of the importance of these qualities and how they could bring joy and happiness to others.
- Reframed Story: Suitable for ChildrenA little boy and girl once went to search for a rare flower, which was said to bring great joy and love to those who had it. After a long journey, they found the flower and they took it home to their parents. When they gave the flower to their father, he was overjoyed and he used it to make a potion that would bring love and joy to anyone who drank it. The little boy and girl shared the potion with everyone in their town, and soon, everyone was happy and filled with love. The town became a peaceful and joyful place, and the little boy and girl were praised for their kindness. Their parents were also very proud of them, and they knew that they had done something special and important. The story of the little boy and girl’s journey and their gift of love became a beloved tale in their town, and it reminded everyone of the power of kindness and the joy that can be brought to others through simple acts of generosity. The town continued to be a place of peace and joy, and the little boy and girl grew up to be kind and thoughtful individuals, always remembering the lesson of love and joy that they had learned on their journey.
- Analysis of the TransformationThe transformation from the original story to the reframed version illustrates several key changes:
- (a)
- Tone Adjustment: The original story includes elements of danger, such as vengeful spirits and warnings about misusing the flower, which are removed in the reframed version. The focus is shifted entirely to the positive aspects of the story—joy, love, and generosity.
- (b)
- Simplification: The complex narrative involving Cupid and the curses surrounding the flower is omitted. Instead, the reframed version streamlines the plot to focus on the siblings’ positive actions and their impact on their community.
- (c)
- Moral Emphasis: Both versions of the story emphasize kindness and generosity. However, the reframed version places more direct emphasis on how the siblings’ actions transformed their community, making the lesson more immediate and clear to young readers.
- (d)
- Audience Suitability: The reframed story is more suited for children, with a simpler plot and no dark elements. It reinforces positive themes of love and kindness without the complexities of external threats, making it more approachable for a younger audience.
7. Conclusions and Future Work
Author Contributions
Funding
Data Availability Statement
Acknowledgments
Conflicts of Interest
References
- Brown, T.B. Language models are few-shot learners. arXiv 2020, arXiv:2005.14165. [Google Scholar]
- Touvron, H.; Martin, L.; Stone, K.; Albert, P.; Almahairi, A.; Babaei, Y.; Bashlykov, N.; Batra, S.; Bhargava, P.; Bhosale, S.; et al. Llama 2: Open foundation and fine-tuned chat models. arXiv 2023, arXiv:2307.09288. [Google Scholar]
- Jiang, A.Q.; Sablayrolles, A.; Mensch, A.; Bamford, C.; Chaplot, D.S.; Casas, D.D.L.; Bressand, F.; Lengyel, G.; Lample, G.; Saulnier, L.; et al. Mistral 7B. arXiv 2023, arXiv:2310.06825. [Google Scholar]
- Tunstall, L.; Beeching, E.; Lambert, N.; Rajani, N.; Rasul, K.; Belkada, Y.; Huang, S.; von Werra, L.; Fourrier, C.; Habib, N.; et al. Zephyr: Direct distillation of lm alignment. arXiv 2023, arXiv:2310.16944. [Google Scholar]
- Team, G.; Mesnard, T.; Hardin, C.; Dadashi, R.; Bhupatiraju, S.; Pathak, S.; Sifre, L.; Rivière, M.; Kale, M.S.; Love, J.; et al. Gemma: Open models based on gemini research and technology. arXiv 2024, arXiv:2403.08295. [Google Scholar]
- Khan, M.J.; Sukthankar, G. SC-Phi2: A Fine-Tuned Small Language Model for StarCraft II Build Order Prediction. AI 2024, 5, 2338–2352. [Google Scholar] [CrossRef]
- Ren, Y.; Zhang, T.; Dong, X.; Li, W.; Wang, Z.; He, J.; Zhang, H.; Jiao, L. WaterGPT: Training a large language model to become a hydrology expert. Water 2024, 16, 3075. [Google Scholar] [CrossRef]
- Zhou, L.; Ren, X.; Qian, C.; Sun, G. TraceGuard: Fine-Tuning Pre-Trained Model by Using Stego Images to Trace Its User. Mathematics 2024, 12, 3333. [Google Scholar] [CrossRef]
- Mikolov, T.; Sutskever, I.; Chen, K.; Corrado, G.S.; Dean, J. Distributed representations of words and phrases and their compositionality. Adv. Neural Inf. Process. Syst. 2013, 26. [Google Scholar]
- Pennington, J.; Socher, R.; Manning, C.D. Glove: Global vectors for word representation. In Proceedings of the 2014 Conference on Empirical Methods in Natural Language Processing (EMNLP), Doha, Qatar, 25–29 October 2014; pp. 1532–1543. [Google Scholar]
- Chubb, J.; Missaoui, S.; Concannon, S.; Maloney, L.; Walker, J.A. Interactive storytelling for children: A case-study of design and development considerations for ethical conversational AI. Int. J. Child-Comput. Interact. 2022, 32, 100403. [Google Scholar] [CrossRef]
- Devlin, J. Bert: Pre-training of deep bidirectional transformers for language understanding. arXiv 2018, arXiv:1810.04805. [Google Scholar]
- Raffel, C.; Shazeer, N.; Roberts, A.; Lee, K.; Narang, S.; Matena, M.; Zhou, Y.; Li, W.; Liu, P.J. Exploring the limits of transfer learning with a unified text-to-text transformer. J. Mach. Learn. Res. 2020, 21, 1–67. [Google Scholar]
- Vaswani, A. Attention is all you need. In Advances in Neural Information Processing Systems; Cornell Unversity: Ithaca, NY, USA, 2017. [Google Scholar]
- Radford, A.; Wu, J.; Child, R.; Luan, D.; Amodei, D.; Sutskever, I. Language models are unsupervised multitask learners. OpenAI Blog 2019, 1, 9. [Google Scholar]
- Khan, S.; Fazil, M.; Sejwal, V.K.; Alshara, M.A.; Alotaibi, R.M.; Kamal, A.; Baig, A.R. BiCHAT: BiLSTM with deep CNN and hierarchical attention for hate speech detection. J. King Saud Univ.-Comput. Inf. Sci. 2022, 34, 4335–4344. [Google Scholar] [CrossRef]
- Arora, A.; Nakov, P.; Hardalov, M.; Sarwar, S.M.; Nayak, V.; Dinkov, Y.; Zlatkova, D.; Dent, K.; Bhatawdekar, A.; Bouchard, G.; et al. Detecting harmful content on online platforms: What platforms need vs. where research efforts go. ACM Comput. Surv. 2023, 56, 1–17. [Google Scholar] [CrossRef]
- Nayeem, M.T.; Rafiei, D. KidLM: Advancing Language Models for Children–Early Insights and Future Directions. arXiv 2024, arXiv:2410.03884. [Google Scholar]
- Goldberg, Y. Neural Network Methods in Natural Language Processing; Morgan & Claypool Publishers: San Rafael, CA, USA, 2017. [Google Scholar]
- Sutskever, I. Sequence to Sequence Learning with Neural Networks. arXiv 2014, arXiv:1409.3215. [Google Scholar]
- El-Alami, F.z.; El Alaoui, S.O.; Nahnahi, N.E. A multilingual offensive language detection method based on transfer learning from transformer fine-tuning model. J. King Saud Univ.-Comput. Inf. Sci. 2022, 34, 6048–6056. [Google Scholar] [CrossRef]
- Liu, Y. Roberta: A robustly optimized bert pretraining approach. arXiv 2019, arXiv:1907.11692. [Google Scholar]
- Dodge, J.; Ilharco, G.; Schwartz, R.; Farhadi, A.; Hajishirzi, H.; Smith, N. Fine-tuning pretrained language models: Weight initializations, data orders, and early stopping. arXiv 2020, arXiv:2002.06305. [Google Scholar]
- Chowdhury, A.K.; Sujon, S.R.; Shafi, M.S.S.; Ahmmad, T.; Ahmed, S.; Hasib, K.M.; Shah, F.M. Harnessing large language models over transformer models for detecting Bengali depressive social media text: A comprehensive study. Nat. Lang. Process. J. 2024, 7, 100075. [Google Scholar] [CrossRef]
- Pan, R.; García-Díaz, J.A.; Valencia-García, R. Comparing Fine-Tuning, Zero and Few-Shot Strategies with Large Language Models in Hate Speech Detection in English. CMES-Comput. Model. Eng. Sci. 2024, 140, 2849. [Google Scholar] [CrossRef]
- Ayetiran, E.F.; Özgöbek, Ö. An inter-modal attention-based deep learning framework using unified modality for multimodal fake news, hate speech and offensive language detection. Inf. Syst. 2024, 123, 102378. [Google Scholar] [CrossRef]
- Papineni, K.; Roukos, S.; Ward, T.; Zhu, W.J. Bleu: A method for automatic evaluation of machine translation. In Proceedings of the 40th annual meeting of the Association for Computational Linguistics, Philadelphia, PA, USA, 6–12 July 2002; pp. 311–318. [Google Scholar]
- Wang, A. Glue: A multi-task benchmark and analysis platform for natural language understanding. arXiv 2018, arXiv:1804.07461. [Google Scholar]
- Lin, C.Y. Rouge: A package for automatic evaluation of summaries. In Proceedings of the Text Summarization Branches Out, Barcelona, Spain, 25–26 July 2004; pp. 74–81. [Google Scholar]
- Wang, S. Factors related to user perceptions of artificial intelligence (AI)-based content moderation on social media. Comput. Hum. Behav. 2023, 149, 107971. [Google Scholar] [CrossRef]
- Children Stories Text Corpus. 2021. Available online: https://www.kaggle.com/datasets/edenbd/children-stories-text-corpus (accessed on 22 March 2023).
- Bed Time Stories. 2020. Available online: https://www.kaggle.com/datasets/mahendran1/bed-time-stories (accessed on 22 April 2023).
- Fareedkhan-Dev/NLP-1K-Stories-Dataset-Genres-100: This Repository Hosts a Diverse NLP Dataset Comprising 1000 Stories Spanning 100 Genres for Comprehensive Language Understanding Tasks. Available online: https://github.com/FareedKhan-dev/NLP-1K-Stories-Dataset-Genres-100 (accessed on 14 December 2023).
- Li, D.; Guo, Z.; Liu, Q.; Jin, L.; Zhang, Z.; Wei, K.; Li, F. CLICK: Integrating Causal Inference and Commonsense Knowledge Incorporation for Counterfactual Story Generation. Electronics 2023, 12, 4173. [Google Scholar] [CrossRef]
- Kim, J.; Heo, Y.; Yu, H.; Nang, J. A multi-modal story generation framework with AI-driven storyline guidance. Electronics 2023, 12, 1289. [Google Scholar] [CrossRef]
- Parag, J.; Agrawal, P.; Mishra, A.; Sukhwani, M.; Laha, A.; Sankaranarayanan, K. Story generation from sequence of independent short descriptions. arXiv 2017, arXiv:1707.05501. [Google Scholar]
- Feng, Y.; Song, M.; Wang, J.; Chen, Z.; Bi, G.; Huang, M.; Jing, L.; Yu, J. SS-GEN: A Social Story Generation Framework with Large Language Models. arXiv 2024, arXiv:2406.15695. [Google Scholar]
Aspect | Zephyr-7B-Beta | Mistral-7B | Llama2 7B |
---|---|---|---|
Model Type | Transformer-based LLM (Auto-regressive Model) | Transformer-based LLM (Auto-regressive Model) | Transformer-based LLM (Auto-regressive Model) |
Parameter Count | 7 Billion | 7 Billion | 7 Billion |
Architecture | Standard Transformer (Encoder–Decoder) | Optimized Transformer (Memory and Attention Tweaks) | Standard Transformer (Decoder-only) |
Attention Mechanism | Multi-Head Self-Attention | Enhanced attention for speed optimization | Multi-Head Self-Attention |
Layer Normalization | Standard Layer Normalization | Optimized Layer Normalization for faster training | Standard Layer Normalization |
Feed-Forward Networks (FFN) | Position-wise feed-forward networks | Optimized FFN for lower memory usage | Position-wise feed-forward networks |
Tokenization | BPE (Byte-Pair Encoding) | BPE (Byte-Pair Encoding) | SentencePiece Tokenizer (sub-word units) |
Training Objective | Predict next token (causal language modeling) | Predict next token (causal language modeling) | Predict next token (causal language modeling) |
Special Features | General-purpose text generation model | Optimized for faster inference and training | Strong performance across general text generation |
Intended Use Case | General-purpose tasks (summarization, dialogue) | Real-time applications requiring low latency | General-purpose text generation (creative writing) |
Inference Speed | Moderate | Fast | Moderate |
Memory Efficiency | Standard | Optimized for lower memory footprint | Standard |
Model | Trainable Parameters | All Parameters | Trainable Percent |
---|---|---|---|
LLaMA 2 | 311,164,928 | 3,811,577,856 | 8.16% |
Mistral | 352,321,536 | 4,110,684,160 | 8.57% |
Zephyr | 346,030,080 | 4,098,101,248 | 8.44% |
Model Name | Rouge-1 | Rouge-2 | Rouge-L | METEOR | BERT | |
---|---|---|---|---|---|---|
Base (Zero Shot) | LLaMA-7B | 0.2981 | 0.0632 | 0.1851 | 0.2799 | 0.8442 |
Mistral-7B | 0.3185 | 0.0597 | 0.1930 | 0.2918 | 0.8521 | |
Zephyr-7B-Beta | 0.3908 | 0.1147 | 0.2110 | 0.3602 | 0.8758 | |
SFT-LLM Output | LLaMA-7B | 0.3617 | 0.0778 | 0.1921 | 0.3184 | 0.8698 |
Mistral-7B | 0.4785 | 0.1532 | 0.2715 | 0.4455 | 0.8992 | |
Zephyr-7B-Beta | 0.4466 | 0.1138 | 0.1964 | 0.4154 | 0.8797 | |
Reframed-SFT-LLM (Classifier Output) | LLaMA-7B | 0.3105 | 0.0575 | 0.1818 | 0.2669 | 0.8554 |
Mistral-7B | 0.4487 | 0.1113 | 0.2186 | 0.4098 | 0.8864 | |
Zephyr-7B-Beta | 0.4285 | 0.0926 | 0.1977 | 0.3963 | 0.8751 |
BERT Classifier | Precision | Recall | F1 |
---|---|---|---|
Suitable | 0.73 | 0.62 | 0.67 |
Unsuitable | 0.95 | 0.97 | 0.96 |
Accuracy | 0.92 | ||
Macro Average | 0.84 | 0.79 | 0.81 |
Weighted Average | 0.92 | 0.92 | 0.92 |
BERT–Random Forest Hybrid Classifier | Precision | Recall | F1 |
---|---|---|---|
Suitable | 0.75 | 0.23 | 0.35 |
Unsuitable | 0.90 | 0.99 | 0.94 |
Accuracy | 0.89 | ||
Macro Average | 0.82 | 0.61 | 0.65 |
Weighted Average | 0.88 | 0.89 | 0.87 |
Random Forest Classifier | Precision | Recall | F1 |
---|---|---|---|
Suitable | 0.60 | 0.46 | 0.52 |
Unsuitable | 0.92 | 0.96 | 0.94 |
Accuracy | 0.89 | ||
Macro Average | 0.76 | 0.71 | 0.73 |
Weighted Average | 0.88 | 0.89 | 0.89 |
Reference | Model/Methodology | Metric to Compare | Existing Methodology Value | Proposed Methodology Value |
---|---|---|---|---|
Li Dandan et al. [34] | CLICK | BERT Score | 78.2 | 89.92 |
Kim Juntae et al. [35] | Multi-Modal Story Generation | BERT Score | 22.2 | 89.92 |
Parag Jain et al. [36] | SEQ2SEQ | METEOR | 10.30 | 40.98 |
Feng Yi et al. [37] | Mistral-7B | BERT Score | 89.8 | 89.92 |
Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content. |
© 2024 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (https://creativecommons.org/licenses/by/4.0/).
Share and Cite
Punnaivanam, M.; Velvizhy, P. Contextual Fine-Tuning of Language Models with Classifier-Driven Content Moderation for Text Generation. Entropy 2024, 26, 1114. https://doi.org/10.3390/e26121114
Punnaivanam M, Velvizhy P. Contextual Fine-Tuning of Language Models with Classifier-Driven Content Moderation for Text Generation. Entropy. 2024; 26(12):1114. https://doi.org/10.3390/e26121114
Chicago/Turabian StylePunnaivanam, Matan, and Palani Velvizhy. 2024. "Contextual Fine-Tuning of Language Models with Classifier-Driven Content Moderation for Text Generation" Entropy 26, no. 12: 1114. https://doi.org/10.3390/e26121114
APA StylePunnaivanam, M., & Velvizhy, P. (2024). Contextual Fine-Tuning of Language Models with Classifier-Driven Content Moderation for Text Generation. Entropy, 26(12), 1114. https://doi.org/10.3390/e26121114