Spaces:
Sleeping
Sleeping
| # evo_transformer.py | |
| import random | |
| import json | |
| class EvoTransformer: | |
| def __init__(self, config=None): | |
| self.config = config or { | |
| "layers": 4, | |
| "attention_heads": 4, | |
| "ffn_dim": 1024, | |
| "dropout": 0.1, | |
| "memory": False, | |
| } | |
| self.history = [self.config.copy()] | |
| def mutate(self): | |
| new_config = self.config.copy() | |
| trait = random.choice(list(new_config.keys())) | |
| if trait == "layers": | |
| new_config[trait] = max(1, new_config[trait] + random.choice([-1, 1])) | |
| elif trait == "attention_heads": | |
| new_config[trait] = random.choice([2, 4, 6, 8]) | |
| elif trait == "ffn_dim": | |
| new_config[trait] = random.choice([512, 1024, 2048]) | |
| elif trait == "dropout": | |
| change = round(random.uniform(-0.05, 0.05), 2) | |
| new_config[trait] = round(min(max(0.0, new_config[trait] + change), 0.5), 2) | |
| elif trait == "memory": | |
| new_config[trait] = not new_config[trait] | |
| self.config = new_config | |
| self.history.append(new_config.copy()) | |
| def evolve(self, generations=3): | |
| for _ in range(generations): | |
| self.mutate() | |
| def get_history(self): | |
| return self.history | |
| def evaluate(self): | |
| # Simulated accuracy and parameter count | |
| accuracy = round(0.8 + random.uniform(0.01, 0.15), 4) | |
| return { | |
| "accuracy": accuracy, | |
| "params": self.estimate_params() | |
| } | |
| def estimate_params(self): | |
| # Lightweight param estimate formula (not real count) | |
| multiplier = 1.0 + (0.5 if self.config["memory"] else 0.0) | |
| return int(self.config["layers"] * self.config["ffn_dim"] * multiplier) | |
| def export_csv(self): | |
| import pandas as pd | |
| df = pd.DataFrame(self.history) | |
| return df.to_csv(index=False) | |
| def export_json(self): | |
| return json.dumps(self.history, indent=2) | |