### Install rewind-reward-pytorch Source: https://github.com/lucidrains/rewind-reward-pytorch/blob/main/README.md Install the library using pip. This is the initial setup step before using the library. ```bash pip install rewind-reward-pytorch ``` -------------------------------- ### Complete Training Example with RewindReward Pytorch Source: https://context7.com/lucidrains/rewind-reward-pytorch/llms.txt An end-to-end example covering model initialization with custom decoder, optimizer setup, a training loop using RewindTrainWrapper, and inference for predicting progress rewards. Ensure 'num_epochs' and 'dataloader' are defined. ```python import torch from torch.optim import AdamW from rewind_reward_pytorch import RewardModel, RewindTrainWrapper # Initialize model and wrapper reward_model = RewardModel( decoder=dict(dim=768, depth=4, heads=8, attn_dim_head=64), categorical_rewards=False, use_hl_gauss_loss=True ) train_wrapper = RewindTrainWrapper( reward_model, rewind_augmentation_prob=0.5 ) # Setup optimizer (only train specific parameters) optimizer = AdamW(reward_model.parameters(), lr=1e-4) # Training loop for epoch in range(num_epochs): for batch in dataloader: commands = batch['commands'] # List of natural language commands video = batch['video'] # (B, C, T, H, W) tensor video_lens = batch['lengths'] # (B,) actual video lengths optimizer.zero_grad() # Forward pass with rewind augmentation loss = train_wrapper( commands, video, video_lens=video_lens ) loss.backward() optimizer.step() print(f"Epoch {epoch}, Loss: {loss.item():.4f}") # Inference reward_model.eval() with torch.no_grad(): test_commands = ['grasp the yellow block and place it on the shelf'] test_video = torch.rand(1, 3, 16, 224, 224) # Predict per-frame progress rewards progress_rewards = reward_model(test_commands, test_video) # Shape: (1, 16) - rewards for each of the 16 frames print(f"Predicted progress: {progress_rewards[0].tolist()}") ``` -------------------------------- ### Initialize and Use RewindTrainWrapper Source: https://context7.com/lucidrains/rewind-reward-pytorch/llms.txt Demonstrates initializing the RewardModel with continuous rewards and wrapping it with RewindTrainWrapper for data augmentation. Shows basic training loop usage with and without specified video lengths, and how to retrieve augmentation details. ```python import torch from rewind_reward_pytorch import RewardModel, RewindTrainWrapper # Initialize reward model (must use continuous rewards) reward_model = RewardModel( categorical_rewards=False ) # Wrap with ReWiND augmentation wrapper = RewindTrainWrapper( reward_model, rewind_augmentation_prob=0.25 # 25% chance of applying rewind augmentation ) commands = [ 'pick up the blue ball and put it in the red tray', 'pick up the red cube and put it in the green bin', ] video = torch.rand(2, 3, 16, 224, 224) # Basic training loop loss = wrapper(commands, video) loss.backward() # Training with video lengths specified loss = wrapper( commands, video, video_lens=torch.randint(5, 16, (2,)) ) loss.backward() # Get augmentation details for debugging/visualization loss, (is_augmented, aug_video, aug_lens, progress) = wrapper( commands, video, return_maybe_augmented=True ) print(f"Rewind augmentation applied: {is_augmented}") print(f"Augmented video shape: {aug_video.shape}") print(f"Video lengths after augmentation: {aug_lens}") print(f"Progress labels: {progress}") ``` -------------------------------- ### Initialize and Train ReWiND Reward Model Source: https://github.com/lucidrains/rewind-reward-pytorch/blob/main/README.md Initialize the RewardModel and RewindTrainWrapper. The wrapper is used for training, accepting commands and video data to compute a loss. Ensure commands and video tensors are correctly formatted. ```python import torch from rewind_reward_pytorch import ( RewardModel, RewindTrainWrapper ) reward_model = RewardModel() wrapper = RewindTrainWrapper( reward_model, rewind_augmentation_prob = 0.25 ) commands = [ 'pick up the blue ball and put it in the red tray', 'pick up the red cube and put it in the green bin', ] video = torch.rand(2, 3, 16, 224, 224) loss = wrapper( commands, video ) loss.backward() ``` -------------------------------- ### Initialize and Use RewardModel for Continuous Rewards Source: https://context7.com/lucidrains/rewind-reward-pytorch/llms.txt Initialize the RewardModel for continuous reward prediction and perform inference or training. Ensure ground truth rewards are provided for training. ```python import torch from rewind_reward_pytorch import RewardModel # Initialize reward model with default settings reward_model = RewardModel( decoder=dict( dim=768, depth=4, heads=8, attn_dim_head=64 ), mlp_predictor_depth=3, reward_bins=10, max_video_frames=16, dim_image_embed=768, num_register_tokens=4, lang_per_token_embed=True, sentence_transformer_path='sentence-transformers/all-MiniLM-L12-v2', categorical_rewards=False, use_hl_gauss_loss=True, reward_min_value=0., reward_max_value=1., reward_hl_gauss_loss_num_bins=20 ) # Natural language task commands commands = [ 'pick up the blue ball and put it in the red tray', 'pick up the red cube and put it in the green bin', ] # Video input: (batch, channels, frames, height, width) video = torch.rand(2, 3, 16, 224, 224) # Inference: predict per-frame progress rewards pred_rewards = reward_model(commands, video) # Output shape: (2, 16) - per frame progress prediction for each video assert pred_rewards.shape == (2, 16) # Training: compute loss with ground truth rewards rewards = torch.rand(2, 16) # Ground truth progress values [0, 1] loss = reward_model( commands, video, rewards=rewards, video_lens=torch.tensor([16, 14]) # Optional: actual video lengths ) loss.backward() ``` -------------------------------- ### Initialize and Use RewardModel for Categorical Rewards Source: https://context7.com/lucidrains/rewind-reward-pytorch/llms.txt Configure the RewardModel for categorical reward prediction using discrete bins and cross-entropy loss. Inference returns logits over reward bins. ```python import torch from rewind_reward_pytorch import RewardModel # Initialize with categorical rewards (discrete bins) reward_model = RewardModel( reward_bins=10, lang_per_token_embed=True, categorical_rewards=True ) commands = [ 'pick up the blue ball and put it in the red tray', 'pick up the red cube and put it in the green bin' ] video = torch.rand(2, 3, 16, 224, 224) # Inference returns logits over reward bins logits = reward_model(commands, video) # Output shape: (batch, frames, reward_bins) assert logits.shape == (2, 16, 10) # Get predicted reward class per frame predicted_bins = logits.argmax(dim=-1) # (2, 16) ``` -------------------------------- ### Predict Rewards with ReWiND Model Source: https://github.com/lucidrains/rewind-reward-pytorch/blob/main/README.md After training, use the RewardModel to predict rewards for given commands and video data. The output is a tensor of per-frame progress predictions. ```python # after much training pred_rewards = reward_model( commands, video ) # (2, 16) - per frame progress prediction assert pred_rewards.shape == (2, 16) ``` -------------------------------- ### Use RewardModel with Extra Embedding Tokens Source: https://context7.com/lucidrains/rewind-reward-pytorch/llms.txt Pass additional embedding tokens, such as from RGBD or tactile sensors, to enrich the model's input representation during training or inference. ```python import torch from rewind_reward_pytorch import RewardModel reward_model = RewardModel( reward_bins=10, lang_per_token_embed=True, categorical_rewards=False ) commands = [ 'pick up the blue ball and put it in the red tray', 'pick up the red cube and put it in the green bin', ] video = torch.rand(2, 3, 16, 224, 224) # Extra embedding tokens (e.g., from Adapt3R RGBD or tactile sensors) # Shape: (batch, num_tokens, embedding_dim) extra_embed_tokens = torch.randn(2, 7, 768) # Training with extra embeddings loss = reward_model( commands, video, extra_embed_tokens=extra_embed_tokens, video_lens=torch.randint(5, 16, (2,)), rewards=torch.randn((2, 16)) ) loss.backward() # Inference with extra embeddings pred = reward_model(commands, video, extra_embed_tokens=extra_embed_tokens) assert pred.shape == (2, 16) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.