File size: 12,403 Bytes
797f8cc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
import torch
from torch.utils.data import DataLoader
from transformers import AdamW, get_linear_schedule_with_warmup
from tqdm import tqdm
import time
import json
import os
from models import PersonaAssigner, PreferencePredictor, BERTEncoder
from data_utils import load_chatbot_data, load_personas, ChatbotDataset
from torch.cuda.amp import autocast, GradScaler
import sys
class SimpleDashboard:
def __init__(self):
self.stats = {
'iteration': 0,
'total_iterations': 500,
'batch': 0,
'total_batches': 575,
'baseline_loss': 0,
'baseline_accuracy': 0,
'enhanced_loss': 0,
'enhanced_accuracy': 0,
'last_save': '',
}
self.history = {
'baseline_loss': [],
'baseline_accuracy': [],
'enhanced_loss': [],
'enhanced_accuracy': [],
}
def update_stats(self, **kwargs):
self.stats.update(kwargs)
for key in ['baseline_loss', 'baseline_accuracy', 'enhanced_loss', 'enhanced_accuracy']:
if key in kwargs:
self.history[key].append(kwargs[key])
if len(self.history[key]) > 50: # Keep only last 50 points
self.history[key] = self.history[key][-50:]
def draw_ascii_chart(self, data, title, width=50, height=10):
if not data:
return ""
min_val, max_val = min(data), max(data)
range_val = max_val - min_val if max_val > min_val else 1
lines = [f"{title} (min: {min_val:.4f}, max: {max_val:.4f})"]
for i in range(height - 1, -1, -1):
line = ""
for val in data[-width:]:
if (val - min_val) / range_val > i / (height - 1):
line += "█"
else:
line += " "
lines.append(line)
return "\n".join(lines)
def draw(self):
os.system('cls' if os.name == 'nt' else 'clear')
print(f"Experiment Progress:")
print(f"Iteration: {self.stats['iteration']}/{self.stats['total_iterations']}")
print(f"Batch: {self.stats['batch']}/{self.stats['total_batches']}")
print(f"Baseline Loss: {self.stats['baseline_loss']:.4f}")
print(f"Baseline Accuracy: {self.stats['baseline_accuracy']:.2%}")
print(f"Enhanced Loss: {self.stats['enhanced_loss']:.4f}")
print(f"Enhanced Accuracy: {self.stats['enhanced_accuracy']:.2%}")
print(f"Last Save: {self.stats['last_save']}")
print("\n" + self.draw_ascii_chart(self.history['baseline_loss'], "Baseline Loss"))
print("\n" + self.draw_ascii_chart(self.history['enhanced_loss'], "Enhanced Loss"))
print("\n" + self.draw_ascii_chart(self.history['baseline_accuracy'], "Baseline Accuracy"))
print("\n" + self.draw_ascii_chart(self.history['enhanced_accuracy'], "Enhanced Accuracy"))
class ComparativeExperiment:
def __init__(self, batch_size=4, accumulation_steps=8, max_length=64):
print("Initializing ComparativeExperiment...")
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f"Using device: {self.device}")
self.batch_size = batch_size
self.accumulation_steps = accumulation_steps
self.max_length = max_length
print("Initializing models...")
self.bert_encoder = BERTEncoder().to(self.device)
self.persona_assigner = PersonaAssigner(768, 256, 768).to(self.device)
self.enhanced_predictor = PreferencePredictor(768 * 3).to(self.device)
self.baseline_predictor = PreferencePredictor(768 * 3).to(self.device)
print("Initializing optimizer...")
self.optimizer = AdamW([
{'params': self.persona_assigner.parameters()},
{'params': self.enhanced_predictor.parameters()},
{'params': self.baseline_predictor.parameters()}
], lr=1e-4)
self.criterion = torch.nn.CrossEntropyLoss()
self.scaler = GradScaler()
self.current_iteration = 0
self.current_step = 0
self.training_log = []
self.dashboard = SimpleDashboard()
print("ComparativeExperiment initialized.")
def train_iteration(self, batch):
self.persona_assigner.train()
self.enhanced_predictor.train()
self.baseline_predictor.train()
with autocast():
prompt_embeds = self.bert_encoder(
input_ids=batch['prompt']['input_ids'].squeeze(1),
attention_mask=batch['prompt']['attention_mask'].squeeze(1)
)
response_a_embeds = self.bert_encoder(
input_ids=batch['response_a']['input_ids'].squeeze(1),
attention_mask=batch['response_a']['attention_mask'].squeeze(1)
)
response_b_embeds = self.bert_encoder(
input_ids=batch['response_b']['input_ids'].squeeze(1),
attention_mask=batch['response_b']['attention_mask'].squeeze(1)
)
labels = batch['label'].to(self.device)
baseline_inputs = torch.cat([prompt_embeds, response_a_embeds, response_b_embeds], dim=1)
baseline_outputs = self.baseline_predictor(baseline_inputs)
baseline_loss = self.criterion(baseline_outputs, labels)
baseline_accuracy = (baseline_outputs.argmax(dim=1) == labels).float().mean().item()
prompt_personas = self.persona_assigner(prompt_embeds.detach())
response_a_personas = self.persona_assigner(response_a_embeds.detach())
response_b_personas = self.persona_assigner(response_b_embeds.detach())
combined_prompt = prompt_embeds + prompt_personas
combined_response_a = response_a_embeds + response_a_personas
combined_response_b = response_b_embeds + response_b_personas
enhanced_inputs = torch.cat([combined_prompt, combined_response_a, combined_response_b], dim=1)
enhanced_outputs = self.enhanced_predictor(enhanced_inputs)
enhanced_loss = self.criterion(enhanced_outputs, labels)
enhanced_accuracy = (enhanced_outputs.argmax(dim=1) == labels).float().mean().item()
total_loss = baseline_loss + enhanced_loss
scaled_loss = self.scaler.scale(total_loss)
scaled_loss.backward()
return baseline_loss.item(), baseline_accuracy, enhanced_loss.item(), enhanced_accuracy
def save_state(self, filename='experiment_state.pth'):
print(f"Saving state to {filename}...")
torch.save({
'current_iteration': self.current_iteration,
'current_step': self.current_step,
'bert_encoder': self.bert_encoder.state_dict(),
'persona_assigner': self.persona_assigner.state_dict(),
'enhanced_predictor': self.enhanced_predictor.state_dict(),
'baseline_predictor': self.baseline_predictor.state_dict(),
'optimizer': self.optimizer.state_dict(),
'scaler': self.scaler.state_dict(),
}, filename)
with open('training_log.json', 'w') as f:
json.dump(self.training_log, f)
self.dashboard.update_stats(last_save=time.strftime("%Y-%m-%d %H:%M:%S"))
print("State saved successfully.")
def load_state(self, filename='experiment_state.pth'):
if os.path.exists(filename):
print(f"Loading state from {filename}...")
state = torch.load(filename)
self.current_iteration = state['current_iteration']
self.current_step = state['current_step']
print(f"Loaded iteration: {self.current_iteration}, step: {self.current_step}")
self.bert_encoder.load_state_dict(state['bert_encoder'])
self.persona_assigner.load_state_dict(state['persona_assigner'])
self.enhanced_predictor.load_state_dict(state['enhanced_predictor'])
self.baseline_predictor.load_state_dict(state['baseline_predictor'])
self.optimizer.load_state_dict(state['optimizer'])
self.scaler.load_state_dict(state['scaler'])
if os.path.exists('training_log.json'):
with open('training_log.json', 'r') as f:
self.training_log = json.load(f)
print(f"Loaded training log with {len(self.training_log)} entries.")
print("State loaded successfully.")
return True
else:
print(f"No saved state found at {filename}.")
return False
def run_experiment(self, train_loader, num_iterations=500):
print(f"Starting experiment. Total iterations: {num_iterations}")
self.dashboard.update_stats(
total_iterations=num_iterations,
total_batches=len(train_loader)
)
try:
for iteration in range(self.current_iteration, num_iterations):
print(f"Starting iteration {iteration + 1}/{num_iterations}")
self.current_iteration = iteration
for i, batch in enumerate(train_loader):
batch = {k: v.to(self.device) for k, v in batch.items()}
baseline_loss, baseline_accuracy, enhanced_loss, enhanced_accuracy = self.train_iteration(batch)
self.current_step += 1
if self.current_step % self.accumulation_steps == 0:
self.scaler.step(self.optimizer)
self.scaler.update()
self.optimizer.zero_grad()
self.training_log.append({
'iteration': iteration + 1,
'batch': i + 1,
'baseline_loss': baseline_loss,
'baseline_accuracy': baseline_accuracy,
'enhanced_loss': enhanced_loss,
'enhanced_accuracy': enhanced_accuracy,
'timestamp': time.strftime("%Y-%m-%d %H:%M:%S")
})
self.dashboard.update_stats(
iteration=iteration + 1,
batch=i + 1,
baseline_loss=baseline_loss,
baseline_accuracy=baseline_accuracy,
enhanced_loss=enhanced_loss,
enhanced_accuracy=enhanced_accuracy
)
if (i + 1) % 10 == 0: # Update every 10 batches
self.dashboard.draw()
print(f"Completed iteration {iteration + 1}/{num_iterations}")
# Save state after each iteration
self.save_state()
self.dashboard.draw() # Draw dashboard after each iteration
self.save_state('final_experiment_state.pth')
print("Experiment completed.")
except KeyboardInterrupt:
print("Experiment interrupted. Saving state...")
self.save_state('interrupted_experiment_state.pth')
print("State saved. You can resume later by loading 'interrupted_experiment_state.pth'")
except Exception as e:
print(f"An error occurred: {str(e)}")
self.save_state('error_experiment_state.pth')
print("State saved due to error. You can resume later by loading 'error_experiment_state.pth'")
raise
def setup_experiment(train_data, train_labels, batch_size=4, max_length=64):
from transformers import BertTokenizer
print("Setting up experiment...")
print("Initializing tokenizer...")
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
print("Creating dataset...")
train_dataset = ChatbotDataset(
train_data['prompt'].tolist(),
train_data['response_a'].tolist(),
train_data['response_b'].tolist(),
train_labels,
tokenizer,
max_length=max_length
)
print(f"Creating DataLoader with batch size {batch_size}...")
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
print("Initializing ComparativeExperiment...")
experiment = ComparativeExperiment(batch_size=batch_size, max_length=max_length)
print("Experiment setup completed.")
return experiment, train_loader |