| 5 | |
| 6 | @dataclass |
| 7 | class Config: |
| 8 | model_name: str = "neuralforge_model" |
| 9 | batch_size: int = 32 |
| 10 | epochs: int = 100 |
| 11 | learning_rate: float = 0.001 |
| 12 | weight_decay: float = 0.0001 |
| 13 | optimizer: str = "adamw" |
| 14 | scheduler: str = "cosine" |
| 15 | warmup_epochs: int = 5 |
| 16 | grad_clip: float = 1.0 |
| 17 | |
| 18 | data_path: str = "./data" |
| 19 | num_workers: int = 4 |
| 20 | pin_memory: bool = True |
| 21 | |
| 22 | model_dir: str = "./models" |
| 23 | log_dir: str = "./logs" |
| 24 | checkpoint_freq: int = 10 |
| 25 | |
| 26 | use_amp: bool = True |
| 27 | device: str = "cuda" |
| 28 | seed: int = 42 |
| 29 | |
| 30 | nas_enabled: bool = False |
| 31 | nas_population_size: int = 20 |
| 32 | nas_generations: int = 50 |
| 33 | nas_mutation_rate: float = 0.1 |
| 34 | |
| 35 | image_size: int = 224 |
| 36 | num_classes: int = 1000 |
| 37 | |
| 38 | def save(self, path: str): |
| 39 | os.makedirs(os.path.dirname(path), exist_ok=True) |
| 40 | with open(path, 'w') as f: |
| 41 | json.dump(asdict(self), f, indent=2) |
| 42 | |
| 43 | @classmethod |
| 44 | def load(cls, path: str) -> 'Config': |
| 45 | with open(path, 'r') as f: |
| 46 | data = json.load(f) |
| 47 | return cls(**data) |
| 48 | |
| 49 | def update(self, **kwargs): |
| 50 | for key, value in kwargs.items(): |
| 51 | if hasattr(self, key): |
| 52 | setattr(self, key, value) |
| 53 | |
| 54 | def __str__(self) -> str: |
| 55 | return json.dumps(asdict(self), indent=2) |