iLLuMinator / train_enhanced.py
Anipal's picture
Upload 8 files
1d0ef1d verified
"""
Enhanced Training Script for Hugging Face Model
Comprehensive data sources and improved training for maximum accuracy
"""
import os
import json
import torch
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
from transformers import (
Trainer,
TrainingArguments,
DataCollatorForLanguageModeling,
get_linear_schedule_with_warmup
)
import numpy as np
from typing import Dict, List, Optional, Union
import requests
import time
import random
from pathlib import Path
class ComprehensiveDataset(Dataset):
"""Enhanced dataset with comprehensive training data for maximum accuracy"""
def __init__(self, tokenizer, max_length=512, min_length=10):
self.tokenizer = tokenizer
self.max_length = max_length
self.min_length = min_length
print("πŸ”„ Building comprehensive training dataset...")
# Collect training data from multiple sources
self.training_texts = []
# Add built-in comprehensive knowledge
self._add_knowledge_base_data()
# Add programming and technical content
self._add_programming_data()
# Add scientific and academic content
self._add_scientific_data()
# Add conversational and Q&A data
self._add_conversational_data()
# Add Wikipedia-style encyclopedic content
self._add_encyclopedic_data()
# Process and tokenize all data
self._process_training_data()
print(f"βœ… Dataset ready with {len(self.examples)} training examples")
def _add_knowledge_base_data(self):
"""Add comprehensive knowledge base for accuracy"""
knowledge_texts = [
# AI/ML Fundamentals
"""Artificial Intelligence (AI) is the simulation of human intelligence processes by machines, especially computer systems. These processes include learning, reasoning, problem-solving, perception, and language understanding. AI can be categorized into narrow AI, which is designed for specific tasks, and artificial general intelligence (AGI), which aims to match human cognitive abilities across all domains.
Machine Learning is a subset of AI that enables computers to learn and improve from experience without being explicitly programmed. The core idea is to build algorithms that can receive input data and use statistical analysis to predict an output value within an acceptable range. Machine learning algorithms are trained using large amounts of data and are able to make accurate predictions or decisions by learning patterns in the data.
Deep Learning is a subset of machine learning that uses artificial neural networks with multiple layers (hence "deep") to model and understand complex patterns in data. Deep learning has revolutionized many fields including computer vision, natural language processing, and speech recognition. The key advantage of deep learning is its ability to automatically learn hierarchical representations of data, eliminating the need for manual feature engineering.
Neural Networks are computing systems inspired by the biological neural networks that constitute animal brains. They consist of interconnected nodes (neurons) organized in layers. Each connection has a weight that adjusts as learning proceeds. Neural networks can approximate complex non-linear functions and have been successfully applied to various machine learning tasks including classification, regression, and pattern recognition.""",
# Programming and Software Engineering
"""Python is a high-level, interpreted programming language with dynamic semantics. Its high-level built-in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development. Python's syntax emphasizes readability, which reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse.
JavaScript is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. JavaScript enables interactive web pages and is an essential part of web applications. The vast majority of websites use it for client-side page behavior, and many also use it for server-side development through Node.js.
Software Engineering is the systematic application of engineering approaches to the development of software. It involves the design, development, testing, and maintenance of software applications. Software engineering principles include modularity, abstraction, encapsulation, and separation of concerns. Modern software engineering practices emphasize agile methodologies, continuous integration, and test-driven development.""",
# Science and Mathematics
"""Quantum Physics is the branch of physics that deals with the behavior of matter and energy at the atomic and subatomic level. Unlike classical physics, quantum mechanics introduces concepts such as wave-particle duality, quantum entanglement, and the uncertainty principle. These phenomena occur because particles at the quantum level behave according to probability rather than deterministic laws.
Calculus is a branch of mathematics that deals with rates of change and accumulation of quantities. It consists of two main branches: differential calculus, which concerns instantaneous rates of change and slopes of curves, and integral calculus, which concerns accumulation of quantities and areas under curves. Calculus has applications in science, engineering, economics, and many other fields.
Evolution is the change in the heritable traits of biological populations over successive generations. Natural selection is the differential survival and reproduction of individuals due to differences in phenotype. Evolution by natural selection is the process that explains the diversity of life on Earth and the apparent design in organisms.""",
# Technology and Computing
"""Cloud Computing is the delivery of computing services including servers, storage, databases, networking, software, analytics, and intelligence over the Internet to offer faster innovation, flexible resources, and economies of scale. The main types of cloud computing include Infrastructure as a Service (IaaS), Platform as a Service (PaaS), and Software as a Service (SaaS).
Cybersecurity is the practice of protecting systems, networks, and programs from digital attacks. These cyberattacks are usually aimed at accessing, changing, or destroying sensitive information, extorting money from users, or interrupting normal business processes. Effective cybersecurity measures include firewalls, encryption, multi-factor authentication, and regular security updates.
The Internet is a global network of interconnected computers that communicate using standardized protocols, primarily TCP/IP. The World Wide Web is an information system that operates over the Internet, allowing users to access and share information through web pages connected by hyperlinks. The Internet has revolutionized communication, commerce, and information sharing globally.""",
# Business and Economics
"""Entrepreneurship is the activity of setting up a business, taking financial risks in the hope of profit. Entrepreneurs identify market opportunities and organize resources to create value. Successful entrepreneurship often involves innovation, whether in products, services, business models, or processes.
Economics is the social science that studies the production, distribution, and consumption of goods and services. Microeconomics focuses on individual consumers and firms, while macroeconomics examines economy-wide phenomena such as inflation, unemployment, and economic growth. Key economic principles include supply and demand, market efficiency, and the role of government intervention.""",
# History and Culture
"""The Renaissance was a period in European history marking the transition from the Middle Ages to modernity, covering roughly the 14th to 17th centuries. It began in Italy and later spread throughout Europe. The Renaissance was characterized by a renewed interest in classical learning, humanism, artistic achievement, and scientific discovery.
Democracy is a form of government in which power is vested in the people, either directly or through freely elected representatives. Democratic systems are characterized by regular free and fair elections, the rule of law, protection of basic liberties, and equal citizenship. Modern democracies face challenges including political polarization, misinformation, and the need to balance majority rule with minority rights."""
]
self.training_texts.extend(knowledge_texts)
print(f"πŸ“š Added {len(knowledge_texts)} knowledge base entries")
def _add_programming_data(self):
"""Add comprehensive programming and technical content"""
programming_texts = [
# Code examples and explanations
"""Here is an example of a Python function that implements a binary search algorithm:
def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
This algorithm has a time complexity of O(log n) and is much more efficient than linear search for sorted arrays. The key insight is to repeatedly divide the search space in half.""",
"""Object-oriented programming (OOP) is a programming paradigm based on the concept of objects, which contain data (attributes) and code (methods). The main principles of OOP are encapsulation, inheritance, and polymorphism.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
pass
class Dog(Animal):
def speak(self):
return f"{self.name} says Woof!"
class Cat(Animal):
def speak(self):
return f"{self.name} says Meow!"
This example demonstrates inheritance, where Dog and Cat inherit from Animal, and polymorphism, where different classes implement the same method differently.""",
"""Web development involves creating applications that run on the World Wide Web. Modern web development typically involves:
Frontend Development: HTML for structure, CSS for styling, JavaScript for interactivity
Backend Development: Server-side languages like Python, Java, or Node.js
Databases: SQL (MySQL, PostgreSQL) or NoSQL (MongoDB) for data storage
APIs: RESTful APIs or GraphQL for communication between frontend and backend
A typical web application architecture includes a client (browser), server, and database. The client makes HTTP requests to the server, which processes the requests and returns responses, often after querying a database."""
]
self.training_texts.extend(programming_texts)
print(f"πŸ’» Added {len(programming_texts)} programming examples")
def _add_scientific_data(self):
"""Add scientific and academic content"""
scientific_texts = [
"""The Scientific Method is a systematic approach to understanding the natural world through observation, hypothesis formation, experimentation, and analysis. The process typically follows these steps:
1. Observation: Scientists observe phenomena and ask questions
2. Hypothesis: A testable explanation is proposed
3. Prediction: Expected outcomes are predicted based on the hypothesis
4. Experimentation: Controlled experiments are conducted to test predictions
5. Analysis: Results are analyzed and interpreted
6. Conclusion: The hypothesis is supported or rejected based on evidence
This method has been fundamental to scientific progress and has led to countless discoveries and technological advances.""",
"""Climate change refers to long-term shifts in global or regional climate patterns, attributed largely to increased concentrations of greenhouse gases in the atmosphere due to human activities. The primary greenhouse gases include carbon dioxide, methane, and nitrous oxide.
The effects of climate change include rising global temperatures, melting ice caps, rising sea levels, and changing precipitation patterns. These changes have significant impacts on ecosystems, agriculture, water resources, and human societies.
Mitigation strategies include reducing greenhouse gas emissions through renewable energy adoption, energy efficiency improvements, and carbon capture technologies. Adaptation strategies focus on building resilience to climate impacts through infrastructure improvements and ecosystem restoration.""",
"""Genetics is the study of heredity and the variation of inherited characteristics. DNA (deoxyribonucleic acid) contains the genetic instructions used in the development and functioning of all known living organisms. Genes are segments of DNA that code for specific traits.
Genetic inheritance follows patterns described by Mendel's laws, including the law of segregation and the law of independent assortment. Modern genetics has revealed the molecular basis of inheritance and has led to applications in medicine, agriculture, and biotechnology.
CRISPR-Cas9 is a revolutionary gene-editing technology that allows scientists to make precise changes to DNA sequences. This technology has potential applications in treating genetic diseases, improving crops, and advancing biological research."""
]
self.training_texts.extend(scientific_texts)
print(f"πŸ”¬ Added {len(scientific_texts)} scientific texts")
def _add_conversational_data(self):
"""Add conversational and Q&A data for better interaction"""
conversational_texts = [
"""Q: What is artificial intelligence?
A: Artificial intelligence (AI) is the simulation of human intelligence in machines that are programmed to think and learn like humans. AI systems can perform tasks that typically require human intelligence, such as visual perception, speech recognition, decision-making, and language translation.
Q: How does machine learning work?
A: Machine learning works by training algorithms on large datasets to identify patterns and relationships. The algorithm learns from examples and can then make predictions or decisions on new, unseen data. There are three main types: supervised learning (with labeled data), unsupervised learning (finding hidden patterns), and reinforcement learning (learning through rewards).
Q: What programming language should I learn first?
A: For beginners, Python is often recommended because of its simple, readable syntax and versatility. It's used in web development, data science, artificial intelligence, and automation. Other good options include JavaScript for web development or Java for enterprise applications.""",
"""Q: Explain quantum computing in simple terms.
A: Quantum computing uses quantum mechanical phenomena like superposition and entanglement to process information. While classical computers use bits that are either 0 or 1, quantum computers use quantum bits (qubits) that can be in multiple states simultaneously. This allows quantum computers to potentially solve certain problems much faster than classical computers.
Q: What is the difference between HTTP and HTTPS?
A: HTTP (Hypertext Transfer Protocol) is the foundation of data communication on the web. HTTPS (HTTP Secure) is the secure version that encrypts data transmission using SSL/TLS protocols. HTTPS protects against eavesdropping and tampering, making it essential for secure communications like online banking and e-commerce.
Q: How do neural networks learn?
A: Neural networks learn through a process called backpropagation. They start with random weights, make predictions, calculate the error, and then adjust weights backward through the network to minimize error. This process is repeated many times with training data until the network can make accurate predictions."""
]
self.training_texts.extend(conversational_texts)
print(f"πŸ’¬ Added {len(conversational_texts)} conversational examples")
def _add_encyclopedic_data(self):
"""Add encyclopedic knowledge for comprehensive coverage"""
encyclopedic_texts = [
"""Tesla, Inc. is an American electric vehicle and clean energy company founded by Elon Musk and others in 2003. Tesla designs and manufactures electric cars, battery energy storage systems, solar panels, and related products. The company is known for its innovative approach to sustainable transportation and has played a significant role in accelerating the adoption of electric vehicles worldwide.
Tesla's vehicles use advanced battery technology and autonomous driving features. The company operates Gigafactories that produce batteries and vehicles at scale. Tesla has also developed a network of Supercharger stations for fast charging of electric vehicles.""",
"""NVIDIA Corporation is an American multinational technology company known for designing graphics processing units (GPUs) for gaming and professional markets, as well as system on chip units (SoCs) for mobile and automotive applications. Founded in 1993, NVIDIA has become a leader in artificial intelligence computing and high-performance computing.
NVIDIA's GPUs have become essential for training deep learning models due to their parallel processing capabilities. The company's CUDA platform enables developers to use GPUs for general-purpose computing, not just graphics rendering.""",
"""The Internet was developed from ARPANET, a research project funded by the US Department of Defense in the late 1960s. The World Wide Web was invented by Tim Berners-Lee at CERN in 1989-1991. The combination of the Internet infrastructure and the Web protocol revolutionized communication, commerce, and information sharing.
Key technologies that enabled the Internet include TCP/IP protocols, domain name system (DNS), and HTTP. The Internet has evolved from connecting a few universities to becoming a global network connecting billions of devices."""
]
self.training_texts.extend(encyclopedic_texts)
print(f"πŸ“– Added {len(encyclopedic_texts)} encyclopedic entries")
def _process_training_data(self):
"""Process and tokenize all training data"""
print("πŸ”„ Processing and tokenizing training data...")
self.examples = []
for text in self.training_texts:
# Clean and prepare text
text = text.strip()
if len(text) < self.min_length:
continue
# Tokenize text
encodings = self.tokenizer(
text,
truncation=True,
max_length=self.max_length,
return_overflowing_tokens=True,
return_length=True,
padding=False
)
# Add each chunk as a training example
for input_ids, length in zip(encodings['input_ids'], encodings['length']):
if length >= self.min_length:
self.examples.append({
'input_ids': input_ids,
'attention_mask': [1] * len(input_ids),
'labels': input_ids.copy()
})
print(f"βœ… Processed {len(self.examples)} training examples")
def __len__(self):
return len(self.examples)
def __getitem__(self, idx):
return self.examples[idx]
class EnhancedTrainer(Trainer):
"""Enhanced trainer with improved training strategies"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.best_metrics = {}
def compute_loss(self, model, inputs, return_outputs=False):
"""Enhanced loss computation with label smoothing"""
labels = inputs.get("labels")
# Forward pass
outputs = model(**inputs)
if labels is not None:
# Enhanced loss with label smoothing
shift_logits = outputs.logits[..., :-1, :].contiguous()
shift_labels = labels[..., 1:].contiguous()
# Flatten for loss computation
shift_logits = shift_logits.view(-1, shift_logits.size(-1))
shift_labels = shift_labels.view(-1)
# Apply label smoothing for better generalization
loss_fct = torch.nn.CrossEntropyLoss(label_smoothing=0.1, ignore_index=-100)
loss = loss_fct(shift_logits, shift_labels)
else:
loss = outputs.loss
return (loss, outputs) if return_outputs else loss
def evaluate(self, eval_dataset=None, ignore_keys=None, metric_key_prefix="eval"):
"""Enhanced evaluation with additional metrics"""
eval_dataloader = self.get_eval_dataloader(eval_dataset)
output = self.evaluation_loop(
eval_dataloader,
description="Evaluation",
prediction_loss_only=True if self.compute_metrics is None else None,
ignore_keys=ignore_keys,
metric_key_prefix=metric_key_prefix,
)
# Calculate perplexity
if "eval_loss" in output.metrics:
try:
perplexity = torch.exp(torch.tensor(output.metrics["eval_loss"]))
output.metrics["eval_perplexity"] = perplexity.item()
except OverflowError:
output.metrics["eval_perplexity"] = float("inf")
return output
def create_enhanced_training_setup(
model,
tokenizer,
output_dir="./enhanced_illuminator_model",
num_train_epochs=3,
per_device_train_batch_size=4,
gradient_accumulation_steps=8,
learning_rate=5e-5,
warmup_steps=1000,
logging_steps=100,
save_steps=1000,
eval_steps=500,
):
"""Create enhanced training setup for maximum accuracy"""
print("πŸš€ Setting up enhanced training configuration...")
# Create comprehensive dataset
train_dataset = ComprehensiveDataset(tokenizer, max_length=512)
# Create a smaller validation dataset
val_size = min(1000, len(train_dataset) // 10)
val_indices = random.sample(range(len(train_dataset)), val_size)
val_dataset = torch.utils.data.Subset(train_dataset, val_indices)
print(f"πŸ“Š Training samples: {len(train_dataset)}")
print(f"πŸ“Š Validation samples: {len(val_dataset)}")
# Enhanced training arguments
training_args = TrainingArguments(
output_dir=output_dir,
overwrite_output_dir=True,
num_train_epochs=num_train_epochs,
per_device_train_batch_size=per_device_train_batch_size,
per_device_eval_batch_size=per_device_train_batch_size,
gradient_accumulation_steps=gradient_accumulation_steps,
learning_rate=learning_rate,
weight_decay=0.01,
warmup_steps=warmup_steps,
logging_steps=logging_steps,
logging_dir=f"{output_dir}/logs",
save_steps=save_steps,
eval_steps=eval_steps,
evaluation_strategy="steps",
save_strategy="steps",
load_best_model_at_end=True,
metric_for_best_model="eval_loss",
greater_is_better=False,
remove_unused_columns=False,
dataloader_num_workers=4,
fp16=torch.cuda.is_available(),
gradient_checkpointing=True,
report_to=["tensorboard"],
seed=42,
data_seed=42,
)
# Data collator with dynamic padding
data_collator = DataCollatorForLanguageModeling(
tokenizer=tokenizer,
mlm=False, # Causal LM, not masked LM
)
# Create enhanced trainer
trainer = EnhancedTrainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=val_dataset,
data_collator=data_collator,
tokenizer=tokenizer,
)
print("βœ… Enhanced training setup complete!")
return trainer
def train_enhanced_model():
"""Main training function with comprehensive data"""
print("πŸš€ Starting Enhanced Illuminator Model Training")
print("=" * 60)
# Import model and tokenizer
try:
from modeling_illuminator import IlluminatorLMHeadModel, IlluminatorConfig
from tokenization_illuminator import IlluminatorTokenizer
except ImportError:
print("❌ Could not import Illuminator model components")
print("Make sure modeling_illuminator.py and tokenization_illuminator.py are in the same directory")
return
# Initialize model and tokenizer
print("πŸ”§ Initializing model and tokenizer...")
config = IlluminatorConfig(
vocab_size=50257,
n_positions=512, # Smaller for training efficiency
n_embd=768, # Smaller for training efficiency
n_layer=12, # Smaller for training efficiency
n_head=12,
n_inner=3072,
)
model = IlluminatorLMHeadModel(config)
tokenizer = IlluminatorTokenizer()
# Add special tokens
special_tokens = {
"pad_token": "<|pad|>",
"eos_token": "<|eos|>",
"bos_token": "<|bos|>",
"unk_token": "<|unk|>"
}
tokenizer.add_special_tokens(special_tokens)
model.resize_token_embeddings(len(tokenizer))
print(f"πŸ“Š Model parameters: {model.num_parameters():,}")
print(f"πŸ“š Tokenizer vocabulary size: {len(tokenizer)}")
# Create training setup
trainer = create_enhanced_training_setup(
model=model,
tokenizer=tokenizer,
output_dir="./enhanced_illuminator_model",
num_train_epochs=5,
per_device_train_batch_size=2,
gradient_accumulation_steps=16,
learning_rate=1e-4,
warmup_steps=500,
)
print("πŸ‹οΈ Starting training...")
start_time = time.time()
# Train the model
trainer.train()
end_time = time.time()
training_time = end_time - start_time
print(f"⏱️ Training completed in {training_time/3600:.2f} hours")
# Save the model
print("πŸ’Ύ Saving model and tokenizer...")
trainer.save_model()
tokenizer.save_pretrained("./enhanced_illuminator_model")
# Save configuration
config_dict = {
"model_type": "illuminator",
"architectures": ["IlluminatorLMHeadModel"],
"vocab_size": len(tokenizer),
"n_positions": config.n_positions,
"n_embd": config.n_embd,
"n_layer": config.n_layer,
"n_head": config.n_head,
"training_data": "comprehensive_multilingual_dataset",
"training_epochs": 5,
"optimization": "AdamW with label smoothing",
}
with open("./enhanced_illuminator_model/config.json", "w") as f:
json.dump(config_dict, f, indent=2)
print("βœ… Model training and saving complete!")
print("\nπŸŽ‰ Enhanced Illuminator Model ready for Hugging Face Hub!")
if __name__ == "__main__":
train_enhanced_model()