### Install grokfast-pytorch Source: https://github.com/lucidrains/grokfast-pytorch/blob/main/README.md Install the grokfast-pytorch package using pip. ```bash pip install grokfast-pytorch ``` -------------------------------- ### Grokfast-PyTorch Usage Example Source: https://github.com/lucidrains/grokfast-pytorch/blob/main/README.md Demonstrates how to use GrokFastAdamW optimizer with a PyTorch model. Ensure GrokFastAdamW is imported before instantiation. ```python import torch from torch import nn # toy model model = nn.Linear(10, 1) # import GrokFastAdamW and instantiate with parameters from grokfast_pytorch import GrokFastAdamW opt = GrokFastAdamW( model.parameters(), lr = 1e-4, weight_decay = 1e-2 ) # forward and backwards loss = model(torch.randn(10)) loss.backward() # optimizer step opt.step() opt.zero_grad() ``` -------------------------------- ### Initialize and Use GrokFastAdamW Optimizer Source: https://context7.com/lucidrains/grokfast-pytorch/llms.txt Initialize GrokFastAdamW with grokfast enabled and use it in a standard PyTorch training loop. The learning rate is automatically normalized when grokfast is true. ```python import torch from torch import nn from grokfast_pytorch import GrokFastAdamW # Create a simple neural network model = nn.Sequential( nn.Linear(64, 128), nn.ReLU(), nn.Linear(128, 64), nn.Linear(64, 10) ) # Initialize GrokFastAdamW with default grokfast settings optimizer = GrokFastAdamW( model.parameters(), lr=1e-3, # Learning rate (auto-normalized when grokfast=True) betas=(0.9, 0.99), # AdamW momentum coefficients weight_decay=1e-2, # Decoupled weight decay eps=1e-8, # Numerical stability epsilon grokfast=True, # Enable grokfast gradient amplification grokfast_alpha=0.98, # EMA decay rate for slow gradient detection grokfast_lamb=2.0, # Amplification factor for slow gradients grokfast_after_step=0, # Start grokfast after N steps normalize_lr=True # Normalize LR by (1 + lambda) for fair comparison ) # Training loop for epoch in range(100): inputs = torch.randn(32, 64) targets = torch.randint(0, 10, (32,)) # Forward pass outputs = model(inputs) loss = nn.functional.cross_entropy(outputs, targets) # Backward pass loss.backward() # Optimizer step optimizer.step() optimizer.zero_grad() if epoch % 10 == 0: print(f"Epoch {epoch}, Loss: {loss.item():.4f}") ``` -------------------------------- ### Enable spectral entropy regularization Source: https://context7.com/lucidrains/grokfast-pytorch/llms.txt Configures GrokFastAdamW to compute and backpropagate spectral entropy loss via a pre-step hook. ```python import torch from torch import nn from grokfast_pytorch import GrokFastAdamW model = nn.Sequential( nn.Linear(64, 128), nn.Linear(128, 64), nn.Linear(64, 10) ) # Enable spectral entropy regularization optimizer = GrokFastAdamW( model.parameters(), lr=1e-3, weight_decay=1e-2, grokfast=True, grokfast_lamb=2.0, add_spectral_entropy_reg=True, # Enable spectral entropy spectral_entropy_reg_weight=0.1, # Weight for entropy loss use_svd_lowrank=False, # Use full SVD (set True for large matrices) svd_lowrank_kwargs=dict(q=6, niter=2) # kwargs for torch.svd_lowrank if enabled ) # Training with spectral entropy regularization for step in range(200): inputs = torch.randn(32, 64) targets = torch.randint(0, 10, (32,)) outputs = model(inputs) loss = nn.functional.cross_entropy(outputs, targets) loss.backward() # Spectral entropy is computed and backpropagated automatically via hook optimizer.step() optimizer.zero_grad() ``` -------------------------------- ### Initialize GrokFastAdamW with Regenerative Regularization Source: https://context7.com/lucidrains/grokfast-pytorch/llms.txt Configure GrokFastAdamW to use regenerative regularization instead of weight decay. Ensure weight decay is set to 0 when using regen_reg_rate. ```python import torch from torch import nn from grokfast_pytorch import GrokFastAdamW model = nn.Sequential( nn.Linear(64, 128), nn.ReLU(), nn.Linear(128, 10) ) # Use regenerative regularization instead of weight decay optimizer = GrokFastAdamW( model.parameters(), lr=1e-3, weight_decay=0., # Must be 0 when using regen_reg regen_reg_rate=0.01, # Regularize toward initial parameters grokfast=True, grokfast_alpha=0.98, grokfast_lamb=2.0 ) ``` -------------------------------- ### Train on multiple tasks in a continual learning scenario Source: https://context7.com/lucidrains/grokfast-pytorch/llms.txt Standard training loop structure for sequential task learning. ```python for task_id in range(3): print(f"Training on task {task_id}") for step in range(100): inputs = torch.randn(16, 64) targets = torch.randint(0, 10, (16,)) outputs = model(inputs) loss = nn.functional.cross_entropy(outputs, targets) loss.backward() optimizer.step() optimizer.zero_grad() ``` -------------------------------- ### Delay Grokfast activation Source: https://context7.com/lucidrains/grokfast-pytorch/llms.txt Uses the grokfast_after_step parameter to defer gradient amplification until a specific training step. ```python import torch from torch import nn from grokfast_pytorch import GrokFastAdamW model = nn.Linear(256, 10) # Grokfast activates automatically after step 500 optimizer = GrokFastAdamW( model.parameters(), lr=1e-3, weight_decay=1e-2, grokfast=True, grokfast_alpha=0.98, grokfast_lamb=2.0, grokfast_after_step=500 # Start amplifying slow gradients after step 500 ) # Training loop - grokfast kicks in automatically at step 500 for step in range(1000): inputs = torch.randn(32, 256) loss = model(inputs).sum() loss.backward() optimizer.step() optimizer.zero_grad() if step == 500: print("Grokfast now active!") ``` -------------------------------- ### Control Grokfast Mechanism During Training Source: https://context7.com/lucidrains/grokfast-pytorch/llms.txt Dynamically enable, disable, or reset the grokfast mechanism using optimizer methods. This is useful for experiments or enabling grokfast after a warmup period. ```python import torch from torch import nn from grokfast_pytorch import GrokFastAdamW model = nn.Linear(128, 10) # Initialize with grokfast enabled optimizer = GrokFastAdamW( model.parameters(), lr=1e-3, grokfast=True, grokfast_alpha=0.98, grokfast_lamb=2.0 ) # Training with warmup: disable grokfast initially optimizer.turn_off_grokfast() for step in range(1000): inputs = torch.randn(16, 128) loss = model(inputs).sum() loss.backward() optimizer.step() optimizer.zero_grad() # Enable grokfast after 100 warmup steps if step == 100: optimizer.turn_on_grokfast() print("Grokfast enabled at step 100") # Optionally reset grokfast EMA state if step == 500: optimizer.clear_grokfast_state() print("Grokfast state cleared at step 500") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.