### Configure and Use EvolutionDirector Source: https://context7.com/lucidrains/transformer-directed-evolution/llms.txt Shows how to instantiate an EvolutionDirector transformer and use it to guide the genetic algorithm's evolution process. ```python import torch from transformer_directed_evolution import ToyGeneticAlgorithmEnv, EvolutionDirector # Create the genetic algorithm environment env = ToyGeneticAlgorithmEnv( goal='Hello World', population_size=50, max_steps=200 ) # Create the evolution director with transformer configuration director = EvolutionDirector( dim_genome=env.gene_length, # Input dimension matches gene length transformer=dict( # x-transformers Encoder config dim=64, # Model dimension depth=2, # Number of transformer layers attn_dim_head=64, # Attention head dimension heads=4, # Number of attention heads ), mutation_rate_bins=25, # Discrete bins for mutation rate max_mutation_rate=0.2, # Maximum allowed mutation rate critic_fitness_weight=1.0, # Weight for fitness in critic critic_diversity_weight=0.5, # Weight for diversity in critic ) # Run genetic algorithm WITH the trained director trials = 5 results_with_director, fitness_with = env.run( num_trials=trials, director=director, pass_fitness_to_director=True, # Include fitness info in director input display=False ) # Run WITHOUT director for comparison env.reset() results_without_director, fitness_without = env.run(num_trials=trials) print(f"With Director - Avg generations: {results_with_director.float().mean():.1f}") print(f"Without Director - Avg generations: {results_without_director.float().mean():.1f}") ``` -------------------------------- ### Initialize and Run ToyGeneticAlgorithmEnv Source: https://context7.com/lucidrains/transformer-directed-evolution/llms.txt Demonstrates setting up a genetic algorithm environment to evolve strings towards a target and executing the evolution process. ```python import torch from transformer_directed_evolution import ToyGeneticAlgorithmEnv # Create a genetic algorithm environment with custom parameters env = ToyGeneticAlgorithmEnv( goal='Attention is all you need', # Target string to evolve towards population_size=100, # Number of individuals in population mutation_rate=0.05, # Probability of gene mutation frac_fittest_survive=0.25, # Top 25% survive each generation frac_tournament=0.25, # Tournament selection fraction display=False, # Print population each generation max_steps=500 # Maximum generations before timeout ) # Run the genetic algorithm without any external direction num_trials = 10 generations_completed, max_fitnesses = env.run(num_trials=num_trials) print(f"Average generations to converge: {generations_completed.float().mean():.1f}") print(f"Max fitness achieved: {max_fitnesses.max():.4f}") # Access environment properties print(f"Gene length: {env.gene_length}") print(f"Population diversity: {env.diversity:.4f}") print(f"Current generation: {env.generation.item()}") # Manually reset and step through the environment env.reset() done, fitnesses = env.forward(display=True) # Single generation step ``` -------------------------------- ### Calculate Generalized Advantage Estimation (GAE) Source: https://context7.com/lucidrains/transformer-directed-evolution/llms.txt Computes advantages for PPO training using simulated trajectory data. Supports GPU acceleration via associative scans. Ensure rewards, values, and masks are provided as PyTorch tensors. ```python import torch from transformer_directed_evolution.evolution_director import calc_generalized_advantage_estimate # Simulated trajectory data from environment rollout sequence_length = 100 rewards = torch.randn(sequence_length) # Rewards at each timestep values = torch.randn(sequence_length) # Value estimates from critic masks = torch.ones(sequence_length) # Episode continuation masks (0 at episode end) # Calculate GAE advantages advantages = calc_generalized_advantage_estimate( rewards=rewards, values=values, masks=masks, gamma=0.99, # Discount factor lam=0.95, # GAE lambda for bias-variance tradeoff use_accelerated=None # Auto-detect GPU for accelerated scan ) print(f"Advantages shape: {advantages.shape}") print(f"Advantage mean: {advantages.mean():.4f}") print(f"Advantage std: {advantages.std():.4f}") # GPU example with explicit acceleration if torch.cuda.is_available(): rewards_gpu = rewards.cuda() values_gpu = values.cuda() masks_gpu = masks.cuda() advantages_gpu = calc_generalized_advantage_estimate( rewards_gpu, values_gpu, masks_gpu, use_accelerated=True # Force accelerated associative scan ) print(f"GPU advantages computed: {advantages_gpu.device}") ``` -------------------------------- ### Calculate PPO Loss Functions Source: https://context7.com/lucidrains/transformer-directed-evolution/llms.txt Computes actor and critic losses for training the director using Proximal Policy Optimization objectives. ```python import torch from transformer_directed_evolution import EvolutionDirector director = EvolutionDirector( dim_genome=10, transformer=dict(dim=32, depth=1, heads=2, attn_dim_head=16), critic_fitness_weight=1.0, critic_diversity_weight=0.5 ) # Example training data (typically collected from environment rollouts) batch_size = 32 num_actions = 25 # mutation_rate_bins # Simulated training tensors logits = torch.randn(batch_size, num_actions) old_log_probs = torch.randn(batch_size, 1) actions = torch.randint(0, num_actions, (batch_size, 1)) advantages = torch.randn(2, batch_size) # [fitness_advantage, diversity_advantage] values = torch.randn(batch_size) rewards = torch.randn(batch_size) # Calculate actor loss (PPO clipped surrogate objective) actor_loss = director.actor_loss( logits=logits, old_log_probs=old_log_probs, actions=actions, advantages=advantages, eps_clip=0.2, # PPO clip parameter entropy_weight=0.01, # Entropy bonus for exploration norm_eps=1e-5 # Layer norm epsilon ) # Calculate critic loss (value function MSE) critic_loss = director.critic_loss( advantages=advantages, values=values, rewards=rewards ) print(f"Actor loss: {actor_loss.item():.4f}") print(f"Critic loss: {critic_loss.item():.4f}") # Combined loss for optimization total_loss = actor_loss + 0.5 * critic_loss total_loss.backward() ``` -------------------------------- ### Interact with Environment via Generator Source: https://context7.com/lucidrains/transformer-directed-evolution/llms.txt Utilizes a Python generator interface for step-by-step control over the genetic algorithm evolution process. ```python import torch from transformer_directed_evolution import ToyGeneticAlgorithmEnv, EvolutionDirector env = ToyGeneticAlgorithmEnv(goal='Evolution', population_size=30, max_steps=100) director = EvolutionDirector( dim_genome=env.gene_length, transformer=dict(dim=32, depth=1, heads=2, attn_dim_head=16) ) # Create generator for step-by-step control env.reset() gen = env.to_environment_generator() # Get initial state state, parent_ids, fitnesses = next(gen) print(f"Initial population shape: {state.shape}") # Step through evolution with director guidance done = False generation = 0 while not done and generation < 50: # Get director's recommendations with torch.no_grad(): actions = director(state, parent_ids, fitnesses=fitnesses) # Add display flag and send actions to generator actions['display'] = (generation % 10 == 0) # Display every 10 generations # Advance one generation state, parent_ids, fitnesses, diversity, done = gen.send(actions) print(f"Gen {generation}: Max fitness={fitnesses.max():.4f}, Diversity={diversity:.4f}") generation += 1 print(f"Evolution completed at generation {env.generation.item()}") ``` -------------------------------- ### Perform Forward Pass with EvolutionDirector Source: https://context7.com/lucidrains/transformer-directed-evolution/llms.txt Processes gene pools and parent selection to generate crossover masks and mutation rates for environment steps. ```python import torch from transformer_directed_evolution import ToyGeneticAlgorithmEnv, EvolutionDirector env = ToyGeneticAlgorithmEnv(goal='Test', population_size=20) director = EvolutionDirector( dim_genome=env.gene_length, transformer=dict(dim=32, depth=1, heads=2, attn_dim_head=16) ) # Get current state from environment env.reset() done, fitnesses = env.forward() # Initialize environment genome_pool = env.gene_pool # Shape: (population_size, gene_length) parent_ids = env.parent_ids # Shape: (num_children, 2) # Get director's recommended actions actions = director( genome_pool=genome_pool, parent_ids=parent_ids, fitnesses=fitnesses, # Optional: include fitness information pred_selection_operator=False, # Whether to predict selection mask natural_selection_size=None # Size if using selection operator ) print(f"Crossover mask shape: {actions['crossover_mask'].shape}") print(f"Mutation rate: {actions['mutation_rate'].item():.4f}") # Apply director's actions to environment step done, new_fitnesses = env.forward( crossover_mask=actions['crossover_mask'], mutation_rate=actions['mutation_rate'], mutation_strength=0.5, # Strength of mutations when applied display=False ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.