File size: 17,566 Bytes
8d18b7c | 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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 | """Advanced Data Augmentation for Training"""
import json
import logging
import random
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Any, Dict, List, Optional, Tuple
import numpy as np
logger = logging.getLogger(__name__)
@dataclass
class AugmentationConfig:
"""Configuration for data augmentation."""
enabled_methods: List[str] = field(default_factory=lambda: [
"synonym_replacement",
"back_translation",
"code_perturbation",
"paraphrasing",
"noise_injection",
])
probabilities: Dict[str, float] = field(default_factory=lambda: {
"synonym_replacement": 0.3,
"back_translation": 0.2,
"code_perturbation": 0.4,
"paraphrasing": 0.3,
"noise_injection": 0.1,
})
max_augmentations_per_sample: int = 2
class AugmentationMethod(ABC):
"""Base class for augmentation methods."""
@abstractmethod
def augment(self, sample: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""Apply augmentation to sample. Return augmented sample or None if failed."""
pass
@abstractmethod
def can_augment(self, sample: Dict[str, Any]) -> bool:
"""Check if sample can be augmented by this method."""
pass
class SynonymReplacement(AugmentationMethod):
"""Replace words with synonyms."""
def __init__(self, replacement_prob: float = 0.1):
self.replacement_prob = replacement_prob
# Simple synonym dictionary (in practice, use WordNet or embeddings)
self.synonyms = {
"good": ["excellent", "great", "fine", "quality", "superb"],
"bad": ["poor", "terrible", "awful", "inferior", "subpar"],
"big": ["large", "huge", "enormous", "massive", "giant"],
"small": ["tiny", "little", "miniature", "compact", "petite"],
"fast": ["quick", "rapid", "speedy", "swift", "expedited"],
"slow": ["sluggish", "leisurely", "unhurried", "gradual", "delayed"],
"create": ["build", "generate", "produce", "develop", "construct"],
"use": ["utilize", "employ", "apply", "leverage", "harness"],
"find": ["discover", "locate", "detect", "identify", "uncover"],
"improve": ["enhance", "upgrade", "optimize", "refine", "better"],
}
def can_augment(self, sample: Dict[str, Any]) -> bool:
"""Check if sample has text to augment."""
text = self._extract_text(sample)
return len(text.split()) > 10
def augment(self, sample: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""Replace random words with synonyms."""
text = self._extract_text(sample)
words = text.split()
# Replace random words
new_words = []
for word in words:
if random.random() < self.replacement_prob and word.lower() in self.synonyms:
synonym = random.choice(self.synonyms[word.lower()])
# Preserve capitalization
if word[0].isupper():
synonym = synonym.capitalize()
new_words.append(synonym)
else:
new_words.append(word)
new_text = " ".join(new_words)
if new_text == text:
return None
augmented = sample.copy()
self._replace_text(augmented, new_text)
augmented["augmentation"] = "synonym_replacement"
return augmented
def _extract_text(self, sample: Dict[str, Any]) -> str:
"""Extract text from sample."""
if "conversations" in sample:
conv = sample["conversations"]
if isinstance(conv, list):
return " ".join(msg.get("content", "") for msg in conv if isinstance(msg, dict))
return sample.get("text", sample.get("content", ""))
def _replace_text(self, sample: Dict[str, Any], new_text: str):
"""Replace text in sample."""
if "conversations" in sample:
conv = sample["conversations"]
if isinstance(conv, list):
# Replace content of first message
for msg in conv:
if isinstance(msg, dict) and "content" in msg:
msg["content"] = new_text[:len(msg["content"])]
break
else:
sample["text"] = new_text
sample["content"] = new_text
class CodePerturbation(AugmentationMethod):
"""Perturb code while preserving functionality."""
def __init__(self):
self.perturbations = [
self._rename_variables,
self._reorder_statements,
self._add_redundant_parentheses,
self._change_loop_style,
self._add_comments,
]
def can_augment(self, sample: Dict[str, Any]) -> bool:
"""Check if sample has code."""
return "code" in sample or any(
"```" in str(conv.get("content", ""))
for conv in sample.get("conversations", [])
if isinstance(conv, dict)
)
def augment(self, sample: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""Apply random code perturbation."""
code = self._extract_code(sample)
if not code:
return None
# Apply random perturbation
perturbation = random.choice(self.perturbations)
new_code = perturbation(code)
if new_code == code:
return None
augmented = sample.copy()
self._replace_code(augmented, new_code)
augmented["augmentation"] = f"code_perturbation:{perturbation.__name__}"
return augmented
def _extract_code(self, sample: Dict[str, Any]) -> str:
"""Extract code from sample."""
if "code" in sample:
return sample["code"]
# Look for code blocks in conversations
for conv in sample.get("conversations", []):
if isinstance(conv, dict):
content = conv.get("content", "")
if "```" in content:
# Extract code block
parts = content.split("```")
if len(parts) >= 2:
return parts[1].strip()
return ""
def _replace_code(self, sample: Dict[str, Any], new_code: str):
"""Replace code in sample."""
if "code" in sample:
sample["code"] = new_code
else:
for conv in sample.get("conversations", []):
if isinstance(conv, dict) and "```" in conv.get("content", ""):
parts = conv["content"].split("```")
conv["content"] = f"```{new_code}```"
def _rename_variables(self, code: str) -> str:
"""Rename variables to random names (simple version)."""
# This is a simplified version - in practice use AST parsing
import re
# Find variable names (simplistic)
variables = re.findall(r'\b([a-zA-Z_][a-zA-Z0-9_]*)\b', code)
unique_vars = set(variables)
# Generate random replacements
replacements = {}
for var in unique_vars:
if len(var) > 1 and var not in ["if", "for", "while", "def", "class", "return", "import", "from"]:
new_name = f"var_{random.randint(1000, 9999)}"
replacements[var] = new_name
# Replace
for old, new in replacements.items():
code = code.replace(old, new)
return code
def _reorder_statements(self, code: str) -> str:
"""Reorder independent statements."""
lines = code.split('\n')
# Simple: shuffle non-indented lines (top-level statements)
# This is risky - only apply to simple code
return code # TODO: Implement safely
def _add_redundant_parentheses(self, code: str) -> str:
"""Add redundant parentheses."""
# Simplistic: add parentheses around binary operations
import re
# This is placeholder - would need proper parsing
return code
def _change_loop_style(self, code: str) -> str:
"""Change between for loops and while loops where possible."""
# This requires AST parsing - placeholder
return code
def _add_comments(self, code: str) -> str:
"""Add explanatory comments."""
lines = code.split('\n')
new_lines = []
for i, line in enumerate(lines):
new_lines.append(line)
if line.strip() and not line.strip().startswith('#'):
if random.random() < 0.2:
new_lines.append(f"# TODO: Explain this line")
return '\n'.join(new_lines)
class BackTranslation(AugmentationMethod):
"""Simulate back-translation by paraphrasing."""
def __init__(self):
self.paraphrase_templates = [
"In other words, {text}",
"To put it differently, {text}",
"That is to say, {text}",
"Alternatively, {text}",
]
def can_augment(self, sample: Dict[str, Any]) -> bool:
"""Check if sample has text suitable for back translation."""
text = self._extract_text(sample)
return len(text) > 50
def augment(self, sample: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""Apply back translation simulation."""
text = self._extract_text(sample)
template = random.choice(self.paraphrase_templates)
new_text = template.format(text=text[:200]) + text[200:] # Add prefix
if new_text == text:
return None
augmented = sample.copy()
self._replace_text(augmented, new_text)
augmented["augmentation"] = "back_translation"
return augmented
def _extract_text(self, sample: Dict[str, Any]) -> str:
"""Extract text from sample."""
if "conversations" in sample:
conv = sample["conversations"]
if isinstance(conv, list):
return " ".join(msg.get("content", "") for msg in conv if isinstance(msg, dict))
return sample.get("text", sample.get("content", ""))
def _replace_text(self, sample: Dict[str, Any], new_text: str):
"""Replace text in sample."""
if "conversations" in sample:
conv = sample["conversations"]
if isinstance(conv, list):
for msg in conv:
if isinstance(msg, dict) and "content" in msg:
msg["content"] = new_text[:len(msg["content"])]
break
else:
sample["text"] = new_text
sample["content"] = new_text
class Paraphrasing(AugmentationMethod):
"""Paraphrase text using templates."""
def __init__(self):
self.paraphrase_patterns = [
(r"\b(is)\b", ["represents", "constitutes", "means"]),
(r"\b(has)\b", ["contains", "possesses", "includes"]),
(r"\b(use)\b", ["utilize", "employ", "leverage"]),
(r"\b(make)\b", ["create", "build", "produce"]),
(r"\b(find)\b", ["discover", "locate", "identify"]),
]
def can_augment(self, sample: Dict[str, Any]) -> bool:
"""Check if sample has text."""
text = self._extract_text(sample)
return len(text) > 30
def augment(self, sample: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""Apply paraphrasing."""
text = self._extract_text(sample)
new_text = text
# Apply random pattern
pattern, replacements = random.choice(self.paraphrase_patterns)
import re
matches = re.findall(pattern, text, re.IGNORECASE)
if matches:
# Replace first occurrence
old_word = matches[0]
new_word = random.choice(replacements)
new_text = re.sub(pattern, new_word, text, count=1, flags=re.IGNORECASE)
if new_text == text:
return None
augmented = sample.copy()
self._replace_text(augmented, new_text)
augmented["augmentation"] = "paraphrasing"
return augmented
def _extract_text(self, sample: Dict[str, Any]) -> str:
"""Extract text from sample."""
if "conversations" in sample:
conv = sample["conversations"]
if isinstance(conv, list):
return " ".join(msg.get("content", "") for msg in conv if isinstance(msg, dict))
return sample.get("text", sample.get("content", ""))
def _replace_text(self, sample: Dict[str, Any], new_text: str):
"""Replace text in sample."""
if "conversations" in sample:
conv = sample["conversations"]
if isinstance(conv, list):
for msg in conv:
if isinstance(msg, dict) and "content" in msg:
msg["content"] = new_text[:len(msg["content"])]
break
else:
sample["text"] = new_text
sample["content"] = new_text
class NoiseInjection(AugmentationMethod):
"""Inject noise into text."""
def __init__(self, noise_prob: float = 0.01):
self.noise_prob = noise_prob
self.noise_tokens = ["[MASK]", "<noise>", "...", "[UNK]"]
def can_augment(self, sample: Dict[str, Any]) -> bool:
"""Check if sample has text."""
text = self._extract_text(sample)
return len(text) > 20
def augment(self, sample: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""Inject noise tokens."""
text = self._extract_text(sample)
words = text.split()
# Randomly replace words with noise
new_words = []
for word in words:
if random.random() < self.noise_prob and len(word) > 3:
new_words.append(random.choice(self.noise_tokens))
else:
new_words.append(word)
new_text = " ".join(new_words)
if new_text == text:
return None
augmented = sample.copy()
self._replace_text(augmented, new_text)
augmented["augmentation"] = "noise_injection"
return augmented
def _extract_text(self, sample: Dict[str, Any]) -> str:
"""Extract text from sample."""
if "conversations" in sample:
conv = sample["conversations"]
if isinstance(conv, list):
return " ".join(msg.get("content", "") for msg in conv if isinstance(msg, dict))
return sample.get("text", sample.get("content", ""))
def _replace_text(self, sample: Dict[str, Any], new_text: str):
"""Replace text in sample."""
if "conversations" in sample:
conv = sample["conversations"]
if isinstance(conv, list):
for msg in conv:
if isinstance(msg, dict) and "content" in msg:
msg["content"] = new_text[:len(msg["content"])]
break
else:
sample["text"] = new_text
sample["content"] = new_text
class DataAugmenter:
"""Manages multiple augmentation methods."""
def __init__(self, config: AugmentationConfig):
self.config = config
self.methods: Dict[str, AugmentationMethod] = {
"synonym_replacement": SynonymReplacement(),
"back_translation": BackTranslation(),
"code_perturbation": CodePerturbation(),
"paraphrasing": Paraphrasing(),
"noise_injection": NoiseInjection(),
}
def augment(self, sample: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""Apply random augmentation to sample."""
# Choose random enabled method
enabled_methods = [
m for m in self.config.enabled_methods
if m in self.methods and self.methods[m].can_augment(sample)
]
if not enabled_methods:
return None
method_name = random.choice(enabled_methods)
method = self.methods[method_name]
# Apply augmentation
augmented = method.augment(sample)
if augmented:
augmented["augmentation_applied"] = method_name
return augmented
def augment_batch(
self,
batch: List[Dict[str, Any]],
augmentation_ratio: float = 0.1,
) -> List[Dict[str, Any]]:
"""Augment a batch of samples."""
augmented_batch = []
for sample in batch:
augmented_batch.append(sample)
if random.random() < augmentation_ratio:
augmented = self.augment(sample)
if augmented:
augmented_batch.append(augmented)
return augmented_batch
def augment_sample(
sample: Dict[str, Any],
methods: List[str],
max_augmentations: int = 2,
) -> List[Dict[str, Any]]:
"""Augment a single sample with multiple methods."""
augmenter = DataAugmenter(AugmentationConfig(enabled_methods=methods))
results = [sample]
for _ in range(max_augmentations):
augmented = augmenter.augment(sample)
if augmented:
results.append(augmented)
return results
|